Path: blob/master/servers/rendering/rendering_device_driver.h
20951 views
/**************************************************************************/1/* rendering_device_driver.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132// ***********************************************************************************33// RenderingDeviceDriver - Design principles34// -----------------------------------------35// - Very little validation is done, and normally only in dev or debug builds.36// - Error reporting is generally simple: returning an id of 0 or a false boolean.37// - Certain enums/constants/structs follow Vulkan values/layout. That makes things easier for RDDVulkan (it asserts compatibility).38// - We allocate as little as possible in functions expected to be quick (a counterexample is loading/saving shaders) and use alloca() whenever suitable.39// - We try to back opaque ids with the native ones or memory addresses.40// - When using bookkeeping structures because the actual API id of a resource is not enough, we use a PagedAllocator.41// - Every struct has default initializers.42// - Using VectorView to take array-like arguments. Vector<uint8_t> is an exception (an indiom for "BLOB").43// - If a driver needs some higher-level information (the kind of info RenderingDevice keeps), it shall store a copy of what it needs.44// There's no backwards communication from the driver to query data from RenderingDevice.45// ***********************************************************************************4647#include "core/object/object.h"48#include "core/variant/type_info.h"49#include "servers/rendering/rendering_context_driver.h"50#include "servers/rendering/rendering_device_commons.h"5152class RenderingShaderContainer;53class RenderingShaderContainerFormat;5455// These utilities help drivers avoid allocations.56#define ALLOCA(m_size) ((m_size != 0) ? alloca(m_size) : nullptr)57#define ALLOCA_ARRAY(m_type, m_count) ((m_type *)ALLOCA(sizeof(m_type) * (m_count)))58#define ALLOCA_SINGLE(m_type) ALLOCA_ARRAY(m_type, 1)5960// This helps forwarding certain arrays to the API with confidence.61#define ARRAYS_COMPATIBLE(m_type_a, m_type_b) (sizeof(m_type_a) == sizeof(m_type_b) && alignof(m_type_a) == alignof(m_type_b))62// This is used when you also need to ensure structured types are compatible field-by-field.63// TODO: The fieldwise check is unimplemented, but still this one is useful, as a strong annotation about the needs.64#define ARRAYS_COMPATIBLE_FIELDWISE(m_type_a, m_type_b) ARRAYS_COMPATIBLE(m_type_a, m_type_b)65// Another utility, to make it easy to compare members of different enums, which is not fine with some compilers.66#define ENUM_MEMBERS_EQUAL(m_a, m_b) ((int64_t)m_a == (int64_t)m_b)6768// This helps using a single paged allocator for many resource types.69template <typename... RESOURCE_TYPES>70struct VersatileResourceTemplate {71static constexpr size_t RESOURCE_SIZES[] = { sizeof(RESOURCE_TYPES)... };72static constexpr size_t MAX_RESOURCE_SIZE = Span(RESOURCE_SIZES).max();73uint8_t data[MAX_RESOURCE_SIZE];7475template <typename T>76static T *allocate(PagedAllocator<VersatileResourceTemplate, true> &p_allocator) {77T *obj = (T *)p_allocator.alloc();78memnew_placement(obj, T);79return obj;80}8182template <typename T>83static void free(PagedAllocator<VersatileResourceTemplate, true> &p_allocator, T *p_object) {84p_object->~T();85p_allocator.free((VersatileResourceTemplate *)p_object);86}87};8889class RenderingDeviceDriver : public RenderingDeviceCommons {90GDSOFTCLASS(RenderingDeviceDriver, RenderingDeviceCommons);9192public:93struct ID {94uint64_t id = 0;95_ALWAYS_INLINE_ ID() = default;96_ALWAYS_INLINE_ ID(uint64_t p_id) :97id(p_id) {}98};99100#define DEFINE_ID(m_name) \101struct m_name##ID : public ID { \102_ALWAYS_INLINE_ explicit operator bool() const { \103return id != 0; \104} \105_ALWAYS_INLINE_ m_name##ID &operator=(m_name##ID p_other) { \106id = p_other.id; \107return *this; \108} \109_ALWAYS_INLINE_ bool operator<(const m_name##ID &p_other) const { \110return id < p_other.id; \111} \112_ALWAYS_INLINE_ bool operator==(const m_name##ID &p_other) const { \113return id == p_other.id; \114} \115_ALWAYS_INLINE_ bool operator!=(const m_name##ID &p_other) const { \116return id != p_other.id; \117} \118_ALWAYS_INLINE_ m_name##ID(const m_name##ID &p_other) : ID(p_other.id) {} \119_ALWAYS_INLINE_ explicit m_name##ID(uint64_t p_int) : ID(p_int) {} \120_ALWAYS_INLINE_ explicit m_name##ID(void *p_ptr) : ID((uint64_t)p_ptr) {} \121_ALWAYS_INLINE_ m_name##ID() = default; \122};123124// Id types declared before anything else to prevent cyclic dependencies between the different concerns.125DEFINE_ID(Buffer);126DEFINE_ID(Texture);127DEFINE_ID(Sampler);128DEFINE_ID(VertexFormat);129DEFINE_ID(CommandQueue);130DEFINE_ID(CommandQueueFamily);131DEFINE_ID(CommandPool);132DEFINE_ID(CommandBuffer);133DEFINE_ID(SwapChain);134DEFINE_ID(Framebuffer);135DEFINE_ID(Shader);136DEFINE_ID(UniformSet);137DEFINE_ID(Pipeline);138DEFINE_ID(RenderPass);139DEFINE_ID(QueryPool);140DEFINE_ID(Fence);141DEFINE_ID(Semaphore);142DEFINE_ID(AccelerationStructure);143DEFINE_ID(RaytracingPipeline);144145public:146/*****************/147/**** GENERIC ****/148/*****************/149150virtual Error initialize(uint32_t p_device_index, uint32_t p_frame_count) = 0;151152/****************/153/**** MEMORY ****/154/****************/155156enum MemoryAllocationType {157MEMORY_ALLOCATION_TYPE_CPU, // For images, CPU allocation also means linear, GPU is tiling optimal.158MEMORY_ALLOCATION_TYPE_GPU,159};160161/*****************/162/**** BUFFERS ****/163/*****************/164165enum BufferUsageBits {166BUFFER_USAGE_TRANSFER_FROM_BIT = (1 << 0),167BUFFER_USAGE_TRANSFER_TO_BIT = (1 << 1),168BUFFER_USAGE_TEXEL_BIT = (1 << 2),169BUFFER_USAGE_UNIFORM_BIT = (1 << 4),170BUFFER_USAGE_STORAGE_BIT = (1 << 5),171BUFFER_USAGE_INDEX_BIT = (1 << 6),172BUFFER_USAGE_VERTEX_BIT = (1 << 7),173BUFFER_USAGE_INDIRECT_BIT = (1 << 8),174BUFFER_USAGE_SHADER_BINDING_TABLE_BIT = (1 << 10),175BUFFER_USAGE_DEVICE_ADDRESS_BIT = (1 << 17),176BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT = (1 << 19),177BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT = (1 << 20),178// There are no Vulkan-equivalent. Try to use unused/unclaimed bits.179BUFFER_USAGE_DYNAMIC_PERSISTENT_BIT = (1 << 31),180};181182enum {183BUFFER_WHOLE_SIZE = ~0ULL184};185186/** Allocates a new GPU buffer. Must be destroyed with buffer_free().187* @param p_size The size in bytes of the buffer.188* @param p_usage Usage flags.189* @param p_allocation_type See MemoryAllocationType.190* @param p_frames_drawn Used for debug checks when BUFFER_USAGE_DYNAMIC_PERSISTENT_BIT is set.191* @return the buffer.192*/193virtual BufferID buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type, uint64_t p_frames_drawn) = 0;194// Only for a buffer with BUFFER_USAGE_TEXEL_BIT.195virtual bool buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) = 0;196virtual void buffer_free(BufferID p_buffer) = 0;197virtual uint64_t buffer_get_allocation_size(BufferID p_buffer) = 0;198virtual uint8_t *buffer_map(BufferID p_buffer) = 0;199virtual void buffer_unmap(BufferID p_buffer) = 0;200virtual uint8_t *buffer_persistent_map_advance(BufferID p_buffer, uint64_t p_frames_drawn) = 0;201virtual uint64_t buffer_get_dynamic_offsets(Span<BufferID> p_buffers) = 0;202virtual void buffer_flush(BufferID p_buffer) {}203// Only for a buffer with BUFFER_USAGE_DEVICE_ADDRESS_BIT.204virtual uint64_t buffer_get_device_address(BufferID p_buffer) = 0;205206/*****************/207/**** TEXTURE ****/208/*****************/209210struct TextureView {211DataFormat format = DATA_FORMAT_MAX;212TextureSwizzle swizzle_r = TEXTURE_SWIZZLE_R;213TextureSwizzle swizzle_g = TEXTURE_SWIZZLE_G;214TextureSwizzle swizzle_b = TEXTURE_SWIZZLE_B;215TextureSwizzle swizzle_a = TEXTURE_SWIZZLE_A;216};217218enum TextureLayout {219TEXTURE_LAYOUT_UNDEFINED,220TEXTURE_LAYOUT_GENERAL,221TEXTURE_LAYOUT_STORAGE_OPTIMAL,222TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,223TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,224TEXTURE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,225TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,226TEXTURE_LAYOUT_COPY_SRC_OPTIMAL,227TEXTURE_LAYOUT_COPY_DST_OPTIMAL,228TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL,229TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL,230TEXTURE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL,231TEXTURE_LAYOUT_FRAGMENT_DENSITY_MAP_ATTACHMENT_OPTIMAL,232TEXTURE_LAYOUT_MAX233};234235enum TextureAspect {236TEXTURE_ASPECT_COLOR = 0,237TEXTURE_ASPECT_DEPTH = 1,238TEXTURE_ASPECT_STENCIL = 2,239TEXTURE_ASPECT_MAX240};241242enum TextureUsageMethod {243TEXTURE_USAGE_VRS_FRAGMENT_SHADING_RATE_BIT = TEXTURE_USAGE_MAX_BIT << 1,244TEXTURE_USAGE_VRS_FRAGMENT_DENSITY_MAP_BIT = TEXTURE_USAGE_MAX_BIT << 2,245};246247enum TextureAspectBits {248TEXTURE_ASPECT_COLOR_BIT = (1 << TEXTURE_ASPECT_COLOR),249TEXTURE_ASPECT_DEPTH_BIT = (1 << TEXTURE_ASPECT_DEPTH),250TEXTURE_ASPECT_STENCIL_BIT = (1 << TEXTURE_ASPECT_STENCIL),251};252253struct TextureSubresource {254TextureAspect aspect = TEXTURE_ASPECT_COLOR;255uint32_t layer = 0;256uint32_t mipmap = 0;257};258259struct TextureSubresourceLayers {260BitField<TextureAspectBits> aspect = {};261uint32_t mipmap = 0;262uint32_t base_layer = 0;263uint32_t layer_count = 0;264};265266struct TextureSubresourceRange {267BitField<TextureAspectBits> aspect = {};268uint32_t base_mipmap = 0;269uint32_t mipmap_count = 0;270uint32_t base_layer = 0;271uint32_t layer_count = 0;272};273274struct TextureCopyableLayout {275uint64_t size = 0;276uint64_t row_pitch = 0;277};278279virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) = 0;280virtual TextureID texture_create_from_extension(uint64_t p_native_texture, TextureType p_type, DataFormat p_format, uint32_t p_array_layers, bool p_depth_stencil, uint32_t p_mipmaps) = 0;281// texture_create_shared_*() can only use original, non-view textures as original. RenderingDevice is responsible for ensuring that.282virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) = 0;283virtual TextureID texture_create_shared_from_slice(TextureID p_original_texture, const TextureView &p_view, TextureSliceType p_slice_type, uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps) = 0;284virtual void texture_free(TextureID p_texture) = 0;285virtual uint64_t texture_get_allocation_size(TextureID p_texture) = 0;286// Returns a texture layout for buffer <-> texture copies. If you are copying multiple texture subresources to/from the same buffer,287// you are responsible for correctly aligning the start offset for every buffer region. See API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT.288virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) = 0;289// Returns the data of a texture layer for a CPU texture that was created with TEXTURE_USAGE_CPU_READ_BIT.290virtual Vector<uint8_t> texture_get_data(TextureID p_texture, uint32_t p_layer) = 0;291virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) = 0;292virtual bool texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) = 0;293294/*****************/295/**** SAMPLER ****/296/*****************/297298virtual SamplerID sampler_create(const SamplerState &p_state) = 0;299virtual void sampler_free(SamplerID p_sampler) = 0;300virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) = 0;301302/**********************/303/**** VERTEX ARRAY ****/304/**********************/305306virtual VertexFormatID vertex_format_create(Span<VertexAttribute> p_vertex_attribs, const VertexAttributeBindingsMap &p_vertex_bindings) = 0;307virtual void vertex_format_free(VertexFormatID p_vertex_format) = 0;308309/******************/310/**** BARRIERS ****/311/******************/312313enum PipelineStageBits {314PIPELINE_STAGE_TOP_OF_PIPE_BIT = (1 << 0),315PIPELINE_STAGE_DRAW_INDIRECT_BIT = (1 << 1),316PIPELINE_STAGE_VERTEX_INPUT_BIT = (1 << 2),317PIPELINE_STAGE_VERTEX_SHADER_BIT = (1 << 3),318PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT = (1 << 4),319PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT = (1 << 5),320PIPELINE_STAGE_GEOMETRY_SHADER_BIT = (1 << 6),321PIPELINE_STAGE_FRAGMENT_SHADER_BIT = (1 << 7),322PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT = (1 << 8),323PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT = (1 << 9),324PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT = (1 << 10),325PIPELINE_STAGE_COMPUTE_SHADER_BIT = (1 << 11),326PIPELINE_STAGE_COPY_BIT = (1 << 12),327PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT = (1 << 13),328PIPELINE_STAGE_RESOLVE_BIT = (1 << 14),329PIPELINE_STAGE_ALL_GRAPHICS_BIT = (1 << 15),330PIPELINE_STAGE_ALL_COMMANDS_BIT = (1 << 16),331PIPELINE_STAGE_CLEAR_STORAGE_BIT = (1 << 17),332PIPELINE_STAGE_RAY_TRACING_SHADER_BIT = (1 << 21),333PIPELINE_STAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT = (1 << 22),334PIPELINE_STAGE_FRAGMENT_DENSITY_PROCESS_BIT = (1 << 23),335PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT = (1 << 25),336};337338enum BarrierAccessBits {339BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT = (1 << 0),340BARRIER_ACCESS_INDEX_READ_BIT = (1 << 1),341BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT = (1 << 2),342BARRIER_ACCESS_UNIFORM_READ_BIT = (1 << 3),343BARRIER_ACCESS_INPUT_ATTACHMENT_READ_BIT = (1 << 4),344BARRIER_ACCESS_SHADER_READ_BIT = (1 << 5),345BARRIER_ACCESS_SHADER_WRITE_BIT = (1 << 6),346BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT = (1 << 7),347BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT = (1 << 8),348BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT = (1 << 9),349BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT = (1 << 10),350BARRIER_ACCESS_COPY_READ_BIT = (1 << 11),351BARRIER_ACCESS_COPY_WRITE_BIT = (1 << 12),352BARRIER_ACCESS_HOST_READ_BIT = (1 << 13),353BARRIER_ACCESS_HOST_WRITE_BIT = (1 << 14),354BARRIER_ACCESS_MEMORY_READ_BIT = (1 << 15),355BARRIER_ACCESS_MEMORY_WRITE_BIT = (1 << 16),356BARRIER_ACCESS_ACCELERATION_STRUCTURE_READ_BIT = (1 << 21),357BARRIER_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT = (1 << 22),358BARRIER_ACCESS_FRAGMENT_SHADING_RATE_ATTACHMENT_READ_BIT = (1 << 23),359BARRIER_ACCESS_FRAGMENT_DENSITY_MAP_ATTACHMENT_READ_BIT = (1 << 24),360BARRIER_ACCESS_RESOLVE_READ_BIT = (1 << 25),361BARRIER_ACCESS_RESOLVE_WRITE_BIT = (1 << 26),362BARRIER_ACCESS_STORAGE_CLEAR_BIT = (1 << 27),363};364365// https://github.com/godotengine/godot/pull/110360 - "MemoryBarrier" conflicts with Windows header defines366struct MemoryAccessBarrier {367BitField<BarrierAccessBits> src_access = {};368BitField<BarrierAccessBits> dst_access = {};369};370371struct BufferBarrier {372BufferID buffer;373BitField<BarrierAccessBits> src_access = {};374BitField<BarrierAccessBits> dst_access = {};375uint64_t offset = 0;376uint64_t size = 0;377};378379struct TextureBarrier {380TextureID texture;381BitField<BarrierAccessBits> src_access = {};382BitField<BarrierAccessBits> dst_access = {};383TextureLayout prev_layout = TEXTURE_LAYOUT_UNDEFINED;384TextureLayout next_layout = TEXTURE_LAYOUT_UNDEFINED;385TextureSubresourceRange subresources;386};387388struct AccelerationStructureBarrier {389AccelerationStructureID acceleration_structure;390BitField<BarrierAccessBits> src_access;391BitField<BarrierAccessBits> dst_access;392uint64_t offset = 0;393uint64_t size = 0;394};395396virtual void command_pipeline_barrier(397CommandBufferID p_cmd_buffer,398BitField<PipelineStageBits> p_src_stages,399BitField<PipelineStageBits> p_dst_stages,400VectorView<MemoryAccessBarrier> p_memory_barriers,401VectorView<BufferBarrier> p_buffer_barriers,402VectorView<TextureBarrier> p_texture_barriers,403VectorView<AccelerationStructureBarrier> p_acceleration_structure_barriers) = 0;404405/****************/406/**** FENCES ****/407/****************/408409virtual FenceID fence_create() = 0;410virtual Error fence_wait(FenceID p_fence) = 0;411virtual void fence_free(FenceID p_fence) = 0;412413/********************/414/**** SEMAPHORES ****/415/********************/416417virtual SemaphoreID semaphore_create() = 0;418virtual void semaphore_free(SemaphoreID p_semaphore) = 0;419420/*************************/421/**** COMMAND BUFFERS ****/422/*************************/423424// ----- QUEUE FAMILY -----425426enum CommandQueueFamilyBits {427COMMAND_QUEUE_FAMILY_GRAPHICS_BIT = 0x1,428COMMAND_QUEUE_FAMILY_COMPUTE_BIT = 0x2,429COMMAND_QUEUE_FAMILY_TRANSFER_BIT = 0x4430};431432// The requested command queue family must support all specified bits or it'll fail to return a valid family otherwise. If a valid surface is specified, the queue must support presenting to it.433// It is valid to specify no bits and a valid surface: in this case, the dedicated presentation queue family will be the preferred option.434virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) = 0;435436// ----- QUEUE -----437438virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) = 0;439virtual Error command_queue_execute_and_present(CommandQueueID p_cmd_queue, VectorView<SemaphoreID> p_wait_semaphores, VectorView<CommandBufferID> p_cmd_buffers, VectorView<SemaphoreID> p_cmd_semaphores, FenceID p_cmd_fence, VectorView<SwapChainID> p_swap_chains) = 0;440virtual void command_queue_free(CommandQueueID p_cmd_queue) = 0;441442// ----- POOL -----443444enum CommandBufferType {445COMMAND_BUFFER_TYPE_PRIMARY,446COMMAND_BUFFER_TYPE_SECONDARY,447};448449virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) = 0;450virtual bool command_pool_reset(CommandPoolID p_cmd_pool) = 0;451virtual void command_pool_free(CommandPoolID p_cmd_pool) = 0;452453// ----- BUFFER -----454455virtual CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) = 0;456virtual bool command_buffer_begin(CommandBufferID p_cmd_buffer) = 0;457virtual bool command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) = 0;458virtual void command_buffer_end(CommandBufferID p_cmd_buffer) = 0;459virtual void command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) = 0;460461/********************/462/**** SWAP CHAIN ****/463/********************/464465// The swap chain won't be valid for use until it is resized at least once.466virtual SwapChainID swap_chain_create(RenderingContextDriver::SurfaceID p_surface) = 0;467468// The swap chain must not be in use when a resize is requested. Wait until all rendering associated to the swap chain is finished before resizing it.469virtual Error swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) = 0;470471// Acquire the framebuffer that can be used for drawing. This must be called only once every time a new frame will be rendered.472virtual FramebufferID swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) = 0;473474// Retrieve the render pass that can be used to draw on the swap chain's framebuffers.475virtual RenderPassID swap_chain_get_render_pass(SwapChainID p_swap_chain) = 0;476477// Retrieve the rotation in degrees to apply as a pre-transform. Usually 0 on PC. May be 0, 90, 180 & 270 on Android.478virtual int swap_chain_get_pre_rotation_degrees(SwapChainID p_swap_chain) { return 0; }479480// Retrieve the format used by the swap chain's framebuffers.481virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) = 0;482483// Tells the swapchain the max_fps so it can use the proper frame pacing.484// Android uses this with Swappy library. Some implementations or platforms may ignore this hint.485virtual void swap_chain_set_max_fps(SwapChainID p_swap_chain, int p_max_fps) {}486487// Wait until all rendering associated to the swap chain is finished before deleting it.488virtual void swap_chain_free(SwapChainID p_swap_chain) = 0;489490/*********************/491/**** FRAMEBUFFER ****/492/*********************/493494virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) = 0;495virtual void framebuffer_free(FramebufferID p_framebuffer) = 0;496497/****************/498/**** SHADER ****/499/****************/500501struct ImmutableSampler {502UniformType type = UNIFORM_TYPE_MAX;503uint32_t binding = 0xffffffff; // Binding index as specified in shader.504LocalVector<ID> ids;505};506507// Creates a Pipeline State Object (PSO) out of the shader and all the input data it needs.508// Immutable samplers can be embedded when creating the pipeline layout on the condition they remain valid and unchanged, so they don't need to be509// specified when creating uniform sets PSO resource for binding.510virtual ShaderID shader_create_from_container(const Ref<RenderingShaderContainer> &p_shader_container, const Vector<ImmutableSampler> &p_immutable_samplers) = 0;511// Only meaningful if API_TRAIT_SHADER_CHANGE_INVALIDATION is SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH.512virtual uint32_t shader_get_layout_hash(ShaderID p_shader) { return 0; }513virtual void shader_free(ShaderID p_shader) = 0;514virtual void shader_destroy_modules(ShaderID p_shader) = 0;515516public:517/*********************/518/**** UNIFORM SET ****/519/*********************/520521struct BoundUniform {522UniformType type = UNIFORM_TYPE_MAX;523uint32_t binding = 0xffffffff; // Binding index as specified in shader.524LocalVector<ID> ids;525// Flag to indicate that this is an immutable sampler so it is skipped when creating uniform526// sets, as it would be set previously when creating the pipeline layout.527bool immutable_sampler = false;528529_FORCE_INLINE_ bool is_dynamic() const {530return type == UNIFORM_TYPE_STORAGE_BUFFER_DYNAMIC || type == UNIFORM_TYPE_UNIFORM_BUFFER_DYNAMIC;531}532};533534virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index, int p_linear_pool_index) = 0;535virtual void linear_uniform_set_pools_reset(int p_linear_pool_index) {}536virtual void uniform_set_free(UniformSetID p_uniform_set) = 0;537virtual bool uniform_sets_have_linear_pools() const { return false; }538virtual uint32_t uniform_sets_get_dynamic_offsets(VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count) const = 0;539540// ----- COMMANDS -----541542virtual void command_uniform_set_prepare_for_use(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) = 0;543544/******************/545/**** TRANSFER ****/546/******************/547548struct BufferCopyRegion {549uint64_t src_offset = 0;550uint64_t dst_offset = 0;551uint64_t size = 0;552};553554struct TextureCopyRegion {555TextureSubresourceLayers src_subresources;556Vector3i src_offset;557TextureSubresourceLayers dst_subresources;558Vector3i dst_offset;559Vector3i size;560};561562struct BufferTextureCopyRegion {563uint64_t buffer_offset = 0;564uint64_t row_pitch = 0;565TextureSubresource texture_subresource;566Vector3i texture_offset;567Vector3i texture_region_size;568};569570virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) = 0;571virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) = 0;572573virtual void command_copy_texture(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, VectorView<TextureCopyRegion> p_regions) = 0;574virtual void command_resolve_texture(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, uint32_t p_src_layer, uint32_t p_src_mipmap, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, uint32_t p_dst_layer, uint32_t p_dst_mipmap) = 0;575virtual void command_clear_color_texture(CommandBufferID p_cmd_buffer, TextureID p_texture, TextureLayout p_texture_layout, const Color &p_color, const TextureSubresourceRange &p_subresources) = 0;576virtual void command_clear_depth_stencil_texture(CommandBufferID p_cmd_buffer, TextureID p_texture, TextureLayout p_texture_layout, float p_depth, uint8_t p_stencil, const TextureSubresourceRange &p_subresources) = 0;577578virtual void command_copy_buffer_to_texture(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, VectorView<BufferTextureCopyRegion> p_regions) = 0;579virtual void command_copy_texture_to_buffer(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, BufferID p_dst_buffer, VectorView<BufferTextureCopyRegion> p_regions) = 0;580581/******************/582/**** PIPELINE ****/583/******************/584585virtual void pipeline_free(PipelineID p_pipeline) = 0;586587// ----- BINDING -----588589virtual void command_bind_push_constants(CommandBufferID p_cmd_buffer, ShaderID p_shader, uint32_t p_first_index, VectorView<uint32_t> p_data) = 0;590591// ----- CACHE -----592593virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) = 0;594virtual void pipeline_cache_free() = 0;595virtual size_t pipeline_cache_query_size() = 0;596virtual Vector<uint8_t> pipeline_cache_serialize() = 0;597598/*******************/599/**** RENDERING ****/600/*******************/601602// ----- SUBPASS -----603604enum AttachmentLoadOp {605ATTACHMENT_LOAD_OP_LOAD = 0,606ATTACHMENT_LOAD_OP_CLEAR = 1,607ATTACHMENT_LOAD_OP_DONT_CARE = 2,608};609610enum AttachmentStoreOp {611ATTACHMENT_STORE_OP_STORE = 0,612ATTACHMENT_STORE_OP_DONT_CARE = 1,613};614615struct Attachment {616DataFormat format = DATA_FORMAT_MAX;617TextureSamples samples = TEXTURE_SAMPLES_MAX;618AttachmentLoadOp load_op = ATTACHMENT_LOAD_OP_DONT_CARE;619AttachmentStoreOp store_op = ATTACHMENT_STORE_OP_DONT_CARE;620AttachmentLoadOp stencil_load_op = ATTACHMENT_LOAD_OP_DONT_CARE;621AttachmentStoreOp stencil_store_op = ATTACHMENT_STORE_OP_DONT_CARE;622TextureLayout initial_layout = TEXTURE_LAYOUT_UNDEFINED;623TextureLayout final_layout = TEXTURE_LAYOUT_UNDEFINED;624};625626struct AttachmentReference {627static constexpr uint32_t UNUSED = 0xffffffff;628uint32_t attachment = UNUSED;629TextureLayout layout = TEXTURE_LAYOUT_UNDEFINED;630BitField<TextureAspectBits> aspect = {};631};632633struct Subpass {634LocalVector<AttachmentReference> input_references;635LocalVector<AttachmentReference> color_references;636AttachmentReference depth_stencil_reference;637AttachmentReference depth_resolve_reference;638LocalVector<AttachmentReference> resolve_references;639LocalVector<uint32_t> preserve_attachments;640AttachmentReference fragment_shading_rate_reference;641Size2i fragment_shading_rate_texel_size;642};643644struct SubpassDependency {645uint32_t src_subpass = 0xffffffff;646uint32_t dst_subpass = 0xffffffff;647BitField<PipelineStageBits> src_stages = {};648BitField<PipelineStageBits> dst_stages = {};649BitField<BarrierAccessBits> src_access = {};650BitField<BarrierAccessBits> dst_access = {};651};652653virtual RenderPassID render_pass_create(VectorView<Attachment> p_attachments, VectorView<Subpass> p_subpasses, VectorView<SubpassDependency> p_subpass_dependencies, uint32_t p_view_count, AttachmentReference p_fragment_density_map_attachment) = 0;654virtual void render_pass_free(RenderPassID p_render_pass) = 0;655656// ----- COMMANDS -----657658union RenderPassClearValue {659Color color = {};660struct {661float depth;662uint32_t stencil;663};664665RenderPassClearValue() {}666};667668struct AttachmentClear {669BitField<TextureAspectBits> aspect = {};670uint32_t color_attachment = 0xffffffff;671RenderPassClearValue value;672};673674virtual void command_begin_render_pass(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, FramebufferID p_framebuffer, CommandBufferType p_cmd_buffer_type, const Rect2i &p_rect, VectorView<RenderPassClearValue> p_clear_values) = 0;675virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) = 0;676virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) = 0;677virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) = 0;678virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) = 0;679virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) = 0;680681// Binding.682virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) = 0;683virtual void command_bind_render_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count, uint32_t p_dynamic_offsets) = 0;684685// Drawing.686virtual void command_render_draw(CommandBufferID p_cmd_buffer, uint32_t p_vertex_count, uint32_t p_instance_count, uint32_t p_base_vertex, uint32_t p_first_instance) = 0;687virtual void command_render_draw_indexed(CommandBufferID p_cmd_buffer, uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index, int32_t p_vertex_offset, uint32_t p_first_instance) = 0;688virtual void command_render_draw_indexed_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) = 0;689virtual void command_render_draw_indexed_indirect_count(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) = 0;690virtual void command_render_draw_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) = 0;691virtual void command_render_draw_indirect_count(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) = 0;692693// Buffer binding.694virtual void command_render_bind_vertex_buffers(CommandBufferID p_cmd_buffer, uint32_t p_binding_count, const BufferID *p_buffers, const uint64_t *p_offsets, uint64_t p_dynamic_offsets) = 0;695virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) = 0;696697// Dynamic state.698virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) = 0;699virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) = 0;700701// ----- PIPELINE -----702703virtual PipelineID render_pipeline_create(704ShaderID p_shader,705VertexFormatID p_vertex_format,706RenderPrimitive p_render_primitive,707PipelineRasterizationState p_rasterization_state,708PipelineMultisampleState p_multisample_state,709PipelineDepthStencilState p_depth_stencil_state,710PipelineColorBlendState p_blend_state,711VectorView<int32_t> p_color_attachments,712BitField<PipelineDynamicStateFlags> p_dynamic_state,713RenderPassID p_render_pass,714uint32_t p_render_subpass,715VectorView<PipelineSpecializationConstant> p_specialization_constants) = 0;716717/*****************/718/**** COMPUTE ****/719/*****************/720721// ----- COMMANDS -----722723// Binding.724virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) = 0;725virtual void command_bind_compute_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count, uint32_t p_dynamic_offsets) = 0;726727// Dispatching.728virtual void command_compute_dispatch(CommandBufferID p_cmd_buffer, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) = 0;729virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) = 0;730731// ----- PIPELINE -----732733virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) = 0;734735/********************/736/**** RAYTRACING ****/737/********************/738739// ----- ACCELERATION STRUCTURE -----740741enum AccelerationStructureType {742ACCELERATION_STRUCTURE_TYPE_BLAS,743ACCELERATION_STRUCTURE_TYPE_TLAS,744};745746enum AccelerationStructureGeometryBits {747ACCELERATION_STRUCTURE_GEOMETRY_OPAQUE = 1 << 0,748ACCELERATION_STRUCTURE_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION = 1 << 1,749};750751virtual AccelerationStructureID blas_create(BufferID p_vertex_buffer, uint64_t p_vertex_offset, VertexFormatID p_vertex_format, uint32_t p_vertex_count, uint32_t p_position_attribute_location, BufferID p_index_buffer, IndexBufferFormat p_index_format, uint64_t p_index_offset, uint32_t p_index_count, BitField<AccelerationStructureGeometryBits> p_geometry_bits) = 0;752virtual uint32_t tlas_instances_buffer_get_size_bytes(uint32_t p_instance_count) = 0;753virtual void tlas_instances_buffer_fill(BufferID p_instances_buffer, VectorView<AccelerationStructureID> p_blases, VectorView<Transform3D> p_transforms) = 0;754virtual AccelerationStructureID tlas_create(BufferID p_instances_buffer) = 0;755virtual void acceleration_structure_free(AccelerationStructureID p_acceleration_structure) = 0;756virtual uint32_t acceleration_structure_get_scratch_size_bytes(AccelerationStructureID p_acceleration_structure) = 0;757758// ----- PIPELINE -----759760virtual RaytracingPipelineID raytracing_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) = 0;761virtual void raytracing_pipeline_free(RaytracingPipelineID p_pipeline) = 0;762763// ----- COMMANDS -----764765virtual void command_build_acceleration_structure(CommandBufferID p_cmd_buffer, AccelerationStructureID p_acceleration_structure, BufferID p_scratch_buffer) = 0;766virtual void command_bind_raytracing_pipeline(CommandBufferID p_cmd_buffer, RaytracingPipelineID p_pipeline) = 0;767virtual void command_bind_raytracing_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) = 0;768virtual void command_trace_rays(CommandBufferID p_cmd_buffer, uint32_t p_width, uint32_t p_height) = 0;769770/******************/771/**** CALLBACK ****/772/******************/773774typedef void (*DriverCallback)(RenderingDeviceDriver *p_driver, CommandBufferID p_command_buffer, void *p_userdata);775776/*****************/777/**** QUERIES ****/778/*****************/779780// ----- TIMESTAMP -----781782// Basic.783virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) = 0;784virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) = 0;785virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) = 0;786virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) = 0;787788// Commands.789virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) = 0;790virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) = 0;791792/****************/793/**** LABELS ****/794/****************/795796virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) = 0;797virtual void command_end_label(CommandBufferID p_cmd_buffer) = 0;798799/****************/800/**** DEBUG *****/801/****************/802virtual void command_insert_breadcrumb(CommandBufferID p_cmd_buffer, uint32_t p_data) = 0;803804/********************/805/**** SUBMISSION ****/806/********************/807808virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) = 0;809virtual void end_segment() = 0;810811/**************/812/**** MISC ****/813/**************/814815enum ObjectType {816OBJECT_TYPE_TEXTURE,817OBJECT_TYPE_SAMPLER,818OBJECT_TYPE_BUFFER,819OBJECT_TYPE_SHADER,820OBJECT_TYPE_UNIFORM_SET,821OBJECT_TYPE_PIPELINE,822OBJECT_TYPE_ACCELERATION_STRUCTURE,823OBJECT_TYPE_RAYTRACING_PIPELINE,824};825826struct MultiviewCapabilities {827bool is_supported = false;828bool geometry_shader_is_supported = false;829bool tessellation_shader_is_supported = false;830uint32_t max_view_count = 0;831uint32_t max_instance_count = 0;832};833834struct FragmentShadingRateCapabilities {835Size2i min_texel_size;836Size2i max_texel_size;837Size2i max_fragment_size;838bool pipeline_supported = false;839bool primitive_supported = false;840bool attachment_supported = false;841};842843struct FragmentDensityMapCapabilities {844Size2i min_texel_size;845Size2i max_texel_size;846Size2i offset_granularity;847bool attachment_supported = false;848bool dynamic_attachment_supported = false;849bool non_subsampled_images_supported = false;850bool invocations_supported = false;851bool offset_supported = false;852};853854enum ApiTrait {855API_TRAIT_HONORS_PIPELINE_BARRIERS,856API_TRAIT_SHADER_CHANGE_INVALIDATION,857API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT,858API_TRAIT_TEXTURE_DATA_ROW_PITCH_STEP,859API_TRAIT_SECONDARY_VIEWPORT_SCISSOR,860API_TRAIT_CLEARS_WITH_COPY_ENGINE,861API_TRAIT_USE_GENERAL_IN_COPY_QUEUES,862API_TRAIT_BUFFERS_REQUIRE_TRANSITIONS,863API_TRAIT_TEXTURE_OUTPUTS_REQUIRE_CLEARS,864};865866enum ShaderChangeInvalidation {867SHADER_CHANGE_INVALIDATION_ALL_BOUND_UNIFORM_SETS,868// What Vulkan does.869SHADER_CHANGE_INVALIDATION_INCOMPATIBLE_SETS_PLUS_CASCADE,870// What D3D12 does.871SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH,872};873874enum DeviceFamily {875DEVICE_UNKNOWN,876DEVICE_OPENGL,877DEVICE_VULKAN,878DEVICE_DIRECTX,879DEVICE_METAL,880};881882struct Capabilities {883DeviceFamily device_family = DEVICE_UNKNOWN;884uint32_t version_major = 1;885uint32_t version_minor = 0;886};887888virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) = 0;889virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) = 0;890virtual uint64_t get_total_memory_used() = 0;891virtual uint64_t get_lazily_memory_used() = 0;892virtual uint64_t limit_get(Limit p_limit) = 0;893virtual uint64_t api_trait_get(ApiTrait p_trait);894virtual bool has_feature(Features p_feature) = 0;895virtual const MultiviewCapabilities &get_multiview_capabilities() = 0;896virtual const FragmentShadingRateCapabilities &get_fragment_shading_rate_capabilities() = 0;897virtual const FragmentDensityMapCapabilities &get_fragment_density_map_capabilities() = 0;898virtual String get_api_name() const = 0;899virtual String get_api_version() const = 0;900virtual String get_pipeline_cache_uuid() const = 0;901virtual const Capabilities &get_capabilities() const = 0;902virtual const RenderingShaderContainerFormat &get_shader_container_format() const = 0;903904virtual bool is_composite_alpha_supported(CommandQueueID p_queue) const { return false; }905906/******************/907908virtual ~RenderingDeviceDriver();909};910911using RDD = RenderingDeviceDriver;912913914