rendering_device_driver.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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_STORAGE_OPTIMAL,
  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_COPY_SRC_OPTIMAL,
  202. TEXTURE_LAYOUT_COPY_DST_OPTIMAL,
  203. TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL,
  204. TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL,
  205. TEXTURE_LAYOUT_VRS_ATTACHMENT_OPTIMAL,
  206. TEXTURE_LAYOUT_MAX
  207. };
  208. enum TextureAspect {
  209. TEXTURE_ASPECT_COLOR = 0,
  210. TEXTURE_ASPECT_DEPTH = 1,
  211. TEXTURE_ASPECT_STENCIL = 2,
  212. TEXTURE_ASPECT_MAX
  213. };
  214. enum TextureAspectBits {
  215. TEXTURE_ASPECT_COLOR_BIT = (1 << TEXTURE_ASPECT_COLOR),
  216. TEXTURE_ASPECT_DEPTH_BIT = (1 << TEXTURE_ASPECT_DEPTH),
  217. TEXTURE_ASPECT_STENCIL_BIT = (1 << TEXTURE_ASPECT_STENCIL),
  218. };
  219. struct TextureSubresource {
  220. TextureAspect aspect = TEXTURE_ASPECT_COLOR;
  221. uint32_t layer = 0;
  222. uint32_t mipmap = 0;
  223. };
  224. struct TextureSubresourceLayers {
  225. BitField<TextureAspectBits> aspect;
  226. uint32_t mipmap = 0;
  227. uint32_t base_layer = 0;
  228. uint32_t layer_count = 0;
  229. };
  230. struct TextureSubresourceRange {
  231. BitField<TextureAspectBits> aspect;
  232. uint32_t base_mipmap = 0;
  233. uint32_t mipmap_count = 0;
  234. uint32_t base_layer = 0;
  235. uint32_t layer_count = 0;
  236. };
  237. struct TextureCopyableLayout {
  238. uint64_t offset = 0;
  239. uint64_t size = 0;
  240. uint64_t row_pitch = 0;
  241. uint64_t depth_pitch = 0;
  242. uint64_t layer_pitch = 0;
  243. };
  244. virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) = 0;
  245. 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;
  246. // texture_create_shared_*() can only use original, non-view textures as original. RenderingDevice is responsible for ensuring that.
  247. virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) = 0;
  248. 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;
  249. virtual void texture_free(TextureID p_texture) = 0;
  250. virtual uint64_t texture_get_allocation_size(TextureID p_texture) = 0;
  251. virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) = 0;
  252. virtual uint8_t *texture_map(TextureID p_texture, const TextureSubresource &p_subresource) = 0;
  253. virtual void texture_unmap(TextureID p_texture) = 0;
  254. virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) = 0;
  255. virtual bool texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) = 0;
  256. /*****************/
  257. /**** SAMPLER ****/
  258. /*****************/
  259. virtual SamplerID sampler_create(const SamplerState &p_state) = 0;
  260. virtual void sampler_free(SamplerID p_sampler) = 0;
  261. virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) = 0;
  262. /**********************/
  263. /**** VERTEX ARRAY ****/
  264. /**********************/
  265. virtual VertexFormatID vertex_format_create(VectorView<VertexAttribute> p_vertex_attribs) = 0;
  266. virtual void vertex_format_free(VertexFormatID p_vertex_format) = 0;
  267. /******************/
  268. /**** BARRIERS ****/
  269. /******************/
  270. enum PipelineStageBits {
  271. PIPELINE_STAGE_TOP_OF_PIPE_BIT = (1 << 0),
  272. PIPELINE_STAGE_DRAW_INDIRECT_BIT = (1 << 1),
  273. PIPELINE_STAGE_VERTEX_INPUT_BIT = (1 << 2),
  274. PIPELINE_STAGE_VERTEX_SHADER_BIT = (1 << 3),
  275. PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT = (1 << 4),
  276. PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT = (1 << 5),
  277. PIPELINE_STAGE_GEOMETRY_SHADER_BIT = (1 << 6),
  278. PIPELINE_STAGE_FRAGMENT_SHADER_BIT = (1 << 7),
  279. PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT = (1 << 8),
  280. PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT = (1 << 9),
  281. PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT = (1 << 10),
  282. PIPELINE_STAGE_COMPUTE_SHADER_BIT = (1 << 11),
  283. PIPELINE_STAGE_COPY_BIT = (1 << 12),
  284. PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT = (1 << 13),
  285. PIPELINE_STAGE_RESOLVE_BIT = (1 << 14),
  286. PIPELINE_STAGE_ALL_GRAPHICS_BIT = (1 << 15),
  287. PIPELINE_STAGE_ALL_COMMANDS_BIT = (1 << 16),
  288. PIPELINE_STAGE_CLEAR_STORAGE_BIT = (1 << 17),
  289. };
  290. enum BarrierAccessBits {
  291. BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT = (1 << 0),
  292. BARRIER_ACCESS_INDEX_READ_BIT = (1 << 1),
  293. BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT = (1 << 2),
  294. BARRIER_ACCESS_UNIFORM_READ_BIT = (1 << 3),
  295. BARRIER_ACCESS_INPUT_ATTACHMENT_READ_BIT = (1 << 4),
  296. BARRIER_ACCESS_SHADER_READ_BIT = (1 << 5),
  297. BARRIER_ACCESS_SHADER_WRITE_BIT = (1 << 6),
  298. BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT = (1 << 7),
  299. BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT = (1 << 8),
  300. BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT = (1 << 9),
  301. BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT = (1 << 10),
  302. BARRIER_ACCESS_COPY_READ_BIT = (1 << 11),
  303. BARRIER_ACCESS_COPY_WRITE_BIT = (1 << 12),
  304. BARRIER_ACCESS_HOST_READ_BIT = (1 << 13),
  305. BARRIER_ACCESS_HOST_WRITE_BIT = (1 << 14),
  306. BARRIER_ACCESS_MEMORY_READ_BIT = (1 << 15),
  307. BARRIER_ACCESS_MEMORY_WRITE_BIT = (1 << 16),
  308. BARRIER_ACCESS_FRAGMENT_SHADING_RATE_ATTACHMENT_READ_BIT = (1 << 23),
  309. BARRIER_ACCESS_RESOLVE_READ_BIT = (1 << 24),
  310. BARRIER_ACCESS_RESOLVE_WRITE_BIT = (1 << 25),
  311. BARRIER_ACCESS_STORAGE_CLEAR_BIT = (1 << 27),
  312. };
  313. struct MemoryBarrier {
  314. BitField<BarrierAccessBits> src_access;
  315. BitField<BarrierAccessBits> dst_access;
  316. };
  317. struct BufferBarrier {
  318. BufferID buffer;
  319. BitField<BarrierAccessBits> src_access;
  320. BitField<BarrierAccessBits> dst_access;
  321. uint64_t offset = 0;
  322. uint64_t size = 0;
  323. };
  324. struct TextureBarrier {
  325. TextureID texture;
  326. BitField<BarrierAccessBits> src_access;
  327. BitField<BarrierAccessBits> dst_access;
  328. TextureLayout prev_layout = TEXTURE_LAYOUT_UNDEFINED;
  329. TextureLayout next_layout = TEXTURE_LAYOUT_UNDEFINED;
  330. TextureSubresourceRange subresources;
  331. };
  332. virtual void command_pipeline_barrier(
  333. CommandBufferID p_cmd_buffer,
  334. BitField<PipelineStageBits> p_src_stages,
  335. BitField<PipelineStageBits> p_dst_stages,
  336. VectorView<MemoryBarrier> p_memory_barriers,
  337. VectorView<BufferBarrier> p_buffer_barriers,
  338. VectorView<TextureBarrier> p_texture_barriers) = 0;
  339. /****************/
  340. /**** FENCES ****/
  341. /****************/
  342. virtual FenceID fence_create() = 0;
  343. virtual Error fence_wait(FenceID p_fence) = 0;
  344. virtual void fence_free(FenceID p_fence) = 0;
  345. /********************/
  346. /**** SEMAPHORES ****/
  347. /********************/
  348. virtual SemaphoreID semaphore_create() = 0;
  349. virtual void semaphore_free(SemaphoreID p_semaphore) = 0;
  350. /*************************/
  351. /**** COMMAND BUFFERS ****/
  352. /*************************/
  353. // ----- QUEUE FAMILY -----
  354. enum CommandQueueFamilyBits {
  355. COMMAND_QUEUE_FAMILY_GRAPHICS_BIT = 0x1,
  356. COMMAND_QUEUE_FAMILY_COMPUTE_BIT = 0x2,
  357. COMMAND_QUEUE_FAMILY_TRANSFER_BIT = 0x4
  358. };
  359. // 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.
  360. // It is valid to specify no bits and a valid surface: in this case, the dedicated presentation queue family will be the preferred option.
  361. virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) = 0;
  362. // ----- QUEUE -----
  363. virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) = 0;
  364. 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;
  365. virtual void command_queue_free(CommandQueueID p_cmd_queue) = 0;
  366. // ----- POOL -----
  367. enum CommandBufferType {
  368. COMMAND_BUFFER_TYPE_PRIMARY,
  369. COMMAND_BUFFER_TYPE_SECONDARY,
  370. };
  371. virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) = 0;
  372. virtual void command_pool_free(CommandPoolID p_cmd_pool) = 0;
  373. // ----- BUFFER -----
  374. virtual CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) = 0;
  375. virtual bool command_buffer_begin(CommandBufferID p_cmd_buffer) = 0;
  376. virtual bool command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) = 0;
  377. virtual void command_buffer_end(CommandBufferID p_cmd_buffer) = 0;
  378. virtual void command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) = 0;
  379. /********************/
  380. /**** SWAP CHAIN ****/
  381. /********************/
  382. // The swap chain won't be valid for use until it is resized at least once.
  383. virtual SwapChainID swap_chain_create(RenderingContextDriver::SurfaceID p_surface) = 0;
  384. // 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.
  385. virtual Error swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) = 0;
  386. // Acquire the framebuffer that can be used for drawing. This must be called only once every time a new frame will be rendered.
  387. virtual FramebufferID swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) = 0;
  388. // Retrieve the render pass that can be used to draw on the swap chain's framebuffers.
  389. virtual RenderPassID swap_chain_get_render_pass(SwapChainID p_swap_chain) = 0;
  390. // Retrieve the format used by the swap chain's framebuffers.
  391. virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) = 0;
  392. // Wait until all rendering associated to the swap chain is finished before deleting it.
  393. virtual void swap_chain_free(SwapChainID p_swap_chain) = 0;
  394. /*********************/
  395. /**** FRAMEBUFFER ****/
  396. /*********************/
  397. virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) = 0;
  398. virtual void framebuffer_free(FramebufferID p_framebuffer) = 0;
  399. /****************/
  400. /**** SHADER ****/
  401. /****************/
  402. virtual String shader_get_binary_cache_key() = 0;
  403. virtual Vector<uint8_t> shader_compile_binary_from_spirv(VectorView<ShaderStageSPIRVData> p_spirv, const String &p_shader_name) = 0;
  404. virtual ShaderID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary, ShaderDescription &r_shader_desc, String &r_name) = 0;
  405. // Only meaningful if API_TRAIT_SHADER_CHANGE_INVALIDATION is SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH.
  406. virtual uint32_t shader_get_layout_hash(ShaderID p_shader) { return 0; }
  407. virtual void shader_free(ShaderID p_shader) = 0;
  408. protected:
  409. // An optional service to implementations.
  410. Error _reflect_spirv(VectorView<ShaderStageSPIRVData> p_spirv, ShaderReflection &r_reflection);
  411. public:
  412. /*********************/
  413. /**** UNIFORM SET ****/
  414. /*********************/
  415. struct BoundUniform {
  416. UniformType type = UNIFORM_TYPE_MAX;
  417. uint32_t binding = 0xffffffff; // Binding index as specified in shader.
  418. LocalVector<ID> ids;
  419. };
  420. virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index) = 0;
  421. virtual void uniform_set_free(UniformSetID p_uniform_set) = 0;
  422. // ----- COMMANDS -----
  423. 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;
  424. /******************/
  425. /**** TRANSFER ****/
  426. /******************/
  427. struct BufferCopyRegion {
  428. uint64_t src_offset = 0;
  429. uint64_t dst_offset = 0;
  430. uint64_t size = 0;
  431. };
  432. struct TextureCopyRegion {
  433. TextureSubresourceLayers src_subresources;
  434. Vector3i src_offset;
  435. TextureSubresourceLayers dst_subresources;
  436. Vector3i dst_offset;
  437. Vector3i size;
  438. };
  439. struct BufferTextureCopyRegion {
  440. uint64_t buffer_offset = 0;
  441. TextureSubresourceLayers texture_subresources;
  442. Vector3i texture_offset;
  443. Vector3i texture_region_size;
  444. };
  445. virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) = 0;
  446. virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) = 0;
  447. 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;
  448. 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;
  449. 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;
  450. 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;
  451. 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;
  452. /******************/
  453. /**** PIPELINE ****/
  454. /******************/
  455. virtual void pipeline_free(PipelineID p_pipeline) = 0;
  456. // ----- BINDING -----
  457. virtual void command_bind_push_constants(CommandBufferID p_cmd_buffer, ShaderID p_shader, uint32_t p_first_index, VectorView<uint32_t> p_data) = 0;
  458. // ----- CACHE -----
  459. virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) = 0;
  460. virtual void pipeline_cache_free() = 0;
  461. virtual size_t pipeline_cache_query_size() = 0;
  462. virtual Vector<uint8_t> pipeline_cache_serialize() = 0;
  463. /*******************/
  464. /**** RENDERING ****/
  465. /*******************/
  466. // ----- SUBPASS -----
  467. enum AttachmentLoadOp {
  468. ATTACHMENT_LOAD_OP_LOAD = 0,
  469. ATTACHMENT_LOAD_OP_CLEAR = 1,
  470. ATTACHMENT_LOAD_OP_DONT_CARE = 2,
  471. };
  472. enum AttachmentStoreOp {
  473. ATTACHMENT_STORE_OP_STORE = 0,
  474. ATTACHMENT_STORE_OP_DONT_CARE = 1,
  475. };
  476. struct Attachment {
  477. DataFormat format = DATA_FORMAT_MAX;
  478. TextureSamples samples = TEXTURE_SAMPLES_MAX;
  479. AttachmentLoadOp load_op = ATTACHMENT_LOAD_OP_DONT_CARE;
  480. AttachmentStoreOp store_op = ATTACHMENT_STORE_OP_DONT_CARE;
  481. AttachmentLoadOp stencil_load_op = ATTACHMENT_LOAD_OP_DONT_CARE;
  482. AttachmentStoreOp stencil_store_op = ATTACHMENT_STORE_OP_DONT_CARE;
  483. TextureLayout initial_layout = TEXTURE_LAYOUT_UNDEFINED;
  484. TextureLayout final_layout = TEXTURE_LAYOUT_UNDEFINED;
  485. };
  486. struct AttachmentReference {
  487. static const uint32_t UNUSED = 0xffffffff;
  488. uint32_t attachment = UNUSED;
  489. TextureLayout layout = TEXTURE_LAYOUT_UNDEFINED;
  490. BitField<TextureAspectBits> aspect;
  491. };
  492. struct Subpass {
  493. LocalVector<AttachmentReference> input_references;
  494. LocalVector<AttachmentReference> color_references;
  495. AttachmentReference depth_stencil_reference;
  496. LocalVector<AttachmentReference> resolve_references;
  497. LocalVector<uint32_t> preserve_attachments;
  498. AttachmentReference vrs_reference;
  499. };
  500. struct SubpassDependency {
  501. uint32_t src_subpass = 0xffffffff;
  502. uint32_t dst_subpass = 0xffffffff;
  503. BitField<PipelineStageBits> src_stages;
  504. BitField<PipelineStageBits> dst_stages;
  505. BitField<BarrierAccessBits> src_access;
  506. BitField<BarrierAccessBits> dst_access;
  507. };
  508. virtual RenderPassID render_pass_create(VectorView<Attachment> p_attachments, VectorView<Subpass> p_subpasses, VectorView<SubpassDependency> p_subpass_dependencies, uint32_t p_view_count) = 0;
  509. virtual void render_pass_free(RenderPassID p_render_pass) = 0;
  510. // ----- COMMANDS -----
  511. union RenderPassClearValue {
  512. Color color = {};
  513. struct {
  514. float depth;
  515. uint32_t stencil;
  516. };
  517. RenderPassClearValue() {}
  518. };
  519. struct AttachmentClear {
  520. BitField<TextureAspectBits> aspect;
  521. uint32_t color_attachment = 0xffffffff;
  522. RenderPassClearValue value;
  523. };
  524. 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;
  525. virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) = 0;
  526. virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) = 0;
  527. virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) = 0;
  528. virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) = 0;
  529. virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) = 0;
  530. // Binding.
  531. virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) = 0;
  532. virtual void command_bind_render_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) = 0;
  533. // Drawing.
  534. 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;
  535. 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;
  536. 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;
  537. 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;
  538. 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;
  539. 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;
  540. // Buffer binding.
  541. 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;
  542. virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) = 0;
  543. // Dynamic state.
  544. virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) = 0;
  545. virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) = 0;
  546. // ----- PIPELINE -----
  547. virtual PipelineID render_pipeline_create(
  548. ShaderID p_shader,
  549. VertexFormatID p_vertex_format,
  550. RenderPrimitive p_render_primitive,
  551. PipelineRasterizationState p_rasterization_state,
  552. PipelineMultisampleState p_multisample_state,
  553. PipelineDepthStencilState p_depth_stencil_state,
  554. PipelineColorBlendState p_blend_state,
  555. VectorView<int32_t> p_color_attachments,
  556. BitField<PipelineDynamicStateFlags> p_dynamic_state,
  557. RenderPassID p_render_pass,
  558. uint32_t p_render_subpass,
  559. VectorView<PipelineSpecializationConstant> p_specialization_constants) = 0;
  560. /*****************/
  561. /**** COMPUTE ****/
  562. /*****************/
  563. // ----- COMMANDS -----
  564. // Binding.
  565. virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) = 0;
  566. virtual void command_bind_compute_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) = 0;
  567. // Dispatching.
  568. 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;
  569. virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) = 0;
  570. // ----- PIPELINE -----
  571. virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) = 0;
  572. /*****************/
  573. /**** QUERIES ****/
  574. /*****************/
  575. // ----- TIMESTAMP -----
  576. // Basic.
  577. virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) = 0;
  578. virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) = 0;
  579. virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) = 0;
  580. virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) = 0;
  581. // Commands.
  582. virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) = 0;
  583. virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) = 0;
  584. /****************/
  585. /**** LABELS ****/
  586. /****************/
  587. virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) = 0;
  588. virtual void command_end_label(CommandBufferID p_cmd_buffer) = 0;
  589. /********************/
  590. /**** SUBMISSION ****/
  591. /********************/
  592. virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) = 0;
  593. virtual void end_segment() = 0;
  594. /**************/
  595. /**** MISC ****/
  596. /**************/
  597. enum ObjectType {
  598. OBJECT_TYPE_TEXTURE,
  599. OBJECT_TYPE_SAMPLER,
  600. OBJECT_TYPE_BUFFER,
  601. OBJECT_TYPE_SHADER,
  602. OBJECT_TYPE_UNIFORM_SET,
  603. OBJECT_TYPE_PIPELINE,
  604. };
  605. struct MultiviewCapabilities {
  606. bool is_supported = false;
  607. bool geometry_shader_is_supported = false;
  608. bool tessellation_shader_is_supported = false;
  609. uint32_t max_view_count = 0;
  610. uint32_t max_instance_count = 0;
  611. };
  612. enum ApiTrait {
  613. API_TRAIT_HONORS_PIPELINE_BARRIERS,
  614. API_TRAIT_SHADER_CHANGE_INVALIDATION,
  615. API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT,
  616. API_TRAIT_TEXTURE_DATA_ROW_PITCH_STEP,
  617. API_TRAIT_SECONDARY_VIEWPORT_SCISSOR,
  618. API_TRAIT_CLEARS_WITH_COPY_ENGINE,
  619. };
  620. enum ShaderChangeInvalidation {
  621. SHADER_CHANGE_INVALIDATION_ALL_BOUND_UNIFORM_SETS,
  622. // What Vulkan does.
  623. SHADER_CHANGE_INVALIDATION_INCOMPATIBLE_SETS_PLUS_CASCADE,
  624. // What D3D12 does.
  625. SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH,
  626. };
  627. enum DeviceFamily {
  628. DEVICE_UNKNOWN,
  629. DEVICE_OPENGL,
  630. DEVICE_VULKAN,
  631. DEVICE_DIRECTX,
  632. DEVICE_METAL,
  633. };
  634. struct Capabilities {
  635. DeviceFamily device_family = DEVICE_UNKNOWN;
  636. uint32_t version_major = 1;
  637. uint32_t version_minor = 0;
  638. };
  639. virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) = 0;
  640. virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) = 0;
  641. virtual uint64_t get_total_memory_used() = 0;
  642. virtual uint64_t limit_get(Limit p_limit) = 0;
  643. virtual uint64_t api_trait_get(ApiTrait p_trait);
  644. virtual bool has_feature(Features p_feature) = 0;
  645. virtual const MultiviewCapabilities &get_multiview_capabilities() = 0;
  646. virtual String get_api_name() const = 0;
  647. virtual String get_api_version() const = 0;
  648. virtual String get_pipeline_cache_uuid() const = 0;
  649. virtual const Capabilities &get_capabilities() const = 0;
  650. virtual bool is_composite_alpha_supported(CommandQueueID p_queue) const { return false; }
  651. /******************/
  652. virtual ~RenderingDeviceDriver();
  653. };
  654. using RDD = RenderingDeviceDriver;
  655. #endif // RENDERING_DEVICE_DRIVER_H