rendering_device_driver.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /**************************************************************************/
  2. /* rendering_device_driver.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef RENDERING_DEVICE_DRIVER_H
  31. #define RENDERING_DEVICE_DRIVER_H
  32. // ***********************************************************************************
  33. // RenderingDeviceDriver - Design principles
  34. // -----------------------------------------
  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. // ***********************************************************************************
  46. #include "core/object/object.h"
  47. #include "core/variant/type_info.h"
  48. #include "servers/display_server.h"
  49. #include "servers/rendering/rendering_context_driver.h"
  50. #include "servers/rendering/rendering_device_commons.h"
  51. #include <algorithm>
  52. // This may one day be used in Godot for interoperability between C arrays, Vector and LocalVector.
  53. // (See https://github.com/godotengine/godot-proposals/issues/5144.)
  54. template <typename T>
  55. class VectorView {
  56. const T *_ptr = nullptr;
  57. const uint32_t _size = 0;
  58. public:
  59. const T &operator[](uint32_t p_index) {
  60. DEV_ASSERT(p_index < _size);
  61. return _ptr[p_index];
  62. }
  63. _ALWAYS_INLINE_ const T *ptr() const { return _ptr; }
  64. _ALWAYS_INLINE_ uint32_t size() const { return _size; }
  65. VectorView() = default;
  66. VectorView(const T &p_ptr) :
  67. // With this one you can pass a single element very conveniently!
  68. _ptr(&p_ptr),
  69. _size(1) {}
  70. VectorView(const T *p_ptr, uint32_t p_size) :
  71. _ptr(p_ptr), _size(p_size) {}
  72. VectorView(const Vector<T> &p_lv) :
  73. _ptr(p_lv.ptr()), _size(p_lv.size()) {}
  74. VectorView(const LocalVector<T> &p_lv) :
  75. _ptr(p_lv.ptr()), _size(p_lv.size()) {}
  76. };
  77. // These utilities help drivers avoid allocations.
  78. #define ALLOCA(m_size) ((m_size != 0) ? alloca(m_size) : nullptr)
  79. #define ALLOCA_ARRAY(m_type, m_count) ((m_type *)ALLOCA(sizeof(m_type) * (m_count)))
  80. #define ALLOCA_SINGLE(m_type) ALLOCA_ARRAY(m_type, 1)
  81. // This helps forwarding certain arrays to the API with confidence.
  82. #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))
  83. // This is used when you also need to ensure structured types are compatible field-by-field.
  84. // TODO: The fieldwise check is unimplemented, but still this one is useful, as a strong annotation about the needs.
  85. #define ARRAYS_COMPATIBLE_FIELDWISE(m_type_a, m_type_b) ARRAYS_COMPATIBLE(m_type_a, m_type_b)
  86. // Another utility, to make it easy to compare members of different enums, which is not fine with some compilers.
  87. #define ENUM_MEMBERS_EQUAL(m_a, m_b) ((int64_t)m_a == (int64_t)m_b)
  88. // This helps using a single paged allocator for many resource types.
  89. template <typename... RESOURCE_TYPES>
  90. struct VersatileResourceTemplate {
  91. static constexpr size_t RESOURCE_SIZES[] = { sizeof(RESOURCE_TYPES)... };
  92. static constexpr size_t MAX_RESOURCE_SIZE = std::max_element(RESOURCE_SIZES, RESOURCE_SIZES + sizeof...(RESOURCE_TYPES))[0];
  93. uint8_t data[MAX_RESOURCE_SIZE];
  94. template <typename T>
  95. static T *allocate(PagedAllocator<VersatileResourceTemplate> &p_allocator) {
  96. T *obj = (T *)p_allocator.alloc();
  97. memnew_placement(obj, T);
  98. return obj;
  99. }
  100. template <typename T>
  101. static void free(PagedAllocator<VersatileResourceTemplate> &p_allocator, T *p_object) {
  102. p_object->~T();
  103. p_allocator.free((VersatileResourceTemplate *)p_object);
  104. }
  105. };
  106. class RenderingDeviceDriver : public RenderingDeviceCommons {
  107. public:
  108. struct ID {
  109. size_t id = 0;
  110. _ALWAYS_INLINE_ ID() = default;
  111. _ALWAYS_INLINE_ ID(size_t p_id) :
  112. id(p_id) {}
  113. };
  114. #define DEFINE_ID(m_name) \
  115. struct m_name##ID : public ID { \
  116. _ALWAYS_INLINE_ explicit operator bool() const { return id != 0; } \
  117. _ALWAYS_INLINE_ m_name##ID &operator=(m_name##ID p_other) { \
  118. id = p_other.id; \
  119. return *this; \
  120. } \
  121. _ALWAYS_INLINE_ bool operator<(const m_name##ID &p_other) const { return id < p_other.id; } \
  122. _ALWAYS_INLINE_ bool operator==(const m_name##ID &p_other) const { return id == p_other.id; } \
  123. _ALWAYS_INLINE_ bool operator!=(const m_name##ID &p_other) const { return id != p_other.id; } \
  124. _ALWAYS_INLINE_ m_name##ID(const m_name##ID &p_other) : ID(p_other.id) {} \
  125. _ALWAYS_INLINE_ explicit m_name##ID(uint64_t p_int) : ID(p_int) {} \
  126. _ALWAYS_INLINE_ explicit m_name##ID(void *p_ptr) : ID((size_t)p_ptr) {} \
  127. _ALWAYS_INLINE_ m_name##ID() = default; \
  128. }; \
  129. /* Ensure type-punnable to pointer. Makes some things easier.*/ \
  130. static_assert(sizeof(m_name##ID) == sizeof(void *));
  131. // Id types declared before anything else to prevent cyclic dependencies between the different concerns.
  132. DEFINE_ID(Buffer);
  133. DEFINE_ID(Texture);
  134. DEFINE_ID(Sampler);
  135. DEFINE_ID(VertexFormat);
  136. DEFINE_ID(CommandQueue);
  137. DEFINE_ID(CommandQueueFamily);
  138. DEFINE_ID(CommandPool);
  139. DEFINE_ID(CommandBuffer);
  140. DEFINE_ID(SwapChain);
  141. DEFINE_ID(Framebuffer);
  142. DEFINE_ID(Shader);
  143. DEFINE_ID(UniformSet);
  144. DEFINE_ID(Pipeline);
  145. DEFINE_ID(RenderPass);
  146. DEFINE_ID(QueryPool);
  147. DEFINE_ID(Fence);
  148. DEFINE_ID(Semaphore);
  149. public:
  150. /*****************/
  151. /**** GENERIC ****/
  152. /*****************/
  153. virtual Error initialize(uint32_t p_device_index, uint32_t p_frame_count) = 0;
  154. /****************/
  155. /**** MEMORY ****/
  156. /****************/
  157. enum MemoryAllocationType {
  158. MEMORY_ALLOCATION_TYPE_CPU, // For images, CPU allocation also means linear, GPU is tiling optimal.
  159. MEMORY_ALLOCATION_TYPE_GPU,
  160. };
  161. /*****************/
  162. /**** BUFFERS ****/
  163. /*****************/
  164. enum BufferUsageBits {
  165. BUFFER_USAGE_TRANSFER_FROM_BIT = (1 << 0),
  166. BUFFER_USAGE_TRANSFER_TO_BIT = (1 << 1),
  167. BUFFER_USAGE_TEXEL_BIT = (1 << 2),
  168. BUFFER_USAGE_UNIFORM_BIT = (1 << 4),
  169. BUFFER_USAGE_STORAGE_BIT = (1 << 5),
  170. BUFFER_USAGE_INDEX_BIT = (1 << 6),
  171. BUFFER_USAGE_VERTEX_BIT = (1 << 7),
  172. BUFFER_USAGE_INDIRECT_BIT = (1 << 8),
  173. };
  174. enum {
  175. BUFFER_WHOLE_SIZE = ~0ULL
  176. };
  177. virtual BufferID buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type) = 0;
  178. // Only for a buffer with BUFFER_USAGE_TEXEL_BIT.
  179. virtual bool buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) = 0;
  180. virtual void buffer_free(BufferID p_buffer) = 0;
  181. virtual uint64_t buffer_get_allocation_size(BufferID p_buffer) = 0;
  182. virtual uint8_t *buffer_map(BufferID p_buffer) = 0;
  183. virtual void buffer_unmap(BufferID p_buffer) = 0;
  184. /*****************/
  185. /**** TEXTURE ****/
  186. /*****************/
  187. struct TextureView {
  188. DataFormat format = DATA_FORMAT_MAX;
  189. TextureSwizzle swizzle_r = TEXTURE_SWIZZLE_R;
  190. TextureSwizzle swizzle_g = TEXTURE_SWIZZLE_G;
  191. TextureSwizzle swizzle_b = TEXTURE_SWIZZLE_B;
  192. TextureSwizzle swizzle_a = TEXTURE_SWIZZLE_A;
  193. };
  194. enum TextureLayout {
  195. TEXTURE_LAYOUT_UNDEFINED,
  196. TEXTURE_LAYOUT_GENERAL,
  197. TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
  198. TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
  199. TEXTURE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
  200. TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
  201. TEXTURE_LAYOUT_TRANSFER_SRC_OPTIMAL,
  202. TEXTURE_LAYOUT_TRANSFER_DST_OPTIMAL,
  203. TEXTURE_LAYOUT_PREINITIALIZED,
  204. TEXTURE_LAYOUT_VRS_ATTACHMENT_OPTIMAL = 1000164003,
  205. };
  206. enum TextureAspect {
  207. TEXTURE_ASPECT_COLOR = 0,
  208. TEXTURE_ASPECT_DEPTH = 1,
  209. TEXTURE_ASPECT_STENCIL = 2,
  210. TEXTURE_ASPECT_MAX
  211. };
  212. enum TextureAspectBits {
  213. TEXTURE_ASPECT_COLOR_BIT = (1 << TEXTURE_ASPECT_COLOR),
  214. TEXTURE_ASPECT_DEPTH_BIT = (1 << TEXTURE_ASPECT_DEPTH),
  215. TEXTURE_ASPECT_STENCIL_BIT = (1 << TEXTURE_ASPECT_STENCIL),
  216. };
  217. struct TextureSubresource {
  218. TextureAspect aspect = TEXTURE_ASPECT_COLOR;
  219. uint32_t layer = 0;
  220. uint32_t mipmap = 0;
  221. };
  222. struct TextureSubresourceLayers {
  223. BitField<TextureAspectBits> aspect;
  224. uint32_t mipmap = 0;
  225. uint32_t base_layer = 0;
  226. uint32_t layer_count = 0;
  227. };
  228. struct TextureSubresourceRange {
  229. BitField<TextureAspectBits> aspect;
  230. uint32_t base_mipmap = 0;
  231. uint32_t mipmap_count = 0;
  232. uint32_t base_layer = 0;
  233. uint32_t layer_count = 0;
  234. };
  235. struct TextureCopyableLayout {
  236. uint64_t offset = 0;
  237. uint64_t size = 0;
  238. uint64_t row_pitch = 0;
  239. uint64_t depth_pitch = 0;
  240. uint64_t layer_pitch = 0;
  241. };
  242. virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) = 0;
  243. virtual 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) = 0;
  244. // texture_create_shared_*() can only use original, non-view textures as original. RenderingDevice is responsible for ensuring that.
  245. virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) = 0;
  246. virtual 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;
  247. virtual void texture_free(TextureID p_texture) = 0;
  248. virtual uint64_t texture_get_allocation_size(TextureID p_texture) = 0;
  249. virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) = 0;
  250. virtual uint8_t *texture_map(TextureID p_texture, const TextureSubresource &p_subresource) = 0;
  251. virtual void texture_unmap(TextureID p_texture) = 0;
  252. virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) = 0;
  253. /*****************/
  254. /**** SAMPLER ****/
  255. /*****************/
  256. virtual SamplerID sampler_create(const SamplerState &p_state) = 0;
  257. virtual void sampler_free(SamplerID p_sampler) = 0;
  258. virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) = 0;
  259. /**********************/
  260. /**** VERTEX ARRAY ****/
  261. /**********************/
  262. virtual VertexFormatID vertex_format_create(VectorView<VertexAttribute> p_vertex_attribs) = 0;
  263. virtual void vertex_format_free(VertexFormatID p_vertex_format) = 0;
  264. /******************/
  265. /**** BARRIERS ****/
  266. /******************/
  267. enum PipelineStageBits {
  268. PIPELINE_STAGE_TOP_OF_PIPE_BIT = (1 << 0),
  269. PIPELINE_STAGE_DRAW_INDIRECT_BIT = (1 << 1),
  270. PIPELINE_STAGE_VERTEX_INPUT_BIT = (1 << 2),
  271. PIPELINE_STAGE_VERTEX_SHADER_BIT = (1 << 3),
  272. PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT = (1 << 4),
  273. PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT = (1 << 5),
  274. PIPELINE_STAGE_GEOMETRY_SHADER_BIT = (1 << 6),
  275. PIPELINE_STAGE_FRAGMENT_SHADER_BIT = (1 << 7),
  276. PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT = (1 << 8),
  277. PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT = (1 << 9),
  278. PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT = (1 << 10),
  279. PIPELINE_STAGE_COMPUTE_SHADER_BIT = (1 << 11),
  280. PIPELINE_STAGE_TRANSFER_BIT = (1 << 12),
  281. PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT = (1 << 13),
  282. PIPELINE_STAGE_ALL_GRAPHICS_BIT = (1 << 15),
  283. PIPELINE_STAGE_ALL_COMMANDS_BIT = (1 << 16),
  284. };
  285. enum BarrierAccessBits {
  286. BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT = (1 << 0),
  287. BARRIER_ACCESS_INDEX_READ_BIT = (1 << 1),
  288. BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT = (1 << 2),
  289. BARRIER_ACCESS_UNIFORM_READ_BIT = (1 << 3),
  290. BARRIER_ACCESS_INPUT_ATTACHMENT_READ_BIT = (1 << 4),
  291. BARRIER_ACCESS_SHADER_READ_BIT = (1 << 5),
  292. BARRIER_ACCESS_SHADER_WRITE_BIT = (1 << 6),
  293. BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT = (1 << 7),
  294. BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT = (1 << 8),
  295. BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT = (1 << 9),
  296. BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT = (1 << 10),
  297. BARRIER_ACCESS_TRANSFER_READ_BIT = (1 << 11),
  298. BARRIER_ACCESS_TRANSFER_WRITE_BIT = (1 << 12),
  299. BARRIER_ACCESS_HOST_READ_BIT = (1 << 13),
  300. BARRIER_ACCESS_HOST_WRITE_BIT = (1 << 14),
  301. BARRIER_ACCESS_MEMORY_READ_BIT = (1 << 15),
  302. BARRIER_ACCESS_MEMORY_WRITE_BIT = (1 << 16),
  303. BARRIER_ACCESS_FRAGMENT_SHADING_RATE_ATTACHMENT_READ_BIT = (1 << 23),
  304. };
  305. struct MemoryBarrier {
  306. BitField<BarrierAccessBits> src_access;
  307. BitField<BarrierAccessBits> dst_access;
  308. };
  309. struct BufferBarrier {
  310. BufferID buffer;
  311. BitField<BarrierAccessBits> src_access;
  312. BitField<BarrierAccessBits> dst_access;
  313. uint64_t offset = 0;
  314. uint64_t size = 0;
  315. };
  316. struct TextureBarrier {
  317. TextureID texture;
  318. BitField<BarrierAccessBits> src_access;
  319. BitField<BarrierAccessBits> dst_access;
  320. TextureLayout prev_layout = TEXTURE_LAYOUT_UNDEFINED;
  321. TextureLayout next_layout = TEXTURE_LAYOUT_UNDEFINED;
  322. TextureSubresourceRange subresources;
  323. };
  324. virtual void command_pipeline_barrier(
  325. CommandBufferID p_cmd_buffer,
  326. BitField<PipelineStageBits> p_src_stages,
  327. BitField<PipelineStageBits> p_dst_stages,
  328. VectorView<MemoryBarrier> p_memory_barriers,
  329. VectorView<BufferBarrier> p_buffer_barriers,
  330. VectorView<TextureBarrier> p_texture_barriers) = 0;
  331. /****************/
  332. /**** FENCES ****/
  333. /****************/
  334. virtual FenceID fence_create() = 0;
  335. virtual Error fence_wait(FenceID p_fence) = 0;
  336. virtual void fence_free(FenceID p_fence) = 0;
  337. /********************/
  338. /**** SEMAPHORES ****/
  339. /********************/
  340. virtual SemaphoreID semaphore_create() = 0;
  341. virtual void semaphore_free(SemaphoreID p_semaphore) = 0;
  342. /*************************/
  343. /**** COMMAND BUFFERS ****/
  344. /*************************/
  345. // ----- QUEUE FAMILY -----
  346. enum CommandQueueFamilyBits {
  347. COMMAND_QUEUE_FAMILY_GRAPHICS_BIT = 0x1,
  348. COMMAND_QUEUE_FAMILY_COMPUTE_BIT = 0x2,
  349. COMMAND_QUEUE_FAMILY_TRANSFER_BIT = 0x4
  350. };
  351. // 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.
  352. // It is valid to specify no bits and a valid surface: in this case, the dedicated presentation queue family will be the preferred option.
  353. virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) = 0;
  354. // ----- QUEUE -----
  355. virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) = 0;
  356. virtual 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;
  357. virtual void command_queue_free(CommandQueueID p_cmd_queue) = 0;
  358. // ----- POOL -----
  359. enum CommandBufferType {
  360. COMMAND_BUFFER_TYPE_PRIMARY,
  361. COMMAND_BUFFER_TYPE_SECONDARY,
  362. };
  363. virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) = 0;
  364. virtual void command_pool_free(CommandPoolID p_cmd_pool) = 0;
  365. // ----- BUFFER -----
  366. virtual CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) = 0;
  367. virtual bool command_buffer_begin(CommandBufferID p_cmd_buffer) = 0;
  368. virtual bool command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) = 0;
  369. virtual void command_buffer_end(CommandBufferID p_cmd_buffer) = 0;
  370. virtual void command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) = 0;
  371. /********************/
  372. /**** SWAP CHAIN ****/
  373. /********************/
  374. // The swap chain won't be valid for use until it is resized at least once.
  375. virtual SwapChainID swap_chain_create(RenderingContextDriver::SurfaceID p_surface) = 0;
  376. // 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.
  377. virtual Error swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) = 0;
  378. // Acquire the framebuffer that can be used for drawing. This must be called only once every time a new frame will be rendered.
  379. virtual FramebufferID swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) = 0;
  380. // Retrieve the render pass that can be used to draw on the swap chain's framebuffers.
  381. virtual RenderPassID swap_chain_get_render_pass(SwapChainID p_swap_chain) = 0;
  382. // Retrieve the format used by the swap chain's framebuffers.
  383. virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) = 0;
  384. // Wait until all rendering associated to the swap chain is finished before deleting it.
  385. virtual void swap_chain_free(SwapChainID p_swap_chain) = 0;
  386. /*********************/
  387. /**** FRAMEBUFFER ****/
  388. /*********************/
  389. virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) = 0;
  390. virtual void framebuffer_free(FramebufferID p_framebuffer) = 0;
  391. /****************/
  392. /**** SHADER ****/
  393. /****************/
  394. virtual String shader_get_binary_cache_key() = 0;
  395. virtual Vector<uint8_t> shader_compile_binary_from_spirv(VectorView<ShaderStageSPIRVData> p_spirv, const String &p_shader_name) = 0;
  396. virtual ShaderID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary, ShaderDescription &r_shader_desc, String &r_name) = 0;
  397. // Only meaningful if API_TRAIT_SHADER_CHANGE_INVALIDATION is SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH.
  398. virtual uint32_t shader_get_layout_hash(ShaderID p_shader) { return 0; }
  399. virtual void shader_free(ShaderID p_shader) = 0;
  400. protected:
  401. // An optional service to implementations.
  402. Error _reflect_spirv(VectorView<ShaderStageSPIRVData> p_spirv, ShaderReflection &r_reflection);
  403. public:
  404. /*********************/
  405. /**** UNIFORM SET ****/
  406. /*********************/
  407. struct BoundUniform {
  408. UniformType type = UNIFORM_TYPE_MAX;
  409. uint32_t binding = 0xffffffff; // Binding index as specified in shader.
  410. LocalVector<ID> ids;
  411. };
  412. virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index) = 0;
  413. virtual void uniform_set_free(UniformSetID p_uniform_set) = 0;
  414. // ----- COMMANDS -----
  415. virtual void command_uniform_set_prepare_for_use(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) = 0;
  416. /******************/
  417. /**** TRANSFER ****/
  418. /******************/
  419. struct BufferCopyRegion {
  420. uint64_t src_offset = 0;
  421. uint64_t dst_offset = 0;
  422. uint64_t size = 0;
  423. };
  424. struct TextureCopyRegion {
  425. TextureSubresourceLayers src_subresources;
  426. Vector3i src_offset;
  427. TextureSubresourceLayers dst_subresources;
  428. Vector3i dst_offset;
  429. Vector3i size;
  430. };
  431. struct BufferTextureCopyRegion {
  432. uint64_t buffer_offset = 0;
  433. TextureSubresourceLayers texture_subresources;
  434. Vector3i texture_offset;
  435. Vector3i texture_region_size;
  436. };
  437. virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) = 0;
  438. virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) = 0;
  439. virtual 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;
  440. virtual 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;
  441. virtual 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;
  442. virtual 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;
  443. virtual 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;
  444. /******************/
  445. /**** PIPELINE ****/
  446. /******************/
  447. virtual void pipeline_free(PipelineID p_pipeline) = 0;
  448. // ----- BINDING -----
  449. virtual void command_bind_push_constants(CommandBufferID p_cmd_buffer, ShaderID p_shader, uint32_t p_first_index, VectorView<uint32_t> p_data) = 0;
  450. // ----- CACHE -----
  451. virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) = 0;
  452. virtual void pipeline_cache_free() = 0;
  453. virtual size_t pipeline_cache_query_size() = 0;
  454. virtual Vector<uint8_t> pipeline_cache_serialize() = 0;
  455. /*******************/
  456. /**** RENDERING ****/
  457. /*******************/
  458. // ----- SUBPASS -----
  459. enum AttachmentLoadOp {
  460. ATTACHMENT_LOAD_OP_LOAD = 0,
  461. ATTACHMENT_LOAD_OP_CLEAR = 1,
  462. ATTACHMENT_LOAD_OP_DONT_CARE = 2,
  463. };
  464. enum AttachmentStoreOp {
  465. ATTACHMENT_STORE_OP_STORE = 0,
  466. ATTACHMENT_STORE_OP_DONT_CARE = 1,
  467. };
  468. struct Attachment {
  469. DataFormat format = DATA_FORMAT_MAX;
  470. TextureSamples samples = TEXTURE_SAMPLES_MAX;
  471. AttachmentLoadOp load_op = ATTACHMENT_LOAD_OP_DONT_CARE;
  472. AttachmentStoreOp store_op = ATTACHMENT_STORE_OP_DONT_CARE;
  473. AttachmentLoadOp stencil_load_op = ATTACHMENT_LOAD_OP_DONT_CARE;
  474. AttachmentStoreOp stencil_store_op = ATTACHMENT_STORE_OP_DONT_CARE;
  475. TextureLayout initial_layout = TEXTURE_LAYOUT_UNDEFINED;
  476. TextureLayout final_layout = TEXTURE_LAYOUT_UNDEFINED;
  477. };
  478. struct AttachmentReference {
  479. static const uint32_t UNUSED = 0xffffffff;
  480. uint32_t attachment = UNUSED;
  481. TextureLayout layout = TEXTURE_LAYOUT_UNDEFINED;
  482. BitField<TextureAspectBits> aspect;
  483. };
  484. struct Subpass {
  485. LocalVector<AttachmentReference> input_references;
  486. LocalVector<AttachmentReference> color_references;
  487. AttachmentReference depth_stencil_reference;
  488. LocalVector<AttachmentReference> resolve_references;
  489. LocalVector<uint32_t> preserve_attachments;
  490. AttachmentReference vrs_reference;
  491. };
  492. struct SubpassDependency {
  493. uint32_t src_subpass = 0xffffffff;
  494. uint32_t dst_subpass = 0xffffffff;
  495. BitField<PipelineStageBits> src_stages;
  496. BitField<PipelineStageBits> dst_stages;
  497. BitField<BarrierAccessBits> src_access;
  498. BitField<BarrierAccessBits> dst_access;
  499. };
  500. virtual RenderPassID render_pass_create(VectorView<Attachment> p_attachments, VectorView<Subpass> p_subpasses, VectorView<SubpassDependency> p_subpass_dependencies, uint32_t p_view_count) = 0;
  501. virtual void render_pass_free(RenderPassID p_render_pass) = 0;
  502. // ----- COMMANDS -----
  503. union RenderPassClearValue {
  504. Color color = {};
  505. struct {
  506. float depth;
  507. uint32_t stencil;
  508. };
  509. RenderPassClearValue() {}
  510. };
  511. struct AttachmentClear {
  512. BitField<TextureAspectBits> aspect;
  513. uint32_t color_attachment = 0xffffffff;
  514. RenderPassClearValue value;
  515. };
  516. virtual 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;
  517. virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) = 0;
  518. virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) = 0;
  519. virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) = 0;
  520. virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) = 0;
  521. virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) = 0;
  522. // Binding.
  523. virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) = 0;
  524. virtual void command_bind_render_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) = 0;
  525. // Drawing.
  526. virtual 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;
  527. virtual 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;
  528. virtual 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;
  529. virtual 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;
  530. virtual 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;
  531. virtual 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;
  532. // Buffer binding.
  533. virtual 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;
  534. virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) = 0;
  535. // Dynamic state.
  536. virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) = 0;
  537. virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) = 0;
  538. // ----- PIPELINE -----
  539. virtual PipelineID render_pipeline_create(
  540. ShaderID p_shader,
  541. VertexFormatID p_vertex_format,
  542. RenderPrimitive p_render_primitive,
  543. PipelineRasterizationState p_rasterization_state,
  544. PipelineMultisampleState p_multisample_state,
  545. PipelineDepthStencilState p_depth_stencil_state,
  546. PipelineColorBlendState p_blend_state,
  547. VectorView<int32_t> p_color_attachments,
  548. BitField<PipelineDynamicStateFlags> p_dynamic_state,
  549. RenderPassID p_render_pass,
  550. uint32_t p_render_subpass,
  551. VectorView<PipelineSpecializationConstant> p_specialization_constants) = 0;
  552. /*****************/
  553. /**** COMPUTE ****/
  554. /*****************/
  555. // ----- COMMANDS -----
  556. // Binding.
  557. virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) = 0;
  558. virtual void command_bind_compute_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) = 0;
  559. // Dispatching.
  560. virtual void command_compute_dispatch(CommandBufferID p_cmd_buffer, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) = 0;
  561. virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) = 0;
  562. // ----- PIPELINE -----
  563. virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) = 0;
  564. /*****************/
  565. /**** QUERIES ****/
  566. /*****************/
  567. // ----- TIMESTAMP -----
  568. // Basic.
  569. virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) = 0;
  570. virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) = 0;
  571. virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) = 0;
  572. virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) = 0;
  573. // Commands.
  574. virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) = 0;
  575. virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) = 0;
  576. /****************/
  577. /**** LABELS ****/
  578. /****************/
  579. virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) = 0;
  580. virtual void command_end_label(CommandBufferID p_cmd_buffer) = 0;
  581. /********************/
  582. /**** SUBMISSION ****/
  583. /********************/
  584. virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) = 0;
  585. virtual void end_segment() = 0;
  586. /**************/
  587. /**** MISC ****/
  588. /**************/
  589. enum ObjectType {
  590. OBJECT_TYPE_TEXTURE,
  591. OBJECT_TYPE_SAMPLER,
  592. OBJECT_TYPE_BUFFER,
  593. OBJECT_TYPE_SHADER,
  594. OBJECT_TYPE_UNIFORM_SET,
  595. OBJECT_TYPE_PIPELINE,
  596. };
  597. struct MultiviewCapabilities {
  598. bool is_supported = false;
  599. bool geometry_shader_is_supported = false;
  600. bool tessellation_shader_is_supported = false;
  601. uint32_t max_view_count = 0;
  602. uint32_t max_instance_count = 0;
  603. };
  604. enum ApiTrait {
  605. API_TRAIT_HONORS_PIPELINE_BARRIERS,
  606. API_TRAIT_SHADER_CHANGE_INVALIDATION,
  607. API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT,
  608. API_TRAIT_TEXTURE_DATA_ROW_PITCH_STEP,
  609. API_TRAIT_SECONDARY_VIEWPORT_SCISSOR,
  610. };
  611. enum ShaderChangeInvalidation {
  612. SHADER_CHANGE_INVALIDATION_ALL_BOUND_UNIFORM_SETS,
  613. // What Vulkan does.
  614. SHADER_CHANGE_INVALIDATION_INCOMPATIBLE_SETS_PLUS_CASCADE,
  615. // What D3D12 does.
  616. SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH,
  617. };
  618. enum DeviceFamily {
  619. DEVICE_UNKNOWN,
  620. DEVICE_OPENGL,
  621. DEVICE_VULKAN,
  622. DEVICE_DIRECTX,
  623. };
  624. struct Capabilities {
  625. DeviceFamily device_family = DEVICE_UNKNOWN;
  626. uint32_t version_major = 1;
  627. uint32_t version_minor = 0;
  628. };
  629. virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) = 0;
  630. virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) = 0;
  631. virtual uint64_t get_total_memory_used() = 0;
  632. virtual uint64_t limit_get(Limit p_limit) = 0;
  633. virtual uint64_t api_trait_get(ApiTrait p_trait);
  634. virtual bool has_feature(Features p_feature) = 0;
  635. virtual const MultiviewCapabilities &get_multiview_capabilities() = 0;
  636. virtual String get_api_name() const = 0;
  637. virtual String get_api_version() const = 0;
  638. virtual String get_pipeline_cache_uuid() const = 0;
  639. virtual const Capabilities &get_capabilities() const = 0;
  640. /******************/
  641. virtual ~RenderingDeviceDriver();
  642. };
  643. using RDD = RenderingDeviceDriver;
  644. #endif // RENDERING_DEVICE_DRIVER_H