Path: blob/master/servers/rendering/rendering_device_driver.h
11352 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 = std::max_element(RESOURCE_SIZES, RESOURCE_SIZES + sizeof...(RESOURCE_TYPES))[0];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);142143public:144/*****************/145/**** GENERIC ****/146/*****************/147148virtual Error initialize(uint32_t p_device_index, uint32_t p_frame_count) = 0;149150/****************/151/**** MEMORY ****/152/****************/153154enum MemoryAllocationType {155MEMORY_ALLOCATION_TYPE_CPU, // For images, CPU allocation also means linear, GPU is tiling optimal.156MEMORY_ALLOCATION_TYPE_GPU,157};158159/*****************/160/**** BUFFERS ****/161/*****************/162163enum BufferUsageBits {164BUFFER_USAGE_TRANSFER_FROM_BIT = (1 << 0),165BUFFER_USAGE_TRANSFER_TO_BIT = (1 << 1),166BUFFER_USAGE_TEXEL_BIT = (1 << 2),167BUFFER_USAGE_UNIFORM_BIT = (1 << 4),168BUFFER_USAGE_STORAGE_BIT = (1 << 5),169BUFFER_USAGE_INDEX_BIT = (1 << 6),170BUFFER_USAGE_VERTEX_BIT = (1 << 7),171BUFFER_USAGE_INDIRECT_BIT = (1 << 8),172BUFFER_USAGE_DEVICE_ADDRESS_BIT = (1 << 17),173};174175enum {176BUFFER_WHOLE_SIZE = ~0ULL177};178179virtual BufferID buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type) = 0;180// Only for a buffer with BUFFER_USAGE_TEXEL_BIT.181virtual bool buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) = 0;182virtual void buffer_free(BufferID p_buffer) = 0;183virtual uint64_t buffer_get_allocation_size(BufferID p_buffer) = 0;184virtual uint8_t *buffer_map(BufferID p_buffer) = 0;185virtual void buffer_unmap(BufferID p_buffer) = 0;186// Only for a buffer with BUFFER_USAGE_DEVICE_ADDRESS_BIT.187virtual uint64_t buffer_get_device_address(BufferID p_buffer) = 0;188189/*****************/190/**** TEXTURE ****/191/*****************/192193struct TextureView {194DataFormat format = DATA_FORMAT_MAX;195TextureSwizzle swizzle_r = TEXTURE_SWIZZLE_R;196TextureSwizzle swizzle_g = TEXTURE_SWIZZLE_G;197TextureSwizzle swizzle_b = TEXTURE_SWIZZLE_B;198TextureSwizzle swizzle_a = TEXTURE_SWIZZLE_A;199};200201enum TextureLayout {202TEXTURE_LAYOUT_UNDEFINED,203TEXTURE_LAYOUT_GENERAL,204TEXTURE_LAYOUT_STORAGE_OPTIMAL,205TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,206TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,207TEXTURE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,208TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,209TEXTURE_LAYOUT_COPY_SRC_OPTIMAL,210TEXTURE_LAYOUT_COPY_DST_OPTIMAL,211TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL,212TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL,213TEXTURE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL,214TEXTURE_LAYOUT_FRAGMENT_DENSITY_MAP_ATTACHMENT_OPTIMAL,215TEXTURE_LAYOUT_MAX216};217218enum TextureAspect {219TEXTURE_ASPECT_COLOR = 0,220TEXTURE_ASPECT_DEPTH = 1,221TEXTURE_ASPECT_STENCIL = 2,222TEXTURE_ASPECT_MAX223};224225enum TextureUsageMethod {226TEXTURE_USAGE_VRS_FRAGMENT_SHADING_RATE_BIT = TEXTURE_USAGE_MAX_BIT << 1,227TEXTURE_USAGE_VRS_FRAGMENT_DENSITY_MAP_BIT = TEXTURE_USAGE_MAX_BIT << 2,228};229230enum TextureAspectBits {231TEXTURE_ASPECT_COLOR_BIT = (1 << TEXTURE_ASPECT_COLOR),232TEXTURE_ASPECT_DEPTH_BIT = (1 << TEXTURE_ASPECT_DEPTH),233TEXTURE_ASPECT_STENCIL_BIT = (1 << TEXTURE_ASPECT_STENCIL),234};235236struct TextureSubresource {237TextureAspect aspect = TEXTURE_ASPECT_COLOR;238uint32_t layer = 0;239uint32_t mipmap = 0;240};241242struct TextureSubresourceLayers {243BitField<TextureAspectBits> aspect = {};244uint32_t mipmap = 0;245uint32_t base_layer = 0;246uint32_t layer_count = 0;247};248249struct TextureSubresourceRange {250BitField<TextureAspectBits> aspect = {};251uint32_t base_mipmap = 0;252uint32_t mipmap_count = 0;253uint32_t base_layer = 0;254uint32_t layer_count = 0;255};256257struct TextureCopyableLayout {258uint64_t offset = 0;259uint64_t size = 0;260uint64_t row_pitch = 0;261uint64_t depth_pitch = 0;262uint64_t layer_pitch = 0;263};264265virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) = 0;266virtual 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;267// texture_create_shared_*() can only use original, non-view textures as original. RenderingDevice is responsible for ensuring that.268virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) = 0;269virtual 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;270virtual void texture_free(TextureID p_texture) = 0;271virtual uint64_t texture_get_allocation_size(TextureID p_texture) = 0;272virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) = 0;273virtual uint8_t *texture_map(TextureID p_texture, const TextureSubresource &p_subresource) = 0;274virtual void texture_unmap(TextureID p_texture) = 0;275virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) = 0;276virtual bool texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) = 0;277278/*****************/279/**** SAMPLER ****/280/*****************/281282virtual SamplerID sampler_create(const SamplerState &p_state) = 0;283virtual void sampler_free(SamplerID p_sampler) = 0;284virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) = 0;285286/**********************/287/**** VERTEX ARRAY ****/288/**********************/289290virtual VertexFormatID vertex_format_create(VectorView<VertexAttribute> p_vertex_attribs) = 0;291virtual void vertex_format_free(VertexFormatID p_vertex_format) = 0;292293/******************/294/**** BARRIERS ****/295/******************/296297enum PipelineStageBits {298PIPELINE_STAGE_TOP_OF_PIPE_BIT = (1 << 0),299PIPELINE_STAGE_DRAW_INDIRECT_BIT = (1 << 1),300PIPELINE_STAGE_VERTEX_INPUT_BIT = (1 << 2),301PIPELINE_STAGE_VERTEX_SHADER_BIT = (1 << 3),302PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT = (1 << 4),303PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT = (1 << 5),304PIPELINE_STAGE_GEOMETRY_SHADER_BIT = (1 << 6),305PIPELINE_STAGE_FRAGMENT_SHADER_BIT = (1 << 7),306PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT = (1 << 8),307PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT = (1 << 9),308PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT = (1 << 10),309PIPELINE_STAGE_COMPUTE_SHADER_BIT = (1 << 11),310PIPELINE_STAGE_COPY_BIT = (1 << 12),311PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT = (1 << 13),312PIPELINE_STAGE_RESOLVE_BIT = (1 << 14),313PIPELINE_STAGE_ALL_GRAPHICS_BIT = (1 << 15),314PIPELINE_STAGE_ALL_COMMANDS_BIT = (1 << 16),315PIPELINE_STAGE_CLEAR_STORAGE_BIT = (1 << 17),316PIPELINE_STAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT = (1 << 22),317PIPELINE_STAGE_FRAGMENT_DENSITY_PROCESS_BIT = (1 << 23),318};319320enum BarrierAccessBits {321BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT = (1 << 0),322BARRIER_ACCESS_INDEX_READ_BIT = (1 << 1),323BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT = (1 << 2),324BARRIER_ACCESS_UNIFORM_READ_BIT = (1 << 3),325BARRIER_ACCESS_INPUT_ATTACHMENT_READ_BIT = (1 << 4),326BARRIER_ACCESS_SHADER_READ_BIT = (1 << 5),327BARRIER_ACCESS_SHADER_WRITE_BIT = (1 << 6),328BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT = (1 << 7),329BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT = (1 << 8),330BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT = (1 << 9),331BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT = (1 << 10),332BARRIER_ACCESS_COPY_READ_BIT = (1 << 11),333BARRIER_ACCESS_COPY_WRITE_BIT = (1 << 12),334BARRIER_ACCESS_HOST_READ_BIT = (1 << 13),335BARRIER_ACCESS_HOST_WRITE_BIT = (1 << 14),336BARRIER_ACCESS_MEMORY_READ_BIT = (1 << 15),337BARRIER_ACCESS_MEMORY_WRITE_BIT = (1 << 16),338BARRIER_ACCESS_FRAGMENT_SHADING_RATE_ATTACHMENT_READ_BIT = (1 << 23),339BARRIER_ACCESS_FRAGMENT_DENSITY_MAP_ATTACHMENT_READ_BIT = (1 << 24),340BARRIER_ACCESS_RESOLVE_READ_BIT = (1 << 25),341BARRIER_ACCESS_RESOLVE_WRITE_BIT = (1 << 26),342BARRIER_ACCESS_STORAGE_CLEAR_BIT = (1 << 27),343};344345// https://github.com/godotengine/godot/pull/110360 - "MemoryBarrier" conflicts with Windows header defines346struct MemoryAccessBarrier {347BitField<BarrierAccessBits> src_access = {};348BitField<BarrierAccessBits> dst_access = {};349};350351struct BufferBarrier {352BufferID buffer;353BitField<BarrierAccessBits> src_access = {};354BitField<BarrierAccessBits> dst_access = {};355uint64_t offset = 0;356uint64_t size = 0;357};358359struct TextureBarrier {360TextureID texture;361BitField<BarrierAccessBits> src_access = {};362BitField<BarrierAccessBits> dst_access = {};363TextureLayout prev_layout = TEXTURE_LAYOUT_UNDEFINED;364TextureLayout next_layout = TEXTURE_LAYOUT_UNDEFINED;365TextureSubresourceRange subresources;366};367368virtual void command_pipeline_barrier(369CommandBufferID p_cmd_buffer,370BitField<PipelineStageBits> p_src_stages,371BitField<PipelineStageBits> p_dst_stages,372VectorView<MemoryAccessBarrier> p_memory_barriers,373VectorView<BufferBarrier> p_buffer_barriers,374VectorView<TextureBarrier> p_texture_barriers) = 0;375376/****************/377/**** FENCES ****/378/****************/379380virtual FenceID fence_create() = 0;381virtual Error fence_wait(FenceID p_fence) = 0;382virtual void fence_free(FenceID p_fence) = 0;383384/********************/385/**** SEMAPHORES ****/386/********************/387388virtual SemaphoreID semaphore_create() = 0;389virtual void semaphore_free(SemaphoreID p_semaphore) = 0;390391/*************************/392/**** COMMAND BUFFERS ****/393/*************************/394395// ----- QUEUE FAMILY -----396397enum CommandQueueFamilyBits {398COMMAND_QUEUE_FAMILY_GRAPHICS_BIT = 0x1,399COMMAND_QUEUE_FAMILY_COMPUTE_BIT = 0x2,400COMMAND_QUEUE_FAMILY_TRANSFER_BIT = 0x4401};402403// 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.404// It is valid to specify no bits and a valid surface: in this case, the dedicated presentation queue family will be the preferred option.405virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) = 0;406407// ----- QUEUE -----408409virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) = 0;410virtual 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;411virtual void command_queue_free(CommandQueueID p_cmd_queue) = 0;412413// ----- POOL -----414415enum CommandBufferType {416COMMAND_BUFFER_TYPE_PRIMARY,417COMMAND_BUFFER_TYPE_SECONDARY,418};419420virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) = 0;421virtual bool command_pool_reset(CommandPoolID p_cmd_pool) = 0;422virtual void command_pool_free(CommandPoolID p_cmd_pool) = 0;423424// ----- BUFFER -----425426virtual CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) = 0;427virtual bool command_buffer_begin(CommandBufferID p_cmd_buffer) = 0;428virtual bool command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) = 0;429virtual void command_buffer_end(CommandBufferID p_cmd_buffer) = 0;430virtual void command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) = 0;431432/********************/433/**** SWAP CHAIN ****/434/********************/435436// The swap chain won't be valid for use until it is resized at least once.437virtual SwapChainID swap_chain_create(RenderingContextDriver::SurfaceID p_surface) = 0;438439// 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.440virtual Error swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) = 0;441442// Acquire the framebuffer that can be used for drawing. This must be called only once every time a new frame will be rendered.443virtual FramebufferID swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) = 0;444445// Retrieve the render pass that can be used to draw on the swap chain's framebuffers.446virtual RenderPassID swap_chain_get_render_pass(SwapChainID p_swap_chain) = 0;447448// Retrieve the rotation in degrees to apply as a pre-transform. Usually 0 on PC. May be 0, 90, 180 & 270 on Android.449virtual int swap_chain_get_pre_rotation_degrees(SwapChainID p_swap_chain) { return 0; }450451// Retrieve the format used by the swap chain's framebuffers.452virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) = 0;453454// Tells the swapchain the max_fps so it can use the proper frame pacing.455// Android uses this with Swappy library. Some implementations or platforms may ignore this hint.456virtual void swap_chain_set_max_fps(SwapChainID p_swap_chain, int p_max_fps) {}457458// Wait until all rendering associated to the swap chain is finished before deleting it.459virtual void swap_chain_free(SwapChainID p_swap_chain) = 0;460461/*********************/462/**** FRAMEBUFFER ****/463/*********************/464465virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) = 0;466virtual void framebuffer_free(FramebufferID p_framebuffer) = 0;467468/****************/469/**** SHADER ****/470/****************/471472struct ImmutableSampler {473UniformType type = UNIFORM_TYPE_MAX;474uint32_t binding = 0xffffffff; // Binding index as specified in shader.475LocalVector<ID> ids;476};477478// Creates a Pipeline State Object (PSO) out of the shader and all the input data it needs.479// Immutable samplers can be embedded when creating the pipeline layout on the condition they remain valid and unchanged, so they don't need to be480// specified when creating uniform sets PSO resource for binding.481virtual ShaderID shader_create_from_container(const Ref<RenderingShaderContainer> &p_shader_container, const Vector<ImmutableSampler> &p_immutable_samplers) = 0;482// Only meaningful if API_TRAIT_SHADER_CHANGE_INVALIDATION is SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH.483virtual uint32_t shader_get_layout_hash(ShaderID p_shader) { return 0; }484virtual void shader_free(ShaderID p_shader) = 0;485virtual void shader_destroy_modules(ShaderID p_shader) = 0;486487public:488/*********************/489/**** UNIFORM SET ****/490/*********************/491492struct BoundUniform {493UniformType type = UNIFORM_TYPE_MAX;494uint32_t binding = 0xffffffff; // Binding index as specified in shader.495LocalVector<ID> ids;496// Flag to indicate that this is an immutable sampler so it is skipped when creating uniform497// sets, as it would be set previously when creating the pipeline layout.498bool immutable_sampler = false;499};500501virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index, int p_linear_pool_index) = 0;502virtual void linear_uniform_set_pools_reset(int p_linear_pool_index) {}503virtual void uniform_set_free(UniformSetID p_uniform_set) = 0;504virtual bool uniform_sets_have_linear_pools() const { return false; }505506// ----- COMMANDS -----507508virtual void command_uniform_set_prepare_for_use(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) = 0;509510/******************/511/**** TRANSFER ****/512/******************/513514struct BufferCopyRegion {515uint64_t src_offset = 0;516uint64_t dst_offset = 0;517uint64_t size = 0;518};519520struct TextureCopyRegion {521TextureSubresourceLayers src_subresources;522Vector3i src_offset;523TextureSubresourceLayers dst_subresources;524Vector3i dst_offset;525Vector3i size;526};527528struct BufferTextureCopyRegion {529uint64_t buffer_offset = 0;530TextureSubresourceLayers texture_subresources;531Vector3i texture_offset;532Vector3i texture_region_size;533};534535virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) = 0;536virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) = 0;537538virtual 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;539virtual 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;540virtual 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;541542virtual 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;543virtual 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;544545/******************/546/**** PIPELINE ****/547/******************/548549virtual void pipeline_free(PipelineID p_pipeline) = 0;550551// ----- BINDING -----552553virtual void command_bind_push_constants(CommandBufferID p_cmd_buffer, ShaderID p_shader, uint32_t p_first_index, VectorView<uint32_t> p_data) = 0;554555// ----- CACHE -----556557virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) = 0;558virtual void pipeline_cache_free() = 0;559virtual size_t pipeline_cache_query_size() = 0;560virtual Vector<uint8_t> pipeline_cache_serialize() = 0;561562/*******************/563/**** RENDERING ****/564/*******************/565566// ----- SUBPASS -----567568enum AttachmentLoadOp {569ATTACHMENT_LOAD_OP_LOAD = 0,570ATTACHMENT_LOAD_OP_CLEAR = 1,571ATTACHMENT_LOAD_OP_DONT_CARE = 2,572};573574enum AttachmentStoreOp {575ATTACHMENT_STORE_OP_STORE = 0,576ATTACHMENT_STORE_OP_DONT_CARE = 1,577};578579struct Attachment {580DataFormat format = DATA_FORMAT_MAX;581TextureSamples samples = TEXTURE_SAMPLES_MAX;582AttachmentLoadOp load_op = ATTACHMENT_LOAD_OP_DONT_CARE;583AttachmentStoreOp store_op = ATTACHMENT_STORE_OP_DONT_CARE;584AttachmentLoadOp stencil_load_op = ATTACHMENT_LOAD_OP_DONT_CARE;585AttachmentStoreOp stencil_store_op = ATTACHMENT_STORE_OP_DONT_CARE;586TextureLayout initial_layout = TEXTURE_LAYOUT_UNDEFINED;587TextureLayout final_layout = TEXTURE_LAYOUT_UNDEFINED;588};589590struct AttachmentReference {591static constexpr uint32_t UNUSED = 0xffffffff;592uint32_t attachment = UNUSED;593TextureLayout layout = TEXTURE_LAYOUT_UNDEFINED;594BitField<TextureAspectBits> aspect = {};595};596597struct Subpass {598LocalVector<AttachmentReference> input_references;599LocalVector<AttachmentReference> color_references;600AttachmentReference depth_stencil_reference;601LocalVector<AttachmentReference> resolve_references;602LocalVector<uint32_t> preserve_attachments;603AttachmentReference fragment_shading_rate_reference;604Size2i fragment_shading_rate_texel_size;605};606607struct SubpassDependency {608uint32_t src_subpass = 0xffffffff;609uint32_t dst_subpass = 0xffffffff;610BitField<PipelineStageBits> src_stages = {};611BitField<PipelineStageBits> dst_stages = {};612BitField<BarrierAccessBits> src_access = {};613BitField<BarrierAccessBits> dst_access = {};614};615616virtual 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;617virtual void render_pass_free(RenderPassID p_render_pass) = 0;618619// ----- COMMANDS -----620621union RenderPassClearValue {622Color color = {};623struct {624float depth;625uint32_t stencil;626};627628RenderPassClearValue() {}629};630631struct AttachmentClear {632BitField<TextureAspectBits> aspect = {};633uint32_t color_attachment = 0xffffffff;634RenderPassClearValue value;635};636637virtual 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;638virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) = 0;639virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) = 0;640virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) = 0;641virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) = 0;642virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) = 0;643644// Binding.645virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) = 0;646virtual void command_bind_render_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) = 0;647virtual 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) = 0;648649// Drawing.650virtual 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;651virtual 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;652virtual 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;653virtual 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;654virtual 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;655virtual 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;656657// Buffer binding.658virtual void command_render_bind_vertex_buffers(CommandBufferID p_cmd_buffer, uint32_t p_binding_count, const BufferID *p_buffers, const uint64_t *p_offsets) = 0;659virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) = 0;660661// Dynamic state.662virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) = 0;663virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) = 0;664665// ----- PIPELINE -----666667virtual PipelineID render_pipeline_create(668ShaderID p_shader,669VertexFormatID p_vertex_format,670RenderPrimitive p_render_primitive,671PipelineRasterizationState p_rasterization_state,672PipelineMultisampleState p_multisample_state,673PipelineDepthStencilState p_depth_stencil_state,674PipelineColorBlendState p_blend_state,675VectorView<int32_t> p_color_attachments,676BitField<PipelineDynamicStateFlags> p_dynamic_state,677RenderPassID p_render_pass,678uint32_t p_render_subpass,679VectorView<PipelineSpecializationConstant> p_specialization_constants) = 0;680681/*****************/682/**** COMPUTE ****/683/*****************/684685// ----- COMMANDS -----686687// Binding.688virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) = 0;689virtual void command_bind_compute_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) = 0;690virtual 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) = 0;691692// Dispatching.693virtual void command_compute_dispatch(CommandBufferID p_cmd_buffer, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) = 0;694virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) = 0;695696// ----- PIPELINE -----697698virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) = 0;699700/******************/701/**** CALLBACK ****/702/******************/703704typedef void (*DriverCallback)(RenderingDeviceDriver *p_driver, CommandBufferID p_command_buffer, void *p_userdata);705706/*****************/707/**** QUERIES ****/708/*****************/709710// ----- TIMESTAMP -----711712// Basic.713virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) = 0;714virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) = 0;715virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) = 0;716virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) = 0;717718// Commands.719virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) = 0;720virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) = 0;721722/****************/723/**** LABELS ****/724/****************/725726virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) = 0;727virtual void command_end_label(CommandBufferID p_cmd_buffer) = 0;728729/****************/730/**** DEBUG *****/731/****************/732virtual void command_insert_breadcrumb(CommandBufferID p_cmd_buffer, uint32_t p_data) = 0;733734/********************/735/**** SUBMISSION ****/736/********************/737738virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) = 0;739virtual void end_segment() = 0;740741/**************/742/**** MISC ****/743/**************/744745enum ObjectType {746OBJECT_TYPE_TEXTURE,747OBJECT_TYPE_SAMPLER,748OBJECT_TYPE_BUFFER,749OBJECT_TYPE_SHADER,750OBJECT_TYPE_UNIFORM_SET,751OBJECT_TYPE_PIPELINE,752};753754struct MultiviewCapabilities {755bool is_supported = false;756bool geometry_shader_is_supported = false;757bool tessellation_shader_is_supported = false;758uint32_t max_view_count = 0;759uint32_t max_instance_count = 0;760};761762struct FragmentShadingRateCapabilities {763Size2i min_texel_size;764Size2i max_texel_size;765Size2i max_fragment_size;766bool pipeline_supported = false;767bool primitive_supported = false;768bool attachment_supported = false;769};770771struct FragmentDensityMapCapabilities {772Size2i min_texel_size;773Size2i max_texel_size;774Size2i offset_granularity;775bool attachment_supported = false;776bool dynamic_attachment_supported = false;777bool non_subsampled_images_supported = false;778bool invocations_supported = false;779bool offset_supported = false;780};781782enum ApiTrait {783API_TRAIT_HONORS_PIPELINE_BARRIERS,784API_TRAIT_SHADER_CHANGE_INVALIDATION,785API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT,786API_TRAIT_TEXTURE_DATA_ROW_PITCH_STEP,787API_TRAIT_SECONDARY_VIEWPORT_SCISSOR,788API_TRAIT_CLEARS_WITH_COPY_ENGINE,789API_TRAIT_USE_GENERAL_IN_COPY_QUEUES,790API_TRAIT_BUFFERS_REQUIRE_TRANSITIONS,791};792793enum ShaderChangeInvalidation {794SHADER_CHANGE_INVALIDATION_ALL_BOUND_UNIFORM_SETS,795// What Vulkan does.796SHADER_CHANGE_INVALIDATION_INCOMPATIBLE_SETS_PLUS_CASCADE,797// What D3D12 does.798SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH,799};800801enum DeviceFamily {802DEVICE_UNKNOWN,803DEVICE_OPENGL,804DEVICE_VULKAN,805DEVICE_DIRECTX,806DEVICE_METAL,807};808809struct Capabilities {810DeviceFamily device_family = DEVICE_UNKNOWN;811uint32_t version_major = 1;812uint32_t version_minor = 0;813};814815virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) = 0;816virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) = 0;817virtual uint64_t get_total_memory_used() = 0;818virtual uint64_t get_lazily_memory_used() = 0;819virtual uint64_t limit_get(Limit p_limit) = 0;820virtual uint64_t api_trait_get(ApiTrait p_trait);821virtual bool has_feature(Features p_feature) = 0;822virtual const MultiviewCapabilities &get_multiview_capabilities() = 0;823virtual const FragmentShadingRateCapabilities &get_fragment_shading_rate_capabilities() = 0;824virtual const FragmentDensityMapCapabilities &get_fragment_density_map_capabilities() = 0;825virtual String get_api_name() const = 0;826virtual String get_api_version() const = 0;827virtual String get_pipeline_cache_uuid() const = 0;828virtual const Capabilities &get_capabilities() const = 0;829virtual const RenderingShaderContainerFormat &get_shader_container_format() const = 0;830831virtual bool is_composite_alpha_supported(CommandQueueID p_queue) const { return false; }832833/******************/834835virtual ~RenderingDeviceDriver();836};837838using RDD = RenderingDeviceDriver;839840841