rendering_device_driver.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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, true> &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, true> &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. uint64_t id = 0;
  110. _ALWAYS_INLINE_ ID() = default;
  111. _ALWAYS_INLINE_ ID(uint64_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((uint64_t)p_ptr) {} \
  127. _ALWAYS_INLINE_ m_name##ID() = default; \
  128. };
  129. // Id types declared before anything else to prevent cyclic dependencies between the different concerns.
  130. DEFINE_ID(Buffer);
  131. DEFINE_ID(Texture);
  132. DEFINE_ID(Sampler);
  133. DEFINE_ID(VertexFormat);
  134. DEFINE_ID(CommandQueue);
  135. DEFINE_ID(CommandQueueFamily);
  136. DEFINE_ID(CommandPool);
  137. DEFINE_ID(CommandBuffer);
  138. DEFINE_ID(SwapChain);
  139. DEFINE_ID(Framebuffer);
  140. DEFINE_ID(Shader);
  141. DEFINE_ID(UniformSet);
  142. DEFINE_ID(Pipeline);
  143. DEFINE_ID(RenderPass);
  144. DEFINE_ID(QueryPool);
  145. DEFINE_ID(Fence);
  146. DEFINE_ID(Semaphore);
  147. public:
  148. /*****************/
  149. /**** GENERIC ****/
  150. /*****************/
  151. virtual Error initialize(uint32_t p_device_index, uint32_t p_frame_count) = 0;
  152. /****************/
  153. /**** MEMORY ****/
  154. /****************/
  155. enum MemoryAllocationType {
  156. MEMORY_ALLOCATION_TYPE_CPU, // For images, CPU allocation also means linear, GPU is tiling optimal.
  157. MEMORY_ALLOCATION_TYPE_GPU,
  158. };
  159. /*****************/
  160. /**** BUFFERS ****/
  161. /*****************/
  162. enum BufferUsageBits {
  163. BUFFER_USAGE_TRANSFER_FROM_BIT = (1 << 0),
  164. BUFFER_USAGE_TRANSFER_TO_BIT = (1 << 1),
  165. BUFFER_USAGE_TEXEL_BIT = (1 << 2),
  166. BUFFER_USAGE_UNIFORM_BIT = (1 << 4),
  167. BUFFER_USAGE_STORAGE_BIT = (1 << 5),
  168. BUFFER_USAGE_INDEX_BIT = (1 << 6),
  169. BUFFER_USAGE_VERTEX_BIT = (1 << 7),
  170. BUFFER_USAGE_INDIRECT_BIT = (1 << 8),
  171. };
  172. enum {
  173. BUFFER_WHOLE_SIZE = ~0ULL
  174. };
  175. virtual BufferID buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type) = 0;
  176. // Only for a buffer with BUFFER_USAGE_TEXEL_BIT.
  177. virtual bool buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) = 0;
  178. virtual void buffer_free(BufferID p_buffer) = 0;
  179. virtual uint64_t buffer_get_allocation_size(BufferID p_buffer) = 0;
  180. virtual uint8_t *buffer_map(BufferID p_buffer) = 0;
  181. virtual void buffer_unmap(BufferID p_buffer) = 0;
  182. /*****************/
  183. /**** TEXTURE ****/
  184. /*****************/
  185. struct TextureView {
  186. DataFormat format = DATA_FORMAT_MAX;
  187. TextureSwizzle swizzle_r = TEXTURE_SWIZZLE_R;
  188. TextureSwizzle swizzle_g = TEXTURE_SWIZZLE_G;
  189. TextureSwizzle swizzle_b = TEXTURE_SWIZZLE_B;
  190. TextureSwizzle swizzle_a = TEXTURE_SWIZZLE_A;
  191. };
  192. enum TextureLayout {
  193. TEXTURE_LAYOUT_UNDEFINED,
  194. TEXTURE_LAYOUT_GENERAL,
  195. TEXTURE_LAYOUT_STORAGE_OPTIMAL,
  196. TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
  197. TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
  198. TEXTURE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
  199. TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
  200. TEXTURE_LAYOUT_COPY_SRC_OPTIMAL,
  201. TEXTURE_LAYOUT_COPY_DST_OPTIMAL,
  202. TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL,
  203. TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL,
  204. TEXTURE_LAYOUT_VRS_ATTACHMENT_OPTIMAL,
  205. TEXTURE_LAYOUT_MAX
  206. };
  207. enum TextureAspect {
  208. TEXTURE_ASPECT_COLOR = 0,
  209. TEXTURE_ASPECT_DEPTH = 1,
  210. TEXTURE_ASPECT_STENCIL = 2,
  211. TEXTURE_ASPECT_MAX
  212. };
  213. enum TextureAspectBits {
  214. TEXTURE_ASPECT_COLOR_BIT = (1 << TEXTURE_ASPECT_COLOR),
  215. TEXTURE_ASPECT_DEPTH_BIT = (1 << TEXTURE_ASPECT_DEPTH),
  216. TEXTURE_ASPECT_STENCIL_BIT = (1 << TEXTURE_ASPECT_STENCIL),
  217. };
  218. struct TextureSubresource {
  219. TextureAspect aspect = TEXTURE_ASPECT_COLOR;
  220. uint32_t layer = 0;
  221. uint32_t mipmap = 0;
  222. };
  223. struct TextureSubresourceLayers {
  224. BitField<TextureAspectBits> aspect;
  225. uint32_t mipmap = 0;
  226. uint32_t base_layer = 0;
  227. uint32_t layer_count = 0;
  228. };
  229. struct TextureSubresourceRange {
  230. BitField<TextureAspectBits> aspect;
  231. uint32_t base_mipmap = 0;
  232. uint32_t mipmap_count = 0;
  233. uint32_t base_layer = 0;
  234. uint32_t layer_count = 0;
  235. };
  236. struct TextureCopyableLayout {
  237. uint64_t offset = 0;
  238. uint64_t size = 0;
  239. uint64_t row_pitch = 0;
  240. uint64_t depth_pitch = 0;
  241. uint64_t layer_pitch = 0;
  242. };
  243. virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) = 0;
  244. 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;
  245. // texture_create_shared_*() can only use original, non-view textures as original. RenderingDevice is responsible for ensuring that.
  246. virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) = 0;
  247. 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;
  248. virtual void texture_free(TextureID p_texture) = 0;
  249. virtual uint64_t texture_get_allocation_size(TextureID p_texture) = 0;
  250. virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) = 0;
  251. virtual uint8_t *texture_map(TextureID p_texture, const TextureSubresource &p_subresource) = 0;
  252. virtual void texture_unmap(TextureID p_texture) = 0;
  253. virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) = 0;
  254. virtual bool texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) = 0;
  255. /*****************/
  256. /**** SAMPLER ****/
  257. /*****************/
  258. virtual SamplerID sampler_create(const SamplerState &p_state) = 0;
  259. virtual void sampler_free(SamplerID p_sampler) = 0;
  260. virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) = 0;
  261. /**********************/
  262. /**** VERTEX ARRAY ****/
  263. /**********************/
  264. virtual VertexFormatID vertex_format_create(VectorView<VertexAttribute> p_vertex_attribs) = 0;
  265. virtual void vertex_format_free(VertexFormatID p_vertex_format) = 0;
  266. /******************/
  267. /**** BARRIERS ****/
  268. /******************/
  269. enum PipelineStageBits {
  270. PIPELINE_STAGE_TOP_OF_PIPE_BIT = (1 << 0),
  271. PIPELINE_STAGE_DRAW_INDIRECT_BIT = (1 << 1),
  272. PIPELINE_STAGE_VERTEX_INPUT_BIT = (1 << 2),
  273. PIPELINE_STAGE_VERTEX_SHADER_BIT = (1 << 3),
  274. PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT = (1 << 4),
  275. PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT = (1 << 5),
  276. PIPELINE_STAGE_GEOMETRY_SHADER_BIT = (1 << 6),
  277. PIPELINE_STAGE_FRAGMENT_SHADER_BIT = (1 << 7),
  278. PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT = (1 << 8),
  279. PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT = (1 << 9),
  280. PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT = (1 << 10),
  281. PIPELINE_STAGE_COMPUTE_SHADER_BIT = (1 << 11),
  282. PIPELINE_STAGE_COPY_BIT = (1 << 12),
  283. PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT = (1 << 13),
  284. PIPELINE_STAGE_RESOLVE_BIT = (1 << 14),
  285. PIPELINE_STAGE_ALL_GRAPHICS_BIT = (1 << 15),
  286. PIPELINE_STAGE_ALL_COMMANDS_BIT = (1 << 16),
  287. PIPELINE_STAGE_CLEAR_STORAGE_BIT = (1 << 17),
  288. };
  289. enum BarrierAccessBits {
  290. BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT = (1 << 0),
  291. BARRIER_ACCESS_INDEX_READ_BIT = (1 << 1),
  292. BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT = (1 << 2),
  293. BARRIER_ACCESS_UNIFORM_READ_BIT = (1 << 3),
  294. BARRIER_ACCESS_INPUT_ATTACHMENT_READ_BIT = (1 << 4),
  295. BARRIER_ACCESS_SHADER_READ_BIT = (1 << 5),
  296. BARRIER_ACCESS_SHADER_WRITE_BIT = (1 << 6),
  297. BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT = (1 << 7),
  298. BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT = (1 << 8),
  299. BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT = (1 << 9),
  300. BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT = (1 << 10),
  301. BARRIER_ACCESS_COPY_READ_BIT = (1 << 11),
  302. BARRIER_ACCESS_COPY_WRITE_BIT = (1 << 12),
  303. BARRIER_ACCESS_HOST_READ_BIT = (1 << 13),
  304. BARRIER_ACCESS_HOST_WRITE_BIT = (1 << 14),
  305. BARRIER_ACCESS_MEMORY_READ_BIT = (1 << 15),
  306. BARRIER_ACCESS_MEMORY_WRITE_BIT = (1 << 16),
  307. BARRIER_ACCESS_FRAGMENT_SHADING_RATE_ATTACHMENT_READ_BIT = (1 << 23),
  308. BARRIER_ACCESS_RESOLVE_READ_BIT = (1 << 24),
  309. BARRIER_ACCESS_RESOLVE_WRITE_BIT = (1 << 25),
  310. BARRIER_ACCESS_STORAGE_CLEAR_BIT = (1 << 27),
  311. };
  312. struct MemoryBarrier {
  313. BitField<BarrierAccessBits> src_access;
  314. BitField<BarrierAccessBits> dst_access;
  315. };
  316. struct BufferBarrier {
  317. BufferID buffer;
  318. BitField<BarrierAccessBits> src_access;
  319. BitField<BarrierAccessBits> dst_access;
  320. uint64_t offset = 0;
  321. uint64_t size = 0;
  322. };
  323. struct TextureBarrier {
  324. TextureID texture;
  325. BitField<BarrierAccessBits> src_access;
  326. BitField<BarrierAccessBits> dst_access;
  327. TextureLayout prev_layout = TEXTURE_LAYOUT_UNDEFINED;
  328. TextureLayout next_layout = TEXTURE_LAYOUT_UNDEFINED;
  329. TextureSubresourceRange subresources;
  330. };
  331. virtual void command_pipeline_barrier(
  332. CommandBufferID p_cmd_buffer,
  333. BitField<PipelineStageBits> p_src_stages,
  334. BitField<PipelineStageBits> p_dst_stages,
  335. VectorView<MemoryBarrier> p_memory_barriers,
  336. VectorView<BufferBarrier> p_buffer_barriers,
  337. VectorView<TextureBarrier> p_texture_barriers) = 0;
  338. /****************/
  339. /**** FENCES ****/
  340. /****************/
  341. virtual FenceID fence_create() = 0;
  342. virtual Error fence_wait(FenceID p_fence) = 0;
  343. virtual void fence_free(FenceID p_fence) = 0;
  344. /********************/
  345. /**** SEMAPHORES ****/
  346. /********************/
  347. virtual SemaphoreID semaphore_create() = 0;
  348. virtual void semaphore_free(SemaphoreID p_semaphore) = 0;
  349. /*************************/
  350. /**** COMMAND BUFFERS ****/
  351. /*************************/
  352. // ----- QUEUE FAMILY -----
  353. enum CommandQueueFamilyBits {
  354. COMMAND_QUEUE_FAMILY_GRAPHICS_BIT = 0x1,
  355. COMMAND_QUEUE_FAMILY_COMPUTE_BIT = 0x2,
  356. COMMAND_QUEUE_FAMILY_TRANSFER_BIT = 0x4
  357. };
  358. // 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.
  359. // It is valid to specify no bits and a valid surface: in this case, the dedicated presentation queue family will be the preferred option.
  360. virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) = 0;
  361. // ----- QUEUE -----
  362. virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) = 0;
  363. 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;
  364. virtual void command_queue_free(CommandQueueID p_cmd_queue) = 0;
  365. // ----- POOL -----
  366. enum CommandBufferType {
  367. COMMAND_BUFFER_TYPE_PRIMARY,
  368. COMMAND_BUFFER_TYPE_SECONDARY,
  369. };
  370. virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) = 0;
  371. virtual bool command_pool_reset(CommandPoolID p_cmd_pool) = 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 rotation in degrees to apply as a pre-transform. Usually 0 on PC. May be 0, 90, 180 & 270 on Android.
  391. virtual int swap_chain_get_pre_rotation_degrees(SwapChainID p_swap_chain) { return 0; }
  392. // Retrieve the format used by the swap chain's framebuffers.
  393. virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) = 0;
  394. // Tells the swapchain the max_fps so it can use the proper frame pacing.
  395. // Android uses this with Swappy library. Some implementations or platforms may ignore this hint.
  396. virtual void swap_chain_set_max_fps(SwapChainID p_swap_chain, int p_max_fps) {}
  397. // Wait until all rendering associated to the swap chain is finished before deleting it.
  398. virtual void swap_chain_free(SwapChainID p_swap_chain) = 0;
  399. /*********************/
  400. /**** FRAMEBUFFER ****/
  401. /*********************/
  402. virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) = 0;
  403. virtual void framebuffer_free(FramebufferID p_framebuffer) = 0;
  404. /****************/
  405. /**** SHADER ****/
  406. /****************/
  407. virtual String shader_get_binary_cache_key() = 0;
  408. virtual Vector<uint8_t> shader_compile_binary_from_spirv(VectorView<ShaderStageSPIRVData> p_spirv, const String &p_shader_name) = 0;
  409. struct ImmutableSampler {
  410. UniformType type = UNIFORM_TYPE_MAX;
  411. uint32_t binding = 0xffffffff; // Binding index as specified in shader.
  412. LocalVector<ID> ids;
  413. };
  414. /** Creates a Pipeline State Object (PSO) out of the shader and all the input data it needs.
  415. @param p_shader_binary Shader binary bytecode (e.g. SPIR-V).
  416. @param r_shader_desc TBD.
  417. @param r_name TBD.
  418. @param p_immutable_samplers Immutable samplers can be embedded when creating the pipeline layout on the condition they
  419. remain valid and unchanged, so they don't need to be specified when creating uniform sets.
  420. @return PSO resource for binding.
  421. */
  422. virtual ShaderID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary, ShaderDescription &r_shader_desc, String &r_name, const Vector<ImmutableSampler> &p_immutable_samplers) = 0;
  423. // Only meaningful if API_TRAIT_SHADER_CHANGE_INVALIDATION is SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH.
  424. virtual uint32_t shader_get_layout_hash(ShaderID p_shader) { return 0; }
  425. virtual void shader_free(ShaderID p_shader) = 0;
  426. virtual void shader_destroy_modules(ShaderID p_shader) = 0;
  427. protected:
  428. // An optional service to implementations.
  429. Error _reflect_spirv(VectorView<ShaderStageSPIRVData> p_spirv, ShaderReflection &r_reflection);
  430. public:
  431. /*********************/
  432. /**** UNIFORM SET ****/
  433. /*********************/
  434. struct BoundUniform {
  435. UniformType type = UNIFORM_TYPE_MAX;
  436. uint32_t binding = 0xffffffff; // Binding index as specified in shader.
  437. LocalVector<ID> ids;
  438. // Flag to indicate that this is an immutable sampler so it is skipped when creating uniform
  439. // sets, as it would be set previously when creating the pipeline layout.
  440. bool immutable_sampler = false;
  441. };
  442. virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index, int p_linear_pool_index) = 0;
  443. virtual void linear_uniform_set_pools_reset(int p_linear_pool_index) {}
  444. virtual void uniform_set_free(UniformSetID p_uniform_set) = 0;
  445. virtual bool uniform_sets_have_linear_pools() const { return false; }
  446. // ----- COMMANDS -----
  447. 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;
  448. /******************/
  449. /**** TRANSFER ****/
  450. /******************/
  451. struct BufferCopyRegion {
  452. uint64_t src_offset = 0;
  453. uint64_t dst_offset = 0;
  454. uint64_t size = 0;
  455. };
  456. struct TextureCopyRegion {
  457. TextureSubresourceLayers src_subresources;
  458. Vector3i src_offset;
  459. TextureSubresourceLayers dst_subresources;
  460. Vector3i dst_offset;
  461. Vector3i size;
  462. };
  463. struct BufferTextureCopyRegion {
  464. uint64_t buffer_offset = 0;
  465. TextureSubresourceLayers texture_subresources;
  466. Vector3i texture_offset;
  467. Vector3i texture_region_size;
  468. };
  469. virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) = 0;
  470. virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) = 0;
  471. 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;
  472. 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;
  473. 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;
  474. 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;
  475. 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;
  476. /******************/
  477. /**** PIPELINE ****/
  478. /******************/
  479. virtual void pipeline_free(PipelineID p_pipeline) = 0;
  480. // ----- BINDING -----
  481. virtual void command_bind_push_constants(CommandBufferID p_cmd_buffer, ShaderID p_shader, uint32_t p_first_index, VectorView<uint32_t> p_data) = 0;
  482. // ----- CACHE -----
  483. virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) = 0;
  484. virtual void pipeline_cache_free() = 0;
  485. virtual size_t pipeline_cache_query_size() = 0;
  486. virtual Vector<uint8_t> pipeline_cache_serialize() = 0;
  487. /*******************/
  488. /**** RENDERING ****/
  489. /*******************/
  490. // ----- SUBPASS -----
  491. enum AttachmentLoadOp {
  492. ATTACHMENT_LOAD_OP_LOAD = 0,
  493. ATTACHMENT_LOAD_OP_CLEAR = 1,
  494. ATTACHMENT_LOAD_OP_DONT_CARE = 2,
  495. };
  496. enum AttachmentStoreOp {
  497. ATTACHMENT_STORE_OP_STORE = 0,
  498. ATTACHMENT_STORE_OP_DONT_CARE = 1,
  499. };
  500. struct Attachment {
  501. DataFormat format = DATA_FORMAT_MAX;
  502. TextureSamples samples = TEXTURE_SAMPLES_MAX;
  503. AttachmentLoadOp load_op = ATTACHMENT_LOAD_OP_DONT_CARE;
  504. AttachmentStoreOp store_op = ATTACHMENT_STORE_OP_DONT_CARE;
  505. AttachmentLoadOp stencil_load_op = ATTACHMENT_LOAD_OP_DONT_CARE;
  506. AttachmentStoreOp stencil_store_op = ATTACHMENT_STORE_OP_DONT_CARE;
  507. TextureLayout initial_layout = TEXTURE_LAYOUT_UNDEFINED;
  508. TextureLayout final_layout = TEXTURE_LAYOUT_UNDEFINED;
  509. };
  510. struct AttachmentReference {
  511. static const uint32_t UNUSED = 0xffffffff;
  512. uint32_t attachment = UNUSED;
  513. TextureLayout layout = TEXTURE_LAYOUT_UNDEFINED;
  514. BitField<TextureAspectBits> aspect;
  515. };
  516. struct Subpass {
  517. LocalVector<AttachmentReference> input_references;
  518. LocalVector<AttachmentReference> color_references;
  519. AttachmentReference depth_stencil_reference;
  520. LocalVector<AttachmentReference> resolve_references;
  521. LocalVector<uint32_t> preserve_attachments;
  522. AttachmentReference vrs_reference;
  523. };
  524. struct SubpassDependency {
  525. uint32_t src_subpass = 0xffffffff;
  526. uint32_t dst_subpass = 0xffffffff;
  527. BitField<PipelineStageBits> src_stages;
  528. BitField<PipelineStageBits> dst_stages;
  529. BitField<BarrierAccessBits> src_access;
  530. BitField<BarrierAccessBits> dst_access;
  531. };
  532. virtual RenderPassID render_pass_create(VectorView<Attachment> p_attachments, VectorView<Subpass> p_subpasses, VectorView<SubpassDependency> p_subpass_dependencies, uint32_t p_view_count) = 0;
  533. virtual void render_pass_free(RenderPassID p_render_pass) = 0;
  534. // ----- COMMANDS -----
  535. union RenderPassClearValue {
  536. Color color = {};
  537. struct {
  538. float depth;
  539. uint32_t stencil;
  540. };
  541. RenderPassClearValue() {}
  542. };
  543. struct AttachmentClear {
  544. BitField<TextureAspectBits> aspect;
  545. uint32_t color_attachment = 0xffffffff;
  546. RenderPassClearValue value;
  547. };
  548. 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;
  549. virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) = 0;
  550. virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) = 0;
  551. virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) = 0;
  552. virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) = 0;
  553. virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) = 0;
  554. // Binding.
  555. virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) = 0;
  556. virtual void command_bind_render_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) = 0;
  557. virtual 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;
  558. // Drawing.
  559. 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;
  560. 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;
  561. 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;
  562. 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;
  563. 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;
  564. 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;
  565. // Buffer binding.
  566. 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;
  567. virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) = 0;
  568. // Dynamic state.
  569. virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) = 0;
  570. virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) = 0;
  571. // ----- PIPELINE -----
  572. virtual PipelineID render_pipeline_create(
  573. ShaderID p_shader,
  574. VertexFormatID p_vertex_format,
  575. RenderPrimitive p_render_primitive,
  576. PipelineRasterizationState p_rasterization_state,
  577. PipelineMultisampleState p_multisample_state,
  578. PipelineDepthStencilState p_depth_stencil_state,
  579. PipelineColorBlendState p_blend_state,
  580. VectorView<int32_t> p_color_attachments,
  581. BitField<PipelineDynamicStateFlags> p_dynamic_state,
  582. RenderPassID p_render_pass,
  583. uint32_t p_render_subpass,
  584. VectorView<PipelineSpecializationConstant> p_specialization_constants) = 0;
  585. /*****************/
  586. /**** COMPUTE ****/
  587. /*****************/
  588. // ----- COMMANDS -----
  589. // Binding.
  590. virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) = 0;
  591. virtual void command_bind_compute_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) = 0;
  592. virtual 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;
  593. // Dispatching.
  594. 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;
  595. virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) = 0;
  596. // ----- PIPELINE -----
  597. virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) = 0;
  598. /*****************/
  599. /**** QUERIES ****/
  600. /*****************/
  601. // ----- TIMESTAMP -----
  602. // Basic.
  603. virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) = 0;
  604. virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) = 0;
  605. virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) = 0;
  606. virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) = 0;
  607. // Commands.
  608. virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) = 0;
  609. virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) = 0;
  610. /****************/
  611. /**** LABELS ****/
  612. /****************/
  613. virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) = 0;
  614. virtual void command_end_label(CommandBufferID p_cmd_buffer) = 0;
  615. /****************/
  616. /**** DEBUG *****/
  617. /****************/
  618. virtual void command_insert_breadcrumb(CommandBufferID p_cmd_buffer, uint32_t p_data) = 0;
  619. /********************/
  620. /**** SUBMISSION ****/
  621. /********************/
  622. virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) = 0;
  623. virtual void end_segment() = 0;
  624. /**************/
  625. /**** MISC ****/
  626. /**************/
  627. enum ObjectType {
  628. OBJECT_TYPE_TEXTURE,
  629. OBJECT_TYPE_SAMPLER,
  630. OBJECT_TYPE_BUFFER,
  631. OBJECT_TYPE_SHADER,
  632. OBJECT_TYPE_UNIFORM_SET,
  633. OBJECT_TYPE_PIPELINE,
  634. };
  635. struct MultiviewCapabilities {
  636. bool is_supported = false;
  637. bool geometry_shader_is_supported = false;
  638. bool tessellation_shader_is_supported = false;
  639. uint32_t max_view_count = 0;
  640. uint32_t max_instance_count = 0;
  641. };
  642. enum ApiTrait {
  643. API_TRAIT_HONORS_PIPELINE_BARRIERS,
  644. API_TRAIT_SHADER_CHANGE_INVALIDATION,
  645. API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT,
  646. API_TRAIT_TEXTURE_DATA_ROW_PITCH_STEP,
  647. API_TRAIT_SECONDARY_VIEWPORT_SCISSOR,
  648. API_TRAIT_CLEARS_WITH_COPY_ENGINE,
  649. API_TRAIT_USE_GENERAL_IN_COPY_QUEUES,
  650. API_TRAIT_BUFFERS_REQUIRE_TRANSITIONS,
  651. };
  652. enum ShaderChangeInvalidation {
  653. SHADER_CHANGE_INVALIDATION_ALL_BOUND_UNIFORM_SETS,
  654. // What Vulkan does.
  655. SHADER_CHANGE_INVALIDATION_INCOMPATIBLE_SETS_PLUS_CASCADE,
  656. // What D3D12 does.
  657. SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH,
  658. };
  659. enum DeviceFamily {
  660. DEVICE_UNKNOWN,
  661. DEVICE_OPENGL,
  662. DEVICE_VULKAN,
  663. DEVICE_DIRECTX,
  664. DEVICE_METAL,
  665. };
  666. struct Capabilities {
  667. DeviceFamily device_family = DEVICE_UNKNOWN;
  668. uint32_t version_major = 1;
  669. uint32_t version_minor = 0;
  670. };
  671. virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) = 0;
  672. virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) = 0;
  673. virtual uint64_t get_total_memory_used() = 0;
  674. virtual uint64_t get_lazily_memory_used() = 0;
  675. virtual uint64_t limit_get(Limit p_limit) = 0;
  676. virtual uint64_t api_trait_get(ApiTrait p_trait);
  677. virtual bool has_feature(Features p_feature) = 0;
  678. virtual const MultiviewCapabilities &get_multiview_capabilities() = 0;
  679. virtual String get_api_name() const = 0;
  680. virtual String get_api_version() const = 0;
  681. virtual String get_pipeline_cache_uuid() const = 0;
  682. virtual const Capabilities &get_capabilities() const = 0;
  683. virtual bool is_composite_alpha_supported(CommandQueueID p_queue) const { return false; }
  684. /******************/
  685. virtual ~RenderingDeviceDriver();
  686. };
  687. using RDD = RenderingDeviceDriver;
  688. #endif // RENDERING_DEVICE_DRIVER_H