rendering_device.h 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /*************************************************************************/
  2. /* rendering_device.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_H
  31. #define RENDERING_DEVICE_H
  32. #include "core/object/class_db.h"
  33. #include "core/variant/typed_array.h"
  34. #include "servers/display_server.h"
  35. class RDTextureFormat;
  36. class RDTextureView;
  37. class RDAttachmentFormat;
  38. class RDSamplerState;
  39. class RDVertexAttribute;
  40. class RDShaderSource;
  41. class RDShaderBytecode;
  42. class RDUniforms;
  43. class RDPipelineRasterizationState;
  44. class RDPipelineMultisampleState;
  45. class RDPipelineDepthStencilState;
  46. class RDPipelineColorBlendState;
  47. class RenderingDevice : public Object {
  48. GDCLASS(RenderingDevice, Object)
  49. public:
  50. enum DeviceFamily {
  51. DEVICE_UNKNOWN,
  52. DEVICE_OPENGL,
  53. DEVICE_VULKAN,
  54. DEVICE_DIRECTX
  55. };
  56. enum ShaderStage {
  57. SHADER_STAGE_VERTEX,
  58. SHADER_STAGE_FRAGMENT,
  59. SHADER_STAGE_TESSELATION_CONTROL,
  60. SHADER_STAGE_TESSELATION_EVALUATION,
  61. SHADER_STAGE_COMPUTE,
  62. SHADER_STAGE_MAX,
  63. SHADER_STAGE_VERTEX_BIT = (1 << SHADER_STAGE_VERTEX),
  64. SHADER_STAGE_FRAGMENT_BIT = (1 << SHADER_STAGE_FRAGMENT),
  65. SHADER_STAGE_TESSELATION_CONTROL_BIT = (1 << SHADER_STAGE_TESSELATION_CONTROL),
  66. SHADER_STAGE_TESSELATION_EVALUATION_BIT = (1 << SHADER_STAGE_TESSELATION_EVALUATION),
  67. SHADER_STAGE_COMPUTE_BIT = (1 << SHADER_STAGE_COMPUTE),
  68. };
  69. enum ShaderLanguage {
  70. SHADER_LANGUAGE_GLSL,
  71. SHADER_LANGUAGE_HLSL
  72. };
  73. enum SubgroupOperations {
  74. SUBGROUP_BASIC_BIT = 1,
  75. SUBGROUP_VOTE_BIT = 2,
  76. SUBGROUP_ARITHMETIC_BIT = 4,
  77. SUBGROUP_BALLOT_BIT = 8,
  78. SUBGROUP_SHUFFLE_BIT = 16,
  79. SUBGROUP_SHUFFLE_RELATIVE_BIT = 32,
  80. SUBGROUP_CLUSTERED_BIT = 64,
  81. SUBGROUP_QUAD_BIT = 128,
  82. };
  83. struct Capabilities {
  84. // main device info
  85. DeviceFamily device_family = DEVICE_UNKNOWN;
  86. uint32_t version_major = 1.0;
  87. uint32_t version_minor = 0.0;
  88. // subgroup capabilities
  89. uint32_t subgroup_size = 0;
  90. uint32_t subgroup_in_shaders = 0; // Set flags using SHADER_STAGE_VERTEX_BIT, SHADER_STAGE_FRAGMENT_BIT, etc.
  91. uint32_t subgroup_operations = 0; // Set flags, using SubgroupOperations
  92. };
  93. typedef Vector<uint8_t> (*ShaderCompileFunction)(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language, String *r_error, const Capabilities *p_capabilities);
  94. typedef Vector<uint8_t> (*ShaderCacheFunction)(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language);
  95. private:
  96. static ShaderCompileFunction compile_function;
  97. static ShaderCacheFunction cache_function;
  98. static RenderingDevice *singleton;
  99. protected:
  100. static void _bind_methods();
  101. Capabilities device_capabilities;
  102. public:
  103. //base numeric ID for all types
  104. enum {
  105. INVALID_ID = -1,
  106. INVALID_FORMAT_ID = -1
  107. };
  108. /*****************/
  109. /**** GENERIC ****/
  110. /*****************/
  111. enum CompareOperator {
  112. COMPARE_OP_NEVER,
  113. COMPARE_OP_LESS,
  114. COMPARE_OP_EQUAL,
  115. COMPARE_OP_LESS_OR_EQUAL,
  116. COMPARE_OP_GREATER,
  117. COMPARE_OP_NOT_EQUAL,
  118. COMPARE_OP_GREATER_OR_EQUAL,
  119. COMPARE_OP_ALWAYS,
  120. COMPARE_OP_MAX //not an actual operator, just the amount of operators :D
  121. };
  122. enum DataFormat {
  123. DATA_FORMAT_R4G4_UNORM_PACK8,
  124. DATA_FORMAT_R4G4B4A4_UNORM_PACK16,
  125. DATA_FORMAT_B4G4R4A4_UNORM_PACK16,
  126. DATA_FORMAT_R5G6B5_UNORM_PACK16,
  127. DATA_FORMAT_B5G6R5_UNORM_PACK16,
  128. DATA_FORMAT_R5G5B5A1_UNORM_PACK16,
  129. DATA_FORMAT_B5G5R5A1_UNORM_PACK16,
  130. DATA_FORMAT_A1R5G5B5_UNORM_PACK16,
  131. DATA_FORMAT_R8_UNORM,
  132. DATA_FORMAT_R8_SNORM,
  133. DATA_FORMAT_R8_USCALED,
  134. DATA_FORMAT_R8_SSCALED,
  135. DATA_FORMAT_R8_UINT,
  136. DATA_FORMAT_R8_SINT,
  137. DATA_FORMAT_R8_SRGB,
  138. DATA_FORMAT_R8G8_UNORM,
  139. DATA_FORMAT_R8G8_SNORM,
  140. DATA_FORMAT_R8G8_USCALED,
  141. DATA_FORMAT_R8G8_SSCALED,
  142. DATA_FORMAT_R8G8_UINT,
  143. DATA_FORMAT_R8G8_SINT,
  144. DATA_FORMAT_R8G8_SRGB,
  145. DATA_FORMAT_R8G8B8_UNORM,
  146. DATA_FORMAT_R8G8B8_SNORM,
  147. DATA_FORMAT_R8G8B8_USCALED,
  148. DATA_FORMAT_R8G8B8_SSCALED,
  149. DATA_FORMAT_R8G8B8_UINT,
  150. DATA_FORMAT_R8G8B8_SINT,
  151. DATA_FORMAT_R8G8B8_SRGB,
  152. DATA_FORMAT_B8G8R8_UNORM,
  153. DATA_FORMAT_B8G8R8_SNORM,
  154. DATA_FORMAT_B8G8R8_USCALED,
  155. DATA_FORMAT_B8G8R8_SSCALED,
  156. DATA_FORMAT_B8G8R8_UINT,
  157. DATA_FORMAT_B8G8R8_SINT,
  158. DATA_FORMAT_B8G8R8_SRGB,
  159. DATA_FORMAT_R8G8B8A8_UNORM,
  160. DATA_FORMAT_R8G8B8A8_SNORM,
  161. DATA_FORMAT_R8G8B8A8_USCALED,
  162. DATA_FORMAT_R8G8B8A8_SSCALED,
  163. DATA_FORMAT_R8G8B8A8_UINT,
  164. DATA_FORMAT_R8G8B8A8_SINT,
  165. DATA_FORMAT_R8G8B8A8_SRGB,
  166. DATA_FORMAT_B8G8R8A8_UNORM,
  167. DATA_FORMAT_B8G8R8A8_SNORM,
  168. DATA_FORMAT_B8G8R8A8_USCALED,
  169. DATA_FORMAT_B8G8R8A8_SSCALED,
  170. DATA_FORMAT_B8G8R8A8_UINT,
  171. DATA_FORMAT_B8G8R8A8_SINT,
  172. DATA_FORMAT_B8G8R8A8_SRGB,
  173. DATA_FORMAT_A8B8G8R8_UNORM_PACK32,
  174. DATA_FORMAT_A8B8G8R8_SNORM_PACK32,
  175. DATA_FORMAT_A8B8G8R8_USCALED_PACK32,
  176. DATA_FORMAT_A8B8G8R8_SSCALED_PACK32,
  177. DATA_FORMAT_A8B8G8R8_UINT_PACK32,
  178. DATA_FORMAT_A8B8G8R8_SINT_PACK32,
  179. DATA_FORMAT_A8B8G8R8_SRGB_PACK32,
  180. DATA_FORMAT_A2R10G10B10_UNORM_PACK32,
  181. DATA_FORMAT_A2R10G10B10_SNORM_PACK32,
  182. DATA_FORMAT_A2R10G10B10_USCALED_PACK32,
  183. DATA_FORMAT_A2R10G10B10_SSCALED_PACK32,
  184. DATA_FORMAT_A2R10G10B10_UINT_PACK32,
  185. DATA_FORMAT_A2R10G10B10_SINT_PACK32,
  186. DATA_FORMAT_A2B10G10R10_UNORM_PACK32,
  187. DATA_FORMAT_A2B10G10R10_SNORM_PACK32,
  188. DATA_FORMAT_A2B10G10R10_USCALED_PACK32,
  189. DATA_FORMAT_A2B10G10R10_SSCALED_PACK32,
  190. DATA_FORMAT_A2B10G10R10_UINT_PACK32,
  191. DATA_FORMAT_A2B10G10R10_SINT_PACK32,
  192. DATA_FORMAT_R16_UNORM,
  193. DATA_FORMAT_R16_SNORM,
  194. DATA_FORMAT_R16_USCALED,
  195. DATA_FORMAT_R16_SSCALED,
  196. DATA_FORMAT_R16_UINT,
  197. DATA_FORMAT_R16_SINT,
  198. DATA_FORMAT_R16_SFLOAT,
  199. DATA_FORMAT_R16G16_UNORM,
  200. DATA_FORMAT_R16G16_SNORM,
  201. DATA_FORMAT_R16G16_USCALED,
  202. DATA_FORMAT_R16G16_SSCALED,
  203. DATA_FORMAT_R16G16_UINT,
  204. DATA_FORMAT_R16G16_SINT,
  205. DATA_FORMAT_R16G16_SFLOAT,
  206. DATA_FORMAT_R16G16B16_UNORM,
  207. DATA_FORMAT_R16G16B16_SNORM,
  208. DATA_FORMAT_R16G16B16_USCALED,
  209. DATA_FORMAT_R16G16B16_SSCALED,
  210. DATA_FORMAT_R16G16B16_UINT,
  211. DATA_FORMAT_R16G16B16_SINT,
  212. DATA_FORMAT_R16G16B16_SFLOAT,
  213. DATA_FORMAT_R16G16B16A16_UNORM,
  214. DATA_FORMAT_R16G16B16A16_SNORM,
  215. DATA_FORMAT_R16G16B16A16_USCALED,
  216. DATA_FORMAT_R16G16B16A16_SSCALED,
  217. DATA_FORMAT_R16G16B16A16_UINT,
  218. DATA_FORMAT_R16G16B16A16_SINT,
  219. DATA_FORMAT_R16G16B16A16_SFLOAT,
  220. DATA_FORMAT_R32_UINT,
  221. DATA_FORMAT_R32_SINT,
  222. DATA_FORMAT_R32_SFLOAT,
  223. DATA_FORMAT_R32G32_UINT,
  224. DATA_FORMAT_R32G32_SINT,
  225. DATA_FORMAT_R32G32_SFLOAT,
  226. DATA_FORMAT_R32G32B32_UINT,
  227. DATA_FORMAT_R32G32B32_SINT,
  228. DATA_FORMAT_R32G32B32_SFLOAT,
  229. DATA_FORMAT_R32G32B32A32_UINT,
  230. DATA_FORMAT_R32G32B32A32_SINT,
  231. DATA_FORMAT_R32G32B32A32_SFLOAT,
  232. DATA_FORMAT_R64_UINT,
  233. DATA_FORMAT_R64_SINT,
  234. DATA_FORMAT_R64_SFLOAT,
  235. DATA_FORMAT_R64G64_UINT,
  236. DATA_FORMAT_R64G64_SINT,
  237. DATA_FORMAT_R64G64_SFLOAT,
  238. DATA_FORMAT_R64G64B64_UINT,
  239. DATA_FORMAT_R64G64B64_SINT,
  240. DATA_FORMAT_R64G64B64_SFLOAT,
  241. DATA_FORMAT_R64G64B64A64_UINT,
  242. DATA_FORMAT_R64G64B64A64_SINT,
  243. DATA_FORMAT_R64G64B64A64_SFLOAT,
  244. DATA_FORMAT_B10G11R11_UFLOAT_PACK32,
  245. DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32,
  246. DATA_FORMAT_D16_UNORM,
  247. DATA_FORMAT_X8_D24_UNORM_PACK32,
  248. DATA_FORMAT_D32_SFLOAT,
  249. DATA_FORMAT_S8_UINT,
  250. DATA_FORMAT_D16_UNORM_S8_UINT,
  251. DATA_FORMAT_D24_UNORM_S8_UINT,
  252. DATA_FORMAT_D32_SFLOAT_S8_UINT,
  253. DATA_FORMAT_BC1_RGB_UNORM_BLOCK,
  254. DATA_FORMAT_BC1_RGB_SRGB_BLOCK,
  255. DATA_FORMAT_BC1_RGBA_UNORM_BLOCK,
  256. DATA_FORMAT_BC1_RGBA_SRGB_BLOCK,
  257. DATA_FORMAT_BC2_UNORM_BLOCK,
  258. DATA_FORMAT_BC2_SRGB_BLOCK,
  259. DATA_FORMAT_BC3_UNORM_BLOCK,
  260. DATA_FORMAT_BC3_SRGB_BLOCK,
  261. DATA_FORMAT_BC4_UNORM_BLOCK,
  262. DATA_FORMAT_BC4_SNORM_BLOCK,
  263. DATA_FORMAT_BC5_UNORM_BLOCK,
  264. DATA_FORMAT_BC5_SNORM_BLOCK,
  265. DATA_FORMAT_BC6H_UFLOAT_BLOCK,
  266. DATA_FORMAT_BC6H_SFLOAT_BLOCK,
  267. DATA_FORMAT_BC7_UNORM_BLOCK,
  268. DATA_FORMAT_BC7_SRGB_BLOCK,
  269. DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK,
  270. DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK,
  271. DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK,
  272. DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK,
  273. DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK,
  274. DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK,
  275. DATA_FORMAT_EAC_R11_UNORM_BLOCK,
  276. DATA_FORMAT_EAC_R11_SNORM_BLOCK,
  277. DATA_FORMAT_EAC_R11G11_UNORM_BLOCK,
  278. DATA_FORMAT_EAC_R11G11_SNORM_BLOCK,
  279. DATA_FORMAT_ASTC_4x4_UNORM_BLOCK,
  280. DATA_FORMAT_ASTC_4x4_SRGB_BLOCK,
  281. DATA_FORMAT_ASTC_5x4_UNORM_BLOCK,
  282. DATA_FORMAT_ASTC_5x4_SRGB_BLOCK,
  283. DATA_FORMAT_ASTC_5x5_UNORM_BLOCK,
  284. DATA_FORMAT_ASTC_5x5_SRGB_BLOCK,
  285. DATA_FORMAT_ASTC_6x5_UNORM_BLOCK,
  286. DATA_FORMAT_ASTC_6x5_SRGB_BLOCK,
  287. DATA_FORMAT_ASTC_6x6_UNORM_BLOCK,
  288. DATA_FORMAT_ASTC_6x6_SRGB_BLOCK,
  289. DATA_FORMAT_ASTC_8x5_UNORM_BLOCK,
  290. DATA_FORMAT_ASTC_8x5_SRGB_BLOCK,
  291. DATA_FORMAT_ASTC_8x6_UNORM_BLOCK,
  292. DATA_FORMAT_ASTC_8x6_SRGB_BLOCK,
  293. DATA_FORMAT_ASTC_8x8_UNORM_BLOCK,
  294. DATA_FORMAT_ASTC_8x8_SRGB_BLOCK,
  295. DATA_FORMAT_ASTC_10x5_UNORM_BLOCK,
  296. DATA_FORMAT_ASTC_10x5_SRGB_BLOCK,
  297. DATA_FORMAT_ASTC_10x6_UNORM_BLOCK,
  298. DATA_FORMAT_ASTC_10x6_SRGB_BLOCK,
  299. DATA_FORMAT_ASTC_10x8_UNORM_BLOCK,
  300. DATA_FORMAT_ASTC_10x8_SRGB_BLOCK,
  301. DATA_FORMAT_ASTC_10x10_UNORM_BLOCK,
  302. DATA_FORMAT_ASTC_10x10_SRGB_BLOCK,
  303. DATA_FORMAT_ASTC_12x10_UNORM_BLOCK,
  304. DATA_FORMAT_ASTC_12x10_SRGB_BLOCK,
  305. DATA_FORMAT_ASTC_12x12_UNORM_BLOCK,
  306. DATA_FORMAT_ASTC_12x12_SRGB_BLOCK,
  307. DATA_FORMAT_G8B8G8R8_422_UNORM,
  308. DATA_FORMAT_B8G8R8G8_422_UNORM,
  309. DATA_FORMAT_G8_B8_R8_3PLANE_420_UNORM,
  310. DATA_FORMAT_G8_B8R8_2PLANE_420_UNORM,
  311. DATA_FORMAT_G8_B8_R8_3PLANE_422_UNORM,
  312. DATA_FORMAT_G8_B8R8_2PLANE_422_UNORM,
  313. DATA_FORMAT_G8_B8_R8_3PLANE_444_UNORM,
  314. DATA_FORMAT_R10X6_UNORM_PACK16,
  315. DATA_FORMAT_R10X6G10X6_UNORM_2PACK16,
  316. DATA_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
  317. DATA_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16,
  318. DATA_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16,
  319. DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16,
  320. DATA_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16,
  321. DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16,
  322. DATA_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16,
  323. DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16,
  324. DATA_FORMAT_R12X4_UNORM_PACK16,
  325. DATA_FORMAT_R12X4G12X4_UNORM_2PACK16,
  326. DATA_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16,
  327. DATA_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16,
  328. DATA_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16,
  329. DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16,
  330. DATA_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16,
  331. DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16,
  332. DATA_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16,
  333. DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16,
  334. DATA_FORMAT_G16B16G16R16_422_UNORM,
  335. DATA_FORMAT_B16G16R16G16_422_UNORM,
  336. DATA_FORMAT_G16_B16_R16_3PLANE_420_UNORM,
  337. DATA_FORMAT_G16_B16R16_2PLANE_420_UNORM,
  338. DATA_FORMAT_G16_B16_R16_3PLANE_422_UNORM,
  339. DATA_FORMAT_G16_B16R16_2PLANE_422_UNORM,
  340. DATA_FORMAT_G16_B16_R16_3PLANE_444_UNORM,
  341. DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG,
  342. DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG,
  343. DATA_FORMAT_PVRTC2_2BPP_UNORM_BLOCK_IMG,
  344. DATA_FORMAT_PVRTC2_4BPP_UNORM_BLOCK_IMG,
  345. DATA_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG,
  346. DATA_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG,
  347. DATA_FORMAT_PVRTC2_2BPP_SRGB_BLOCK_IMG,
  348. DATA_FORMAT_PVRTC2_4BPP_SRGB_BLOCK_IMG,
  349. DATA_FORMAT_MAX
  350. };
  351. /*****************/
  352. /**** BARRIER ****/
  353. /*****************/
  354. enum BarrierMask {
  355. BARRIER_MASK_RASTER = 1,
  356. BARRIER_MASK_COMPUTE = 2,
  357. BARRIER_MASK_TRANSFER = 4,
  358. BARRIER_MASK_NO_BARRIER = 8,
  359. BARRIER_MASK_ALL = BARRIER_MASK_RASTER | BARRIER_MASK_COMPUTE | BARRIER_MASK_TRANSFER
  360. };
  361. /*****************/
  362. /**** TEXTURE ****/
  363. /*****************/
  364. enum TextureType {
  365. TEXTURE_TYPE_1D,
  366. TEXTURE_TYPE_2D,
  367. TEXTURE_TYPE_3D,
  368. TEXTURE_TYPE_CUBE,
  369. TEXTURE_TYPE_1D_ARRAY,
  370. TEXTURE_TYPE_2D_ARRAY,
  371. TEXTURE_TYPE_CUBE_ARRAY,
  372. TEXTURE_TYPE_MAX
  373. };
  374. enum TextureSamples {
  375. TEXTURE_SAMPLES_1,
  376. TEXTURE_SAMPLES_2,
  377. TEXTURE_SAMPLES_4,
  378. TEXTURE_SAMPLES_8,
  379. TEXTURE_SAMPLES_16,
  380. TEXTURE_SAMPLES_32,
  381. TEXTURE_SAMPLES_64,
  382. TEXTURE_SAMPLES_MAX
  383. };
  384. enum TextureUsageBits {
  385. TEXTURE_USAGE_SAMPLING_BIT = (1 << 0),
  386. TEXTURE_USAGE_COLOR_ATTACHMENT_BIT = (1 << 1),
  387. TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT = (1 << 2),
  388. TEXTURE_USAGE_STORAGE_BIT = (1 << 3),
  389. TEXTURE_USAGE_STORAGE_ATOMIC_BIT = (1 << 4),
  390. TEXTURE_USAGE_CPU_READ_BIT = (1 << 5),
  391. TEXTURE_USAGE_CAN_UPDATE_BIT = (1 << 6),
  392. TEXTURE_USAGE_CAN_COPY_FROM_BIT = (1 << 7),
  393. TEXTURE_USAGE_CAN_COPY_TO_BIT = (1 << 8),
  394. TEXTURE_USAGE_RESOLVE_ATTACHMENT_BIT = (1 << 9),
  395. };
  396. enum TextureSwizzle {
  397. TEXTURE_SWIZZLE_IDENTITY,
  398. TEXTURE_SWIZZLE_ZERO,
  399. TEXTURE_SWIZZLE_ONE,
  400. TEXTURE_SWIZZLE_R,
  401. TEXTURE_SWIZZLE_G,
  402. TEXTURE_SWIZZLE_B,
  403. TEXTURE_SWIZZLE_A,
  404. TEXTURE_SWIZZLE_MAX
  405. };
  406. struct TextureFormat {
  407. DataFormat format;
  408. uint32_t width;
  409. uint32_t height;
  410. uint32_t depth;
  411. uint32_t array_layers;
  412. uint32_t mipmaps;
  413. TextureType texture_type;
  414. TextureSamples samples;
  415. uint32_t usage_bits;
  416. Vector<DataFormat> shareable_formats;
  417. TextureFormat() {
  418. format = DATA_FORMAT_R8_UNORM;
  419. width = 1;
  420. height = 1;
  421. depth = 1;
  422. array_layers = 1;
  423. mipmaps = 1;
  424. texture_type = TEXTURE_TYPE_2D;
  425. samples = TEXTURE_SAMPLES_1;
  426. usage_bits = 0;
  427. }
  428. };
  429. struct TextureView {
  430. DataFormat format_override;
  431. TextureSwizzle swizzle_r;
  432. TextureSwizzle swizzle_g;
  433. TextureSwizzle swizzle_b;
  434. TextureSwizzle swizzle_a;
  435. TextureView() {
  436. format_override = DATA_FORMAT_MAX; //means, use same as format
  437. swizzle_r = TEXTURE_SWIZZLE_R;
  438. swizzle_g = TEXTURE_SWIZZLE_G;
  439. swizzle_b = TEXTURE_SWIZZLE_B;
  440. swizzle_a = TEXTURE_SWIZZLE_A;
  441. }
  442. };
  443. virtual RID texture_create(const TextureFormat &p_format, const TextureView &p_view, const Vector<Vector<uint8_t>> &p_data = Vector<Vector<uint8_t>>()) = 0;
  444. virtual RID texture_create_shared(const TextureView &p_view, RID p_with_texture) = 0;
  445. enum TextureSliceType {
  446. TEXTURE_SLICE_2D,
  447. TEXTURE_SLICE_CUBEMAP,
  448. TEXTURE_SLICE_3D,
  449. TEXTURE_SLICE_2D_ARRAY,
  450. };
  451. virtual RID texture_create_shared_from_slice(const TextureView &p_view, RID p_with_texture, uint32_t p_layer, uint32_t p_mipmap, TextureSliceType p_slice_type = TEXTURE_SLICE_2D) = 0;
  452. virtual Error texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, uint32_t p_post_barrier = BARRIER_MASK_ALL) = 0;
  453. virtual Vector<uint8_t> texture_get_data(RID p_texture, uint32_t p_layer) = 0; // CPU textures will return immediately, while GPU textures will most likely force a flush
  454. virtual bool texture_is_format_supported_for_usage(DataFormat p_format, uint32_t p_usage) const = 0;
  455. virtual bool texture_is_shared(RID p_texture) = 0;
  456. virtual bool texture_is_valid(RID p_texture) = 0;
  457. virtual Error texture_copy(RID p_from_texture, RID p_to_texture, const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_size, uint32_t p_src_mipmap, uint32_t p_dst_mipmap, uint32_t p_src_layer, uint32_t p_dst_layer, uint32_t p_post_barrier = BARRIER_MASK_ALL) = 0;
  458. virtual Error texture_clear(RID p_texture, const Color &p_color, uint32_t p_base_mipmap, uint32_t p_mipmaps, uint32_t p_base_layer, uint32_t p_layers, uint32_t p_post_barrier = BARRIER_MASK_ALL) = 0;
  459. virtual Error texture_resolve_multisample(RID p_from_texture, RID p_to_texture, uint32_t p_post_barrier = BARRIER_MASK_ALL) = 0;
  460. /*********************/
  461. /**** FRAMEBUFFER ****/
  462. /*********************/
  463. struct AttachmentFormat {
  464. DataFormat format;
  465. TextureSamples samples;
  466. uint32_t usage_flags;
  467. AttachmentFormat() {
  468. format = DATA_FORMAT_R8G8B8A8_UNORM;
  469. samples = TEXTURE_SAMPLES_1;
  470. usage_flags = 0;
  471. }
  472. };
  473. typedef int64_t FramebufferFormatID;
  474. // This ID is warranted to be unique for the same formats, does not need to be freed
  475. virtual FramebufferFormatID framebuffer_format_create(const Vector<AttachmentFormat> &p_format) = 0;
  476. virtual FramebufferFormatID framebuffer_format_create_empty(TextureSamples p_samples = TEXTURE_SAMPLES_1) = 0;
  477. virtual TextureSamples framebuffer_format_get_texture_samples(FramebufferFormatID p_format) = 0;
  478. virtual RID framebuffer_create(const Vector<RID> &p_texture_attachments, FramebufferFormatID p_format_check = INVALID_ID) = 0;
  479. virtual RID framebuffer_create_empty(const Size2i &p_size, TextureSamples p_samples = TEXTURE_SAMPLES_1, FramebufferFormatID p_format_check = INVALID_ID) = 0;
  480. virtual FramebufferFormatID framebuffer_get_format(RID p_framebuffer) = 0;
  481. /*****************/
  482. /**** SAMPLER ****/
  483. /*****************/
  484. enum SamplerFilter {
  485. SAMPLER_FILTER_NEAREST,
  486. SAMPLER_FILTER_LINEAR,
  487. };
  488. enum SamplerRepeatMode {
  489. SAMPLER_REPEAT_MODE_REPEAT,
  490. SAMPLER_REPEAT_MODE_MIRRORED_REPEAT,
  491. SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE,
  492. SAMPLER_REPEAT_MODE_CLAMP_TO_BORDER,
  493. SAMPLER_REPEAT_MODE_MIRROR_CLAMP_TO_EDGE,
  494. SAMPLER_REPEAT_MODE_MAX
  495. };
  496. enum SamplerBorderColor {
  497. SAMPLER_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK,
  498. SAMPLER_BORDER_COLOR_INT_TRANSPARENT_BLACK,
  499. SAMPLER_BORDER_COLOR_FLOAT_OPAQUE_BLACK,
  500. SAMPLER_BORDER_COLOR_INT_OPAQUE_BLACK,
  501. SAMPLER_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
  502. SAMPLER_BORDER_COLOR_INT_OPAQUE_WHITE,
  503. SAMPLER_BORDER_COLOR_MAX
  504. };
  505. struct SamplerState {
  506. SamplerFilter mag_filter;
  507. SamplerFilter min_filter;
  508. SamplerFilter mip_filter;
  509. SamplerRepeatMode repeat_u;
  510. SamplerRepeatMode repeat_v;
  511. SamplerRepeatMode repeat_w;
  512. float lod_bias;
  513. bool use_anisotropy;
  514. float anisotropy_max;
  515. bool enable_compare;
  516. CompareOperator compare_op;
  517. float min_lod;
  518. float max_lod;
  519. SamplerBorderColor border_color;
  520. bool unnormalized_uvw;
  521. SamplerState() {
  522. mag_filter = SAMPLER_FILTER_NEAREST;
  523. min_filter = SAMPLER_FILTER_NEAREST;
  524. mip_filter = SAMPLER_FILTER_NEAREST;
  525. repeat_u = SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  526. repeat_v = SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  527. repeat_w = SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  528. lod_bias = 0;
  529. use_anisotropy = false;
  530. anisotropy_max = 1.0;
  531. enable_compare = false;
  532. compare_op = COMPARE_OP_ALWAYS;
  533. min_lod = 0;
  534. max_lod = 1e20; //something very large should do
  535. border_color = SAMPLER_BORDER_COLOR_FLOAT_OPAQUE_BLACK;
  536. unnormalized_uvw = false;
  537. }
  538. };
  539. virtual RID sampler_create(const SamplerState &p_state) = 0;
  540. /**********************/
  541. /**** VERTEX ARRAY ****/
  542. /**********************/
  543. enum VertexFrequency {
  544. VERTEX_FREQUENCY_VERTEX,
  545. VERTEX_FREQUENCY_INSTANCE,
  546. };
  547. struct VertexAttribute {
  548. uint32_t location; //shader location
  549. uint32_t offset;
  550. DataFormat format;
  551. uint32_t stride;
  552. VertexFrequency frequency;
  553. VertexAttribute() {
  554. location = 0;
  555. offset = 0;
  556. stride = 0;
  557. format = DATA_FORMAT_MAX;
  558. frequency = VERTEX_FREQUENCY_VERTEX;
  559. }
  560. };
  561. virtual RID vertex_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>(), bool p_use_as_storage = false) = 0;
  562. typedef int64_t VertexFormatID;
  563. // This ID is warranted to be unique for the same formats, does not need to be freed
  564. virtual VertexFormatID vertex_format_create(const Vector<VertexAttribute> &p_vertex_formats) = 0;
  565. virtual RID vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const Vector<RID> &p_src_buffers) = 0;
  566. enum IndexBufferFormat {
  567. INDEX_BUFFER_FORMAT_UINT16,
  568. INDEX_BUFFER_FORMAT_UINT32,
  569. };
  570. virtual RID index_buffer_create(uint32_t p_size_indices, IndexBufferFormat p_format, const Vector<uint8_t> &p_data = Vector<uint8_t>(), bool p_use_restart_indices = false) = 0;
  571. virtual RID index_array_create(RID p_index_buffer, uint32_t p_index_offset, uint32_t p_index_count) = 0;
  572. /****************/
  573. /**** SHADER ****/
  574. /****************/
  575. const Capabilities *get_device_capabilities() const { return &device_capabilities; };
  576. virtual Vector<uint8_t> shader_compile_from_source(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language = SHADER_LANGUAGE_GLSL, String *r_error = nullptr, bool p_allow_cache = true);
  577. static void shader_set_compile_function(ShaderCompileFunction p_function);
  578. static void shader_set_cache_function(ShaderCacheFunction p_function);
  579. struct ShaderStageData {
  580. ShaderStage shader_stage;
  581. Vector<uint8_t> spir_v;
  582. ShaderStageData() {
  583. shader_stage = SHADER_STAGE_VERTEX;
  584. }
  585. };
  586. RID shader_create_from_bytecode(const Ref<RDShaderBytecode> &p_bytecode);
  587. virtual RID shader_create(const Vector<ShaderStageData> &p_stages) = 0;
  588. virtual uint32_t shader_get_vertex_input_attribute_mask(RID p_shader) = 0;
  589. /******************/
  590. /**** UNIFORMS ****/
  591. /******************/
  592. enum UniformType {
  593. UNIFORM_TYPE_SAMPLER, //for sampling only (sampler GLSL type)
  594. UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, // for sampling only, but includes a texture, (samplerXX GLSL type), first a sampler then a texture
  595. UNIFORM_TYPE_TEXTURE, //only texture, (textureXX GLSL type)
  596. UNIFORM_TYPE_IMAGE, // storage image (imageXX GLSL type), for compute mostly
  597. UNIFORM_TYPE_TEXTURE_BUFFER, // buffer texture (or TBO, textureBuffer type)
  598. UNIFORM_TYPE_SAMPLER_WITH_TEXTURE_BUFFER, // buffer texture with a sampler(or TBO, samplerBuffer type)
  599. UNIFORM_TYPE_IMAGE_BUFFER, //texel buffer, (imageBuffer type), for compute mostly
  600. UNIFORM_TYPE_UNIFORM_BUFFER, //regular uniform buffer (or UBO).
  601. UNIFORM_TYPE_STORAGE_BUFFER, //storage buffer ("buffer" qualifier) like UBO, but supports storage, for compute mostly
  602. UNIFORM_TYPE_INPUT_ATTACHMENT, //used for sub-pass read/write, for mobile mostly
  603. UNIFORM_TYPE_MAX
  604. };
  605. enum StorageBufferUsage {
  606. STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT = 1
  607. };
  608. virtual RID uniform_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>()) = 0;
  609. virtual RID storage_buffer_create(uint32_t p_size, const Vector<uint8_t> &p_data = Vector<uint8_t>(), uint32_t p_usage = 0) = 0;
  610. virtual RID texture_buffer_create(uint32_t p_size_elements, DataFormat p_format, const Vector<uint8_t> &p_data = Vector<uint8_t>()) = 0;
  611. struct Uniform {
  612. UniformType uniform_type;
  613. int binding; //binding index as specified in shader
  614. //for single items, provide one ID, for
  615. //multiple items (declared as arrays in shader),
  616. //provide more
  617. //for sampler with texture, supply two IDs for each.
  618. //accepted IDs are: Sampler, Texture, Uniform Buffer and Texture Buffer
  619. Vector<RID> ids;
  620. Uniform() {
  621. uniform_type = UNIFORM_TYPE_IMAGE;
  622. binding = 0;
  623. }
  624. };
  625. virtual RID uniform_set_create(const Vector<Uniform> &p_uniforms, RID p_shader, uint32_t p_shader_set) = 0;
  626. virtual bool uniform_set_is_valid(RID p_uniform_set) = 0;
  627. virtual Error buffer_update(RID p_buffer, uint32_t p_offset, uint32_t p_size, const void *p_data, uint32_t p_post_barrier = BARRIER_MASK_ALL) = 0;
  628. virtual Error buffer_clear(RID p_buffer, uint32_t p_offset, uint32_t p_size, uint32_t p_post_barrier = BARRIER_MASK_ALL) = 0;
  629. virtual Vector<uint8_t> buffer_get_data(RID p_buffer) = 0; //this causes stall, only use to retrieve large buffers for saving
  630. /*************************/
  631. /**** RENDER PIPELINE ****/
  632. /*************************/
  633. enum RenderPrimitive {
  634. RENDER_PRIMITIVE_POINTS,
  635. RENDER_PRIMITIVE_LINES,
  636. RENDER_PRIMITIVE_LINES_WITH_ADJACENCY,
  637. RENDER_PRIMITIVE_LINESTRIPS,
  638. RENDER_PRIMITIVE_LINESTRIPS_WITH_ADJACENCY,
  639. RENDER_PRIMITIVE_TRIANGLES,
  640. RENDER_PRIMITIVE_TRIANGLES_WITH_ADJACENCY,
  641. RENDER_PRIMITIVE_TRIANGLE_STRIPS,
  642. RENDER_PRIMITIVE_TRIANGLE_STRIPS_WITH_AJACENCY,
  643. RENDER_PRIMITIVE_TRIANGLE_STRIPS_WITH_RESTART_INDEX,
  644. RENDER_PRIMITIVE_TESSELATION_PATCH,
  645. RENDER_PRIMITIVE_MAX
  646. };
  647. //disable optimization, tessellate control points
  648. enum PolygonCullMode {
  649. POLYGON_CULL_DISABLED,
  650. POLYGON_CULL_FRONT,
  651. POLYGON_CULL_BACK,
  652. };
  653. enum PolygonFrontFace {
  654. POLYGON_FRONT_FACE_CLOCKWISE,
  655. POLYGON_FRONT_FACE_COUNTER_CLOCKWISE,
  656. };
  657. enum StencilOperation {
  658. STENCIL_OP_KEEP,
  659. STENCIL_OP_ZERO,
  660. STENCIL_OP_REPLACE,
  661. STENCIL_OP_INCREMENT_AND_CLAMP,
  662. STENCIL_OP_DECREMENT_AND_CLAMP,
  663. STENCIL_OP_INVERT,
  664. STENCIL_OP_INCREMENT_AND_WRAP,
  665. STENCIL_OP_DECREMENT_AND_WRAP,
  666. STENCIL_OP_MAX //not an actual operator, just the amount of operators :D
  667. };
  668. enum LogicOperation {
  669. LOGIC_OP_CLEAR,
  670. LOGIC_OP_AND,
  671. LOGIC_OP_AND_REVERSE,
  672. LOGIC_OP_COPY,
  673. LOGIC_OP_AND_INVERTED,
  674. LOGIC_OP_NO_OP,
  675. LOGIC_OP_XOR,
  676. LOGIC_OP_OR,
  677. LOGIC_OP_NOR,
  678. LOGIC_OP_EQUIVALENT,
  679. LOGIC_OP_INVERT,
  680. LOGIC_OP_OR_REVERSE,
  681. LOGIC_OP_COPY_INVERTED,
  682. LOGIC_OP_OR_INVERTED,
  683. LOGIC_OP_NAND,
  684. LOGIC_OP_SET,
  685. LOGIC_OP_MAX //not an actual operator, just the amount of operators :D
  686. };
  687. enum BlendFactor {
  688. BLEND_FACTOR_ZERO,
  689. BLEND_FACTOR_ONE,
  690. BLEND_FACTOR_SRC_COLOR,
  691. BLEND_FACTOR_ONE_MINUS_SRC_COLOR,
  692. BLEND_FACTOR_DST_COLOR,
  693. BLEND_FACTOR_ONE_MINUS_DST_COLOR,
  694. BLEND_FACTOR_SRC_ALPHA,
  695. BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
  696. BLEND_FACTOR_DST_ALPHA,
  697. BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
  698. BLEND_FACTOR_CONSTANT_COLOR,
  699. BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
  700. BLEND_FACTOR_CONSTANT_ALPHA,
  701. BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA,
  702. BLEND_FACTOR_SRC_ALPHA_SATURATE,
  703. BLEND_FACTOR_SRC1_COLOR,
  704. BLEND_FACTOR_ONE_MINUS_SRC1_COLOR,
  705. BLEND_FACTOR_SRC1_ALPHA,
  706. BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA,
  707. BLEND_FACTOR_MAX
  708. };
  709. enum BlendOperation {
  710. BLEND_OP_ADD,
  711. BLEND_OP_SUBTRACT,
  712. BLEND_OP_REVERSE_SUBTRACT,
  713. BLEND_OP_MINIMUM,
  714. BLEND_OP_MAXIMUM, //yes this one is an actual operator
  715. BLEND_OP_MAX //not an actual operator, just the amount of operators :D
  716. };
  717. struct PipelineRasterizationState {
  718. bool enable_depth_clamp;
  719. bool discard_primitives;
  720. bool wireframe;
  721. PolygonCullMode cull_mode;
  722. PolygonFrontFace front_face;
  723. bool depth_bias_enable;
  724. float depth_bias_constant_factor;
  725. float depth_bias_clamp;
  726. float depth_bias_slope_factor;
  727. float line_width;
  728. uint32_t patch_control_points;
  729. PipelineRasterizationState() {
  730. enable_depth_clamp = false;
  731. discard_primitives = false;
  732. wireframe = false;
  733. cull_mode = POLYGON_CULL_DISABLED;
  734. front_face = POLYGON_FRONT_FACE_CLOCKWISE;
  735. depth_bias_enable = false;
  736. depth_bias_constant_factor = 0;
  737. depth_bias_clamp = 0;
  738. depth_bias_slope_factor = 0;
  739. line_width = 1.0;
  740. patch_control_points = 1;
  741. }
  742. };
  743. struct PipelineMultisampleState {
  744. TextureSamples sample_count;
  745. bool enable_sample_shading;
  746. float min_sample_shading;
  747. Vector<uint32_t> sample_mask;
  748. bool enable_alpha_to_coverage;
  749. bool enable_alpha_to_one;
  750. PipelineMultisampleState() {
  751. sample_count = TEXTURE_SAMPLES_1;
  752. enable_sample_shading = false;
  753. min_sample_shading = 0;
  754. enable_alpha_to_coverage = false;
  755. enable_alpha_to_one = false;
  756. }
  757. };
  758. struct PipelineDepthStencilState {
  759. bool enable_depth_test;
  760. bool enable_depth_write;
  761. CompareOperator depth_compare_operator;
  762. bool enable_depth_range;
  763. float depth_range_min;
  764. float depth_range_max;
  765. bool enable_stencil;
  766. struct StencilOperationState {
  767. StencilOperation fail;
  768. StencilOperation pass;
  769. StencilOperation depth_fail;
  770. CompareOperator compare;
  771. uint32_t compare_mask;
  772. uint32_t write_mask;
  773. uint32_t reference;
  774. StencilOperationState() {
  775. fail = STENCIL_OP_ZERO;
  776. pass = STENCIL_OP_ZERO;
  777. depth_fail = STENCIL_OP_ZERO;
  778. compare = COMPARE_OP_ALWAYS;
  779. compare_mask = 0;
  780. write_mask = 0;
  781. reference = 0;
  782. }
  783. };
  784. StencilOperationState front_op;
  785. StencilOperationState back_op;
  786. PipelineDepthStencilState() {
  787. enable_depth_test = false;
  788. enable_depth_write = false;
  789. depth_compare_operator = COMPARE_OP_ALWAYS;
  790. enable_depth_range = false;
  791. depth_range_min = 0;
  792. depth_range_max = 0;
  793. enable_stencil = false;
  794. }
  795. };
  796. struct PipelineColorBlendState {
  797. bool enable_logic_op;
  798. LogicOperation logic_op;
  799. struct Attachment {
  800. bool enable_blend;
  801. BlendFactor src_color_blend_factor;
  802. BlendFactor dst_color_blend_factor;
  803. BlendOperation color_blend_op;
  804. BlendFactor src_alpha_blend_factor;
  805. BlendFactor dst_alpha_blend_factor;
  806. BlendOperation alpha_blend_op;
  807. bool write_r;
  808. bool write_g;
  809. bool write_b;
  810. bool write_a;
  811. Attachment() {
  812. enable_blend = false;
  813. src_color_blend_factor = BLEND_FACTOR_ZERO;
  814. dst_color_blend_factor = BLEND_FACTOR_ZERO;
  815. color_blend_op = BLEND_OP_ADD;
  816. src_alpha_blend_factor = BLEND_FACTOR_ZERO;
  817. dst_alpha_blend_factor = BLEND_FACTOR_ZERO;
  818. alpha_blend_op = BLEND_OP_ADD;
  819. write_r = true;
  820. write_g = true;
  821. write_b = true;
  822. write_a = true;
  823. }
  824. };
  825. static PipelineColorBlendState create_disabled(int p_attachments = 1) {
  826. PipelineColorBlendState bs;
  827. for (int i = 0; i < p_attachments; i++) {
  828. bs.attachments.push_back(Attachment());
  829. }
  830. return bs;
  831. }
  832. static PipelineColorBlendState create_blend(int p_attachments = 1) {
  833. PipelineColorBlendState bs;
  834. for (int i = 0; i < p_attachments; i++) {
  835. Attachment ba;
  836. ba.enable_blend = true;
  837. ba.src_color_blend_factor = BLEND_FACTOR_SRC_ALPHA;
  838. ba.dst_color_blend_factor = BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  839. ba.src_alpha_blend_factor = BLEND_FACTOR_SRC_ALPHA;
  840. ba.dst_alpha_blend_factor = BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  841. bs.attachments.push_back(ba);
  842. }
  843. return bs;
  844. }
  845. Vector<Attachment> attachments; //one per render target texture
  846. Color blend_constant;
  847. PipelineColorBlendState() {
  848. enable_logic_op = false;
  849. logic_op = LOGIC_OP_CLEAR;
  850. }
  851. };
  852. enum PipelineDynamicStateFlags {
  853. DYNAMIC_STATE_LINE_WIDTH = (1 << 0),
  854. DYNAMIC_STATE_DEPTH_BIAS = (1 << 1),
  855. DYNAMIC_STATE_BLEND_CONSTANTS = (1 << 2),
  856. DYNAMIC_STATE_DEPTH_BOUNDS = (1 << 3),
  857. DYNAMIC_STATE_STENCIL_COMPARE_MASK = (1 << 4),
  858. DYNAMIC_STATE_STENCIL_WRITE_MASK = (1 << 5),
  859. DYNAMIC_STATE_STENCIL_REFERENCE = (1 << 6),
  860. };
  861. virtual bool render_pipeline_is_valid(RID p_pipeline) = 0;
  862. virtual RID render_pipeline_create(RID p_shader, FramebufferFormatID p_framebuffer_format, VertexFormatID p_vertex_format, RenderPrimitive p_render_primitive, const PipelineRasterizationState &p_rasterization_state, const PipelineMultisampleState &p_multisample_state, const PipelineDepthStencilState &p_depth_stencil_state, const PipelineColorBlendState &p_blend_state, int p_dynamic_state_flags = 0) = 0;
  863. /**************************/
  864. /**** COMPUTE PIPELINE ****/
  865. /**************************/
  866. virtual RID compute_pipeline_create(RID p_shader) = 0;
  867. virtual bool compute_pipeline_is_valid(RID p_pipeline) = 0;
  868. /****************/
  869. /**** SCREEN ****/
  870. /****************/
  871. virtual int screen_get_width(DisplayServer::WindowID p_screen = 0) const = 0;
  872. virtual int screen_get_height(DisplayServer::WindowID p_screen = 0) const = 0;
  873. virtual FramebufferFormatID screen_get_framebuffer_format() const = 0;
  874. /********************/
  875. /**** DRAW LISTS ****/
  876. /********************/
  877. enum InitialAction {
  878. INITIAL_ACTION_CLEAR, //start rendering and clear the whole framebuffer (region or not) (supply params)
  879. INITIAL_ACTION_CLEAR_REGION, //start rendering and clear the framebuffer in the specified region (supply params)
  880. INITIAL_ACTION_CLEAR_REGION_CONTINUE, //countinue rendering and clear the framebuffer in the specified region (supply params)
  881. INITIAL_ACTION_KEEP, //start rendering, but keep attached color texture contents (depth will be cleared)
  882. INITIAL_ACTION_DROP, //start rendering, ignore what is there, just write above it
  883. INITIAL_ACTION_CONTINUE, //continue rendering (framebuffer must have been left in "continue" state as final action previously)
  884. INITIAL_ACTION_MAX
  885. };
  886. enum FinalAction {
  887. FINAL_ACTION_READ, //will no longer render to it, allows attached textures to be read again, but depth buffer contents will be dropped (Can't be read from)
  888. FINAL_ACTION_DISCARD, // discard contents after rendering
  889. FINAL_ACTION_CONTINUE, //will continue rendering later, attached textures can't be read until re-bound with "finish"
  890. FINAL_ACTION_MAX
  891. };
  892. typedef int64_t DrawListID;
  893. virtual DrawListID draw_list_begin_for_screen(DisplayServer::WindowID p_screen = 0, const Color &p_clear_color = Color()) = 0;
  894. virtual DrawListID draw_list_begin(RID p_framebuffer, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values = Vector<Color>(), float p_clear_depth = 1.0, uint32_t p_clear_stencil = 0, const Rect2 &p_region = Rect2(), const Vector<RID> &p_storage_textures = Vector<RID>()) = 0;
  895. virtual Error draw_list_begin_split(RID p_framebuffer, uint32_t p_splits, DrawListID *r_split_ids, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values = Vector<Color>(), float p_clear_depth = 1.0, uint32_t p_clear_stencil = 0, const Rect2 &p_region = Rect2(), const Vector<RID> &p_storage_textures = Vector<RID>()) = 0;
  896. virtual void draw_list_bind_render_pipeline(DrawListID p_list, RID p_render_pipeline) = 0;
  897. virtual void draw_list_bind_uniform_set(DrawListID p_list, RID p_uniform_set, uint32_t p_index) = 0;
  898. virtual void draw_list_bind_vertex_array(DrawListID p_list, RID p_vertex_array) = 0;
  899. virtual void draw_list_bind_index_array(DrawListID p_list, RID p_index_array) = 0;
  900. virtual void draw_list_set_line_width(DrawListID p_list, float p_width) = 0;
  901. virtual void draw_list_set_push_constant(DrawListID p_list, const void *p_data, uint32_t p_data_size) = 0;
  902. virtual void draw_list_draw(DrawListID p_list, bool p_use_indices, uint32_t p_instances = 1, uint32_t p_procedural_vertices = 0) = 0;
  903. virtual void draw_list_enable_scissor(DrawListID p_list, const Rect2 &p_rect) = 0;
  904. virtual void draw_list_disable_scissor(DrawListID p_list) = 0;
  905. virtual void draw_list_end(uint32_t p_post_barrier = BARRIER_MASK_ALL) = 0;
  906. /***********************/
  907. /**** COMPUTE LISTS ****/
  908. /***********************/
  909. typedef int64_t ComputeListID;
  910. virtual ComputeListID compute_list_begin(bool p_allow_draw_overlap = false) = 0;
  911. virtual void compute_list_bind_compute_pipeline(ComputeListID p_list, RID p_compute_pipeline) = 0;
  912. virtual void compute_list_bind_uniform_set(ComputeListID p_list, RID p_uniform_set, uint32_t p_index) = 0;
  913. virtual void compute_list_set_push_constant(ComputeListID p_list, const void *p_data, uint32_t p_data_size) = 0;
  914. virtual void compute_list_dispatch(ComputeListID p_list, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) = 0;
  915. virtual void compute_list_dispatch_threads(ComputeListID p_list, uint32_t p_x_threads, uint32_t p_y_threads, uint32_t p_z_threads) = 0;
  916. virtual void compute_list_dispatch_indirect(ComputeListID p_list, RID p_buffer, uint32_t p_offset) = 0;
  917. virtual void compute_list_add_barrier(ComputeListID p_list) = 0;
  918. virtual void compute_list_end(uint32_t p_post_barrier = BARRIER_MASK_ALL) = 0;
  919. virtual void barrier(uint32_t p_from = BARRIER_MASK_ALL, uint32_t p_to = BARRIER_MASK_ALL) = 0;
  920. virtual void full_barrier() = 0;
  921. /***************/
  922. /**** FREE! ****/
  923. /***************/
  924. virtual void free(RID p_id) = 0;
  925. /****************/
  926. /**** Timing ****/
  927. /****************/
  928. virtual void capture_timestamp(const String &p_name) = 0;
  929. virtual uint32_t get_captured_timestamps_count() const = 0;
  930. virtual uint64_t get_captured_timestamps_frame() const = 0;
  931. virtual uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const = 0;
  932. virtual uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const = 0;
  933. virtual String get_captured_timestamp_name(uint32_t p_index) const = 0;
  934. /****************/
  935. /**** LIMITS ****/
  936. /****************/
  937. enum Limit {
  938. LIMIT_MAX_BOUND_UNIFORM_SETS,
  939. LIMIT_MAX_FRAMEBUFFER_COLOR_ATTACHMENTS,
  940. LIMIT_MAX_TEXTURES_PER_UNIFORM_SET,
  941. LIMIT_MAX_SAMPLERS_PER_UNIFORM_SET,
  942. LIMIT_MAX_STORAGE_BUFFERS_PER_UNIFORM_SET,
  943. LIMIT_MAX_STORAGE_IMAGES_PER_UNIFORM_SET,
  944. LIMIT_MAX_UNIFORM_BUFFERS_PER_UNIFORM_SET,
  945. LIMIT_MAX_DRAW_INDEXED_INDEX,
  946. LIMIT_MAX_FRAMEBUFFER_HEIGHT,
  947. LIMIT_MAX_FRAMEBUFFER_WIDTH,
  948. LIMIT_MAX_TEXTURE_ARRAY_LAYERS,
  949. LIMIT_MAX_TEXTURE_SIZE_1D,
  950. LIMIT_MAX_TEXTURE_SIZE_2D,
  951. LIMIT_MAX_TEXTURE_SIZE_3D,
  952. LIMIT_MAX_TEXTURE_SIZE_CUBE,
  953. LIMIT_MAX_TEXTURES_PER_SHADER_STAGE,
  954. LIMIT_MAX_SAMPLERS_PER_SHADER_STAGE,
  955. LIMIT_MAX_STORAGE_BUFFERS_PER_SHADER_STAGE,
  956. LIMIT_MAX_STORAGE_IMAGES_PER_SHADER_STAGE,
  957. LIMIT_MAX_UNIFORM_BUFFERS_PER_SHADER_STAGE,
  958. LIMIT_MAX_PUSH_CONSTANT_SIZE,
  959. LIMIT_MAX_UNIFORM_BUFFER_SIZE,
  960. LIMIT_MAX_VERTEX_INPUT_ATTRIBUTE_OFFSET,
  961. LIMIT_MAX_VERTEX_INPUT_ATTRIBUTES,
  962. LIMIT_MAX_VERTEX_INPUT_BINDINGS,
  963. LIMIT_MAX_VERTEX_INPUT_BINDING_STRIDE,
  964. LIMIT_MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT,
  965. LIMIT_MAX_COMPUTE_SHARED_MEMORY_SIZE,
  966. LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X,
  967. LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Y,
  968. LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Z,
  969. LIMIT_MAX_COMPUTE_WORKGROUP_INVOCATIONS,
  970. LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_X,
  971. LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_Y,
  972. LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_Z,
  973. };
  974. virtual int limit_get(Limit p_limit) = 0;
  975. //methods below not exposed, used by RenderingDeviceRD
  976. virtual void prepare_screen_for_drawing() = 0;
  977. virtual void swap_buffers() = 0;
  978. virtual uint32_t get_frame_delay() const = 0;
  979. virtual void submit() = 0;
  980. virtual void sync() = 0;
  981. virtual uint64_t get_memory_usage() const = 0;
  982. virtual RenderingDevice *create_local_device() = 0;
  983. virtual void set_resource_name(RID p_id, const String p_name) = 0;
  984. virtual void draw_command_begin_label(String p_label_name, const Color p_color = Color(1, 1, 1, 1)) = 0;
  985. virtual void draw_command_insert_label(String p_label_name, const Color p_color = Color(1, 1, 1, 1)) = 0;
  986. virtual void draw_command_end_label() = 0;
  987. virtual String get_device_vendor_name() const = 0;
  988. virtual String get_device_name() const = 0;
  989. virtual String get_device_pipeline_cache_uuid() const = 0;
  990. static RenderingDevice *get_singleton();
  991. RenderingDevice();
  992. protected:
  993. //binders to script API
  994. RID _texture_create(const Ref<RDTextureFormat> &p_format, const Ref<RDTextureView> &p_view, const TypedArray<PackedByteArray> &p_data = Array());
  995. RID _texture_create_shared(const Ref<RDTextureView> &p_view, RID p_with_texture);
  996. RID _texture_create_shared_from_slice(const Ref<RDTextureView> &p_view, RID p_with_texture, uint32_t p_layer, uint32_t p_mipmap, TextureSliceType p_slice_type = TEXTURE_SLICE_2D);
  997. FramebufferFormatID _framebuffer_format_create(const TypedArray<RDAttachmentFormat> &p_attachments);
  998. RID _framebuffer_create(const Array &p_textures, FramebufferFormatID p_format_check = INVALID_ID);
  999. RID _sampler_create(const Ref<RDSamplerState> &p_state);
  1000. VertexFormatID _vertex_format_create(const TypedArray<RDVertexAttribute> &p_vertex_formats);
  1001. RID _vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const TypedArray<RID> &p_src_buffers);
  1002. Ref<RDShaderBytecode> _shader_compile_from_source(const Ref<RDShaderSource> &p_source, bool p_allow_cache = true);
  1003. RID _uniform_set_create(const Array &p_uniforms, RID p_shader, uint32_t p_shader_set);
  1004. Error _buffer_update(RID p_buffer, uint32_t p_offset, uint32_t p_size, const Vector<uint8_t> &p_data, uint32_t p_post_barrier = BARRIER_MASK_ALL);
  1005. RID _render_pipeline_create(RID p_shader, FramebufferFormatID p_framebuffer_format, VertexFormatID p_vertex_format, RenderPrimitive p_render_primitive, const Ref<RDPipelineRasterizationState> &p_rasterization_state, const Ref<RDPipelineMultisampleState> &p_multisample_state, const Ref<RDPipelineDepthStencilState> &p_depth_stencil_state, const Ref<RDPipelineColorBlendState> &p_blend_state, int p_dynamic_state_flags = 0);
  1006. Vector<int64_t> _draw_list_begin_split(RID p_framebuffer, uint32_t p_splits, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values = Vector<Color>(), float p_clear_depth = 1.0, uint32_t p_clear_stencil = 0, const Rect2 &p_region = Rect2(), const TypedArray<RID> &p_storage_textures = TypedArray<RID>());
  1007. void _draw_list_set_push_constant(DrawListID p_list, const Vector<uint8_t> &p_data, uint32_t p_data_size);
  1008. void _compute_list_set_push_constant(ComputeListID p_list, const Vector<uint8_t> &p_data, uint32_t p_data_size);
  1009. };
  1010. VARIANT_ENUM_CAST(RenderingDevice::ShaderStage)
  1011. VARIANT_ENUM_CAST(RenderingDevice::ShaderLanguage)
  1012. VARIANT_ENUM_CAST(RenderingDevice::CompareOperator)
  1013. VARIANT_ENUM_CAST(RenderingDevice::DataFormat)
  1014. VARIANT_ENUM_CAST(RenderingDevice::TextureType)
  1015. VARIANT_ENUM_CAST(RenderingDevice::TextureSamples)
  1016. VARIANT_ENUM_CAST(RenderingDevice::TextureUsageBits)
  1017. VARIANT_ENUM_CAST(RenderingDevice::TextureSwizzle)
  1018. VARIANT_ENUM_CAST(RenderingDevice::TextureSliceType)
  1019. VARIANT_ENUM_CAST(RenderingDevice::SamplerFilter)
  1020. VARIANT_ENUM_CAST(RenderingDevice::SamplerRepeatMode)
  1021. VARIANT_ENUM_CAST(RenderingDevice::SamplerBorderColor)
  1022. VARIANT_ENUM_CAST(RenderingDevice::VertexFrequency)
  1023. VARIANT_ENUM_CAST(RenderingDevice::IndexBufferFormat)
  1024. VARIANT_ENUM_CAST(RenderingDevice::StorageBufferUsage)
  1025. VARIANT_ENUM_CAST(RenderingDevice::UniformType)
  1026. VARIANT_ENUM_CAST(RenderingDevice::RenderPrimitive)
  1027. VARIANT_ENUM_CAST(RenderingDevice::PolygonCullMode)
  1028. VARIANT_ENUM_CAST(RenderingDevice::PolygonFrontFace)
  1029. VARIANT_ENUM_CAST(RenderingDevice::StencilOperation)
  1030. VARIANT_ENUM_CAST(RenderingDevice::LogicOperation)
  1031. VARIANT_ENUM_CAST(RenderingDevice::BlendFactor)
  1032. VARIANT_ENUM_CAST(RenderingDevice::BlendOperation)
  1033. VARIANT_ENUM_CAST(RenderingDevice::PipelineDynamicStateFlags)
  1034. VARIANT_ENUM_CAST(RenderingDevice::InitialAction)
  1035. VARIANT_ENUM_CAST(RenderingDevice::FinalAction)
  1036. VARIANT_ENUM_CAST(RenderingDevice::Limit)
  1037. typedef RenderingDevice RD;
  1038. #endif // RENDERING_DEVICE_H