rendering_device_binds.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /**************************************************************************/
  2. /* rendering_device_binds.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_BINDS_H
  31. #define RENDERING_DEVICE_BINDS_H
  32. #include "servers/rendering/rendering_device.h"
  33. #define RD_SETGET(m_type, m_member) \
  34. void set_##m_member(m_type p_##m_member) { base.m_member = p_##m_member; } \
  35. m_type get_##m_member() const { return base.m_member; }
  36. #define RD_BIND(m_variant_type, m_class, m_member) \
  37. ClassDB::bind_method(D_METHOD("set_" _MKSTR(m_member), "p_" _MKSTR(member)), &m_class::set_##m_member); \
  38. ClassDB::bind_method(D_METHOD("get_" _MKSTR(m_member)), &m_class::get_##m_member); \
  39. ADD_PROPERTY(PropertyInfo(m_variant_type, #m_member), "set_" _MKSTR(m_member), "get_" _MKSTR(m_member))
  40. #define RD_SETGET_SUB(m_type, m_sub, m_member) \
  41. void set_##m_sub##_##m_member(m_type p_##m_member) { base.m_sub.m_member = p_##m_member; } \
  42. m_type get_##m_sub##_##m_member() const { return base.m_sub.m_member; }
  43. #define RD_BIND_SUB(m_variant_type, m_class, m_sub, m_member) \
  44. ClassDB::bind_method(D_METHOD("set_" _MKSTR(m_sub) "_" _MKSTR(m_member), "p_" _MKSTR(member)), &m_class::set_##m_sub##_##m_member); \
  45. ClassDB::bind_method(D_METHOD("get_" _MKSTR(m_sub) "_" _MKSTR(m_member)), &m_class::get_##m_sub##_##m_member); \
  46. ADD_PROPERTY(PropertyInfo(m_variant_type, _MKSTR(m_sub) "_" _MKSTR(m_member)), "set_" _MKSTR(m_sub) "_" _MKSTR(m_member), "get_" _MKSTR(m_sub) "_" _MKSTR(m_member))
  47. class RDTextureFormat : public RefCounted {
  48. GDCLASS(RDTextureFormat, RefCounted)
  49. friend class RenderingDevice;
  50. friend class RenderSceneBuffersRD;
  51. RD::TextureFormat base;
  52. public:
  53. RD_SETGET(RD::DataFormat, format)
  54. RD_SETGET(uint32_t, width)
  55. RD_SETGET(uint32_t, height)
  56. RD_SETGET(uint32_t, depth)
  57. RD_SETGET(uint32_t, array_layers)
  58. RD_SETGET(uint32_t, mipmaps)
  59. RD_SETGET(RD::TextureType, texture_type)
  60. RD_SETGET(RD::TextureSamples, samples)
  61. RD_SETGET(BitField<RenderingDevice::TextureUsageBits>, usage_bits)
  62. RD_SETGET(bool, is_resolve_buffer)
  63. RD_SETGET(bool, is_discardable)
  64. void add_shareable_format(RD::DataFormat p_format) { base.shareable_formats.push_back(p_format); }
  65. void remove_shareable_format(RD::DataFormat p_format) { base.shareable_formats.erase(p_format); }
  66. protected:
  67. static void _bind_methods() {
  68. RD_BIND(Variant::INT, RDTextureFormat, format);
  69. RD_BIND(Variant::INT, RDTextureFormat, width);
  70. RD_BIND(Variant::INT, RDTextureFormat, height);
  71. RD_BIND(Variant::INT, RDTextureFormat, depth);
  72. RD_BIND(Variant::INT, RDTextureFormat, array_layers);
  73. RD_BIND(Variant::INT, RDTextureFormat, mipmaps);
  74. RD_BIND(Variant::INT, RDTextureFormat, texture_type);
  75. RD_BIND(Variant::INT, RDTextureFormat, samples);
  76. RD_BIND(Variant::INT, RDTextureFormat, usage_bits);
  77. RD_BIND(Variant::BOOL, RDTextureFormat, is_resolve_buffer);
  78. RD_BIND(Variant::BOOL, RDTextureFormat, is_discardable);
  79. ClassDB::bind_method(D_METHOD("add_shareable_format", "format"), &RDTextureFormat::add_shareable_format);
  80. ClassDB::bind_method(D_METHOD("remove_shareable_format", "format"), &RDTextureFormat::remove_shareable_format);
  81. }
  82. };
  83. class RDTextureView : public RefCounted {
  84. GDCLASS(RDTextureView, RefCounted)
  85. friend class RenderingDevice;
  86. friend class RenderSceneBuffersRD;
  87. RD::TextureView base;
  88. public:
  89. RD_SETGET(RD::DataFormat, format_override)
  90. RD_SETGET(RD::TextureSwizzle, swizzle_r)
  91. RD_SETGET(RD::TextureSwizzle, swizzle_g)
  92. RD_SETGET(RD::TextureSwizzle, swizzle_b)
  93. RD_SETGET(RD::TextureSwizzle, swizzle_a)
  94. protected:
  95. static void _bind_methods() {
  96. RD_BIND(Variant::INT, RDTextureView, format_override);
  97. RD_BIND(Variant::INT, RDTextureView, swizzle_r);
  98. RD_BIND(Variant::INT, RDTextureView, swizzle_g);
  99. RD_BIND(Variant::INT, RDTextureView, swizzle_b);
  100. RD_BIND(Variant::INT, RDTextureView, swizzle_a);
  101. }
  102. };
  103. class RDAttachmentFormat : public RefCounted {
  104. GDCLASS(RDAttachmentFormat, RefCounted)
  105. friend class RenderingDevice;
  106. RD::AttachmentFormat base;
  107. public:
  108. RD_SETGET(RD::DataFormat, format)
  109. RD_SETGET(RD::TextureSamples, samples)
  110. RD_SETGET(uint32_t, usage_flags)
  111. protected:
  112. static void _bind_methods() {
  113. RD_BIND(Variant::INT, RDAttachmentFormat, format);
  114. RD_BIND(Variant::INT, RDAttachmentFormat, samples);
  115. RD_BIND(Variant::INT, RDAttachmentFormat, usage_flags);
  116. }
  117. };
  118. class RDFramebufferPass : public RefCounted {
  119. GDCLASS(RDFramebufferPass, RefCounted)
  120. friend class RenderingDevice;
  121. friend class FramebufferCacheRD;
  122. RD::FramebufferPass base;
  123. public:
  124. RD_SETGET(PackedInt32Array, color_attachments)
  125. RD_SETGET(PackedInt32Array, input_attachments)
  126. RD_SETGET(PackedInt32Array, resolve_attachments)
  127. RD_SETGET(PackedInt32Array, preserve_attachments)
  128. RD_SETGET(int32_t, depth_attachment)
  129. protected:
  130. enum {
  131. ATTACHMENT_UNUSED = -1
  132. };
  133. static void _bind_methods() {
  134. RD_BIND(Variant::PACKED_INT32_ARRAY, RDFramebufferPass, color_attachments);
  135. RD_BIND(Variant::PACKED_INT32_ARRAY, RDFramebufferPass, input_attachments);
  136. RD_BIND(Variant::PACKED_INT32_ARRAY, RDFramebufferPass, resolve_attachments);
  137. RD_BIND(Variant::PACKED_INT32_ARRAY, RDFramebufferPass, preserve_attachments);
  138. RD_BIND(Variant::INT, RDFramebufferPass, depth_attachment);
  139. BIND_CONSTANT(ATTACHMENT_UNUSED);
  140. }
  141. };
  142. class RDSamplerState : public RefCounted {
  143. GDCLASS(RDSamplerState, RefCounted)
  144. friend class RenderingDevice;
  145. RD::SamplerState base;
  146. public:
  147. RD_SETGET(RD::SamplerFilter, mag_filter)
  148. RD_SETGET(RD::SamplerFilter, min_filter)
  149. RD_SETGET(RD::SamplerFilter, mip_filter)
  150. RD_SETGET(RD::SamplerRepeatMode, repeat_u)
  151. RD_SETGET(RD::SamplerRepeatMode, repeat_v)
  152. RD_SETGET(RD::SamplerRepeatMode, repeat_w)
  153. RD_SETGET(float, lod_bias)
  154. RD_SETGET(bool, use_anisotropy)
  155. RD_SETGET(float, anisotropy_max)
  156. RD_SETGET(bool, enable_compare)
  157. RD_SETGET(RD::CompareOperator, compare_op)
  158. RD_SETGET(float, min_lod)
  159. RD_SETGET(float, max_lod)
  160. RD_SETGET(RD::SamplerBorderColor, border_color)
  161. RD_SETGET(bool, unnormalized_uvw)
  162. protected:
  163. static void _bind_methods() {
  164. RD_BIND(Variant::INT, RDSamplerState, mag_filter);
  165. RD_BIND(Variant::INT, RDSamplerState, min_filter);
  166. RD_BIND(Variant::INT, RDSamplerState, mip_filter);
  167. RD_BIND(Variant::INT, RDSamplerState, repeat_u);
  168. RD_BIND(Variant::INT, RDSamplerState, repeat_v);
  169. RD_BIND(Variant::INT, RDSamplerState, repeat_w);
  170. RD_BIND(Variant::FLOAT, RDSamplerState, lod_bias);
  171. RD_BIND(Variant::BOOL, RDSamplerState, use_anisotropy);
  172. RD_BIND(Variant::FLOAT, RDSamplerState, anisotropy_max);
  173. RD_BIND(Variant::BOOL, RDSamplerState, enable_compare);
  174. RD_BIND(Variant::INT, RDSamplerState, compare_op);
  175. RD_BIND(Variant::FLOAT, RDSamplerState, min_lod);
  176. RD_BIND(Variant::FLOAT, RDSamplerState, max_lod);
  177. RD_BIND(Variant::INT, RDSamplerState, border_color);
  178. RD_BIND(Variant::BOOL, RDSamplerState, unnormalized_uvw);
  179. }
  180. };
  181. class RDVertexAttribute : public RefCounted {
  182. GDCLASS(RDVertexAttribute, RefCounted)
  183. friend class RenderingDevice;
  184. RD::VertexAttribute base;
  185. public:
  186. RD_SETGET(uint32_t, location)
  187. RD_SETGET(uint32_t, offset)
  188. RD_SETGET(RD::DataFormat, format)
  189. RD_SETGET(uint32_t, stride)
  190. RD_SETGET(RD::VertexFrequency, frequency)
  191. protected:
  192. static void _bind_methods() {
  193. RD_BIND(Variant::INT, RDVertexAttribute, location);
  194. RD_BIND(Variant::INT, RDVertexAttribute, offset);
  195. RD_BIND(Variant::INT, RDVertexAttribute, format);
  196. RD_BIND(Variant::INT, RDVertexAttribute, stride);
  197. RD_BIND(Variant::INT, RDVertexAttribute, frequency);
  198. }
  199. };
  200. class RDShaderSource : public RefCounted {
  201. GDCLASS(RDShaderSource, RefCounted)
  202. String source[RD::SHADER_STAGE_MAX];
  203. RD::ShaderLanguage language = RD::SHADER_LANGUAGE_GLSL;
  204. public:
  205. void set_stage_source(RD::ShaderStage p_stage, const String &p_source) {
  206. ERR_FAIL_INDEX(p_stage, RD::SHADER_STAGE_MAX);
  207. source[p_stage] = p_source;
  208. }
  209. String get_stage_source(RD::ShaderStage p_stage) const {
  210. ERR_FAIL_INDEX_V(p_stage, RD::SHADER_STAGE_MAX, String());
  211. return source[p_stage];
  212. }
  213. void set_language(RD::ShaderLanguage p_language) {
  214. language = p_language;
  215. }
  216. RD::ShaderLanguage get_language() const {
  217. return language;
  218. }
  219. protected:
  220. static void _bind_methods() {
  221. ClassDB::bind_method(D_METHOD("set_stage_source", "stage", "source"), &RDShaderSource::set_stage_source);
  222. ClassDB::bind_method(D_METHOD("get_stage_source", "stage"), &RDShaderSource::get_stage_source);
  223. ClassDB::bind_method(D_METHOD("set_language", "language"), &RDShaderSource::set_language);
  224. ClassDB::bind_method(D_METHOD("get_language"), &RDShaderSource::get_language);
  225. ADD_GROUP("Source", "source_");
  226. ADD_PROPERTYI(PropertyInfo(Variant::STRING, "source_vertex"), "set_stage_source", "get_stage_source", RD::SHADER_STAGE_VERTEX);
  227. ADD_PROPERTYI(PropertyInfo(Variant::STRING, "source_fragment"), "set_stage_source", "get_stage_source", RD::SHADER_STAGE_FRAGMENT);
  228. ADD_PROPERTYI(PropertyInfo(Variant::STRING, "source_tesselation_control"), "set_stage_source", "get_stage_source", RD::SHADER_STAGE_TESSELATION_CONTROL);
  229. ADD_PROPERTYI(PropertyInfo(Variant::STRING, "source_tesselation_evaluation"), "set_stage_source", "get_stage_source", RD::SHADER_STAGE_TESSELATION_EVALUATION);
  230. ADD_PROPERTYI(PropertyInfo(Variant::STRING, "source_compute"), "set_stage_source", "get_stage_source", RD::SHADER_STAGE_COMPUTE);
  231. ADD_GROUP("Syntax", "source_");
  232. ADD_PROPERTY(PropertyInfo(Variant::INT, "language", PROPERTY_HINT_RANGE, "GLSL,HLSL"), "set_language", "get_language");
  233. }
  234. };
  235. class RDShaderSPIRV : public Resource {
  236. GDCLASS(RDShaderSPIRV, Resource)
  237. Vector<uint8_t> bytecode[RD::SHADER_STAGE_MAX];
  238. String compile_error[RD::SHADER_STAGE_MAX];
  239. public:
  240. void set_stage_bytecode(RD::ShaderStage p_stage, const Vector<uint8_t> &p_bytecode) {
  241. ERR_FAIL_INDEX(p_stage, RD::SHADER_STAGE_MAX);
  242. bytecode[p_stage] = p_bytecode;
  243. }
  244. Vector<uint8_t> get_stage_bytecode(RD::ShaderStage p_stage) const {
  245. ERR_FAIL_INDEX_V(p_stage, RD::SHADER_STAGE_MAX, Vector<uint8_t>());
  246. return bytecode[p_stage];
  247. }
  248. Vector<RD::ShaderStageSPIRVData> get_stages() const {
  249. Vector<RD::ShaderStageSPIRVData> stages;
  250. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  251. if (bytecode[i].size()) {
  252. RD::ShaderStageSPIRVData stage;
  253. stage.shader_stage = RD::ShaderStage(i);
  254. stage.spirv = bytecode[i];
  255. stages.push_back(stage);
  256. }
  257. }
  258. return stages;
  259. }
  260. void set_stage_compile_error(RD::ShaderStage p_stage, const String &p_compile_error) {
  261. ERR_FAIL_INDEX(p_stage, RD::SHADER_STAGE_MAX);
  262. compile_error[p_stage] = p_compile_error;
  263. }
  264. String get_stage_compile_error(RD::ShaderStage p_stage) const {
  265. ERR_FAIL_INDEX_V(p_stage, RD::SHADER_STAGE_MAX, String());
  266. return compile_error[p_stage];
  267. }
  268. protected:
  269. static void _bind_methods() {
  270. ClassDB::bind_method(D_METHOD("set_stage_bytecode", "stage", "bytecode"), &RDShaderSPIRV::set_stage_bytecode);
  271. ClassDB::bind_method(D_METHOD("get_stage_bytecode", "stage"), &RDShaderSPIRV::get_stage_bytecode);
  272. ClassDB::bind_method(D_METHOD("set_stage_compile_error", "stage", "compile_error"), &RDShaderSPIRV::set_stage_compile_error);
  273. ClassDB::bind_method(D_METHOD("get_stage_compile_error", "stage"), &RDShaderSPIRV::get_stage_compile_error);
  274. ADD_GROUP("Bytecode", "bytecode_");
  275. ADD_PROPERTYI(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "bytecode_vertex"), "set_stage_bytecode", "get_stage_bytecode", RD::SHADER_STAGE_VERTEX);
  276. ADD_PROPERTYI(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "bytecode_fragment"), "set_stage_bytecode", "get_stage_bytecode", RD::SHADER_STAGE_FRAGMENT);
  277. ADD_PROPERTYI(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "bytecode_tesselation_control"), "set_stage_bytecode", "get_stage_bytecode", RD::SHADER_STAGE_TESSELATION_CONTROL);
  278. ADD_PROPERTYI(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "bytecode_tesselation_evaluation"), "set_stage_bytecode", "get_stage_bytecode", RD::SHADER_STAGE_TESSELATION_EVALUATION);
  279. ADD_PROPERTYI(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "bytecode_compute"), "set_stage_bytecode", "get_stage_bytecode", RD::SHADER_STAGE_COMPUTE);
  280. ADD_GROUP("Compile Error", "compile_error_");
  281. ADD_PROPERTYI(PropertyInfo(Variant::STRING, "compile_error_vertex"), "set_stage_compile_error", "get_stage_compile_error", RD::SHADER_STAGE_VERTEX);
  282. ADD_PROPERTYI(PropertyInfo(Variant::STRING, "compile_error_fragment"), "set_stage_compile_error", "get_stage_compile_error", RD::SHADER_STAGE_FRAGMENT);
  283. ADD_PROPERTYI(PropertyInfo(Variant::STRING, "compile_error_tesselation_control"), "set_stage_compile_error", "get_stage_compile_error", RD::SHADER_STAGE_TESSELATION_CONTROL);
  284. ADD_PROPERTYI(PropertyInfo(Variant::STRING, "compile_error_tesselation_evaluation"), "set_stage_compile_error", "get_stage_compile_error", RD::SHADER_STAGE_TESSELATION_EVALUATION);
  285. ADD_PROPERTYI(PropertyInfo(Variant::STRING, "compile_error_compute"), "set_stage_compile_error", "get_stage_compile_error", RD::SHADER_STAGE_COMPUTE);
  286. }
  287. };
  288. class RDShaderFile : public Resource {
  289. GDCLASS(RDShaderFile, Resource)
  290. HashMap<StringName, Ref<RDShaderSPIRV>> versions;
  291. String base_error;
  292. public:
  293. void set_bytecode(const Ref<RDShaderSPIRV> &p_bytecode, const StringName &p_version = StringName()) {
  294. ERR_FAIL_COND(p_bytecode.is_null());
  295. versions[p_version] = p_bytecode;
  296. emit_changed();
  297. }
  298. Ref<RDShaderSPIRV> get_spirv(const StringName &p_version = StringName()) const {
  299. ERR_FAIL_COND_V(!versions.has(p_version), Ref<RDShaderSPIRV>());
  300. return versions[p_version];
  301. }
  302. Vector<RD::ShaderStageSPIRVData> get_spirv_stages(const StringName &p_version = StringName()) const {
  303. ERR_FAIL_COND_V(!versions.has(p_version), Vector<RD::ShaderStageSPIRVData>());
  304. return versions[p_version]->get_stages();
  305. }
  306. TypedArray<StringName> get_version_list() const {
  307. Vector<StringName> vnames;
  308. for (const KeyValue<StringName, Ref<RDShaderSPIRV>> &E : versions) {
  309. vnames.push_back(E.key);
  310. }
  311. vnames.sort_custom<StringName::AlphCompare>();
  312. TypedArray<StringName> ret;
  313. ret.resize(vnames.size());
  314. for (int i = 0; i < vnames.size(); i++) {
  315. ret[i] = vnames[i];
  316. }
  317. return ret;
  318. }
  319. void set_base_error(const String &p_error) {
  320. base_error = p_error;
  321. emit_changed();
  322. }
  323. String get_base_error() const {
  324. return base_error;
  325. }
  326. void print_errors(const String &p_file) {
  327. if (!base_error.is_empty()) {
  328. ERR_PRINT("Error parsing shader '" + p_file + "':\n\n" + base_error);
  329. } else {
  330. for (KeyValue<StringName, Ref<RDShaderSPIRV>> &E : versions) {
  331. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  332. String error = E.value->get_stage_compile_error(RD::ShaderStage(i));
  333. if (!error.is_empty()) {
  334. static const char *stage_str[RD::SHADER_STAGE_MAX] = {
  335. "vertex",
  336. "fragment",
  337. "tesselation_control",
  338. "tesselation_evaluation",
  339. "compute"
  340. };
  341. ERR_PRINT("Error parsing shader '" + p_file + "', version '" + String(E.key) + "', stage '" + stage_str[i] + "':\n\n" + error);
  342. }
  343. }
  344. }
  345. }
  346. }
  347. typedef String (*OpenIncludeFunction)(const String &, void *userdata);
  348. Error parse_versions_from_text(const String &p_text, const String p_defines = String(), OpenIncludeFunction p_include_func = nullptr, void *p_include_func_userdata = nullptr);
  349. protected:
  350. Dictionary _get_versions() const {
  351. TypedArray<StringName> vnames = get_version_list();
  352. Dictionary ret;
  353. for (int i = 0; i < vnames.size(); i++) {
  354. ret[vnames[i]] = versions[vnames[i]];
  355. }
  356. return ret;
  357. }
  358. void _set_versions(const Dictionary &p_versions) {
  359. versions.clear();
  360. List<Variant> keys;
  361. p_versions.get_key_list(&keys);
  362. for (const Variant &E : keys) {
  363. StringName vname = E;
  364. Ref<RDShaderSPIRV> bc = p_versions[E];
  365. ERR_CONTINUE(bc.is_null());
  366. versions[vname] = bc;
  367. }
  368. emit_changed();
  369. }
  370. static void _bind_methods() {
  371. ClassDB::bind_method(D_METHOD("set_bytecode", "bytecode", "version"), &RDShaderFile::set_bytecode, DEFVAL(StringName()));
  372. ClassDB::bind_method(D_METHOD("get_spirv", "version"), &RDShaderFile::get_spirv, DEFVAL(StringName()));
  373. ClassDB::bind_method(D_METHOD("get_version_list"), &RDShaderFile::get_version_list);
  374. ClassDB::bind_method(D_METHOD("set_base_error", "error"), &RDShaderFile::set_base_error);
  375. ClassDB::bind_method(D_METHOD("get_base_error"), &RDShaderFile::get_base_error);
  376. ClassDB::bind_method(D_METHOD("_set_versions", "versions"), &RDShaderFile::_set_versions);
  377. ClassDB::bind_method(D_METHOD("_get_versions"), &RDShaderFile::_get_versions);
  378. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_versions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_versions", "_get_versions");
  379. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_error"), "set_base_error", "get_base_error");
  380. }
  381. };
  382. class RDUniform : public RefCounted {
  383. GDCLASS(RDUniform, RefCounted)
  384. friend class RenderingDevice;
  385. friend class UniformSetCacheRD;
  386. RD::Uniform base;
  387. public:
  388. RD_SETGET(RD::UniformType, uniform_type)
  389. RD_SETGET(int32_t, binding)
  390. void add_id(const RID &p_id) { base.append_id(p_id); }
  391. void clear_ids() { base.clear_ids(); }
  392. TypedArray<RID> get_ids() const {
  393. TypedArray<RID> ids;
  394. for (uint32_t i = 0; i < base.get_id_count(); i++) {
  395. ids.push_back(base.get_id(i));
  396. }
  397. return ids;
  398. }
  399. protected:
  400. void _set_ids(const TypedArray<RID> &p_ids) {
  401. base.clear_ids();
  402. for (int i = 0; i < p_ids.size(); i++) {
  403. RID id = p_ids[i];
  404. ERR_FAIL_COND(id.is_null());
  405. base.append_id(id);
  406. }
  407. }
  408. static void _bind_methods() {
  409. RD_BIND(Variant::INT, RDUniform, uniform_type);
  410. RD_BIND(Variant::INT, RDUniform, binding);
  411. ClassDB::bind_method(D_METHOD("add_id", "id"), &RDUniform::add_id);
  412. ClassDB::bind_method(D_METHOD("clear_ids"), &RDUniform::clear_ids);
  413. ClassDB::bind_method(D_METHOD("_set_ids", "ids"), &RDUniform::_set_ids);
  414. ClassDB::bind_method(D_METHOD("get_ids"), &RDUniform::get_ids);
  415. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_ids", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "_set_ids", "get_ids");
  416. }
  417. };
  418. class RDPipelineSpecializationConstant : public RefCounted {
  419. GDCLASS(RDPipelineSpecializationConstant, RefCounted)
  420. friend class RenderingDevice;
  421. Variant value = false;
  422. uint32_t constant_id = 0;
  423. public:
  424. void set_value(const Variant &p_value) {
  425. ERR_FAIL_COND(p_value.get_type() != Variant::BOOL && p_value.get_type() != Variant::INT && p_value.get_type() != Variant::FLOAT);
  426. value = p_value;
  427. }
  428. Variant get_value() const { return value; }
  429. void set_constant_id(uint32_t p_id) {
  430. constant_id = p_id;
  431. }
  432. uint32_t get_constant_id() const {
  433. return constant_id;
  434. }
  435. protected:
  436. static void _bind_methods() {
  437. ClassDB::bind_method(D_METHOD("set_value", "value"), &RDPipelineSpecializationConstant::set_value);
  438. ClassDB::bind_method(D_METHOD("get_value"), &RDPipelineSpecializationConstant::get_value);
  439. ClassDB::bind_method(D_METHOD("set_constant_id", "constant_id"), &RDPipelineSpecializationConstant::set_constant_id);
  440. ClassDB::bind_method(D_METHOD("get_constant_id"), &RDPipelineSpecializationConstant::get_constant_id);
  441. ADD_PROPERTY(PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), "set_value", "get_value");
  442. ADD_PROPERTY(PropertyInfo(Variant::INT, "constant_id", PROPERTY_HINT_RANGE, "0,65535,0"), "set_constant_id", "get_constant_id");
  443. }
  444. };
  445. class RDPipelineRasterizationState : public RefCounted {
  446. GDCLASS(RDPipelineRasterizationState, RefCounted)
  447. friend class RenderingDevice;
  448. RD::PipelineRasterizationState base;
  449. public:
  450. RD_SETGET(bool, enable_depth_clamp)
  451. RD_SETGET(bool, discard_primitives)
  452. RD_SETGET(bool, wireframe)
  453. RD_SETGET(RD::PolygonCullMode, cull_mode)
  454. RD_SETGET(RD::PolygonFrontFace, front_face)
  455. RD_SETGET(bool, depth_bias_enabled)
  456. RD_SETGET(float, depth_bias_constant_factor)
  457. RD_SETGET(float, depth_bias_clamp)
  458. RD_SETGET(float, depth_bias_slope_factor)
  459. RD_SETGET(float, line_width)
  460. RD_SETGET(uint32_t, patch_control_points)
  461. protected:
  462. static void _bind_methods() {
  463. RD_BIND(Variant::BOOL, RDPipelineRasterizationState, enable_depth_clamp);
  464. RD_BIND(Variant::BOOL, RDPipelineRasterizationState, discard_primitives);
  465. RD_BIND(Variant::BOOL, RDPipelineRasterizationState, wireframe);
  466. RD_BIND(Variant::INT, RDPipelineRasterizationState, cull_mode);
  467. RD_BIND(Variant::INT, RDPipelineRasterizationState, front_face);
  468. RD_BIND(Variant::BOOL, RDPipelineRasterizationState, depth_bias_enabled);
  469. RD_BIND(Variant::FLOAT, RDPipelineRasterizationState, depth_bias_constant_factor);
  470. RD_BIND(Variant::FLOAT, RDPipelineRasterizationState, depth_bias_clamp);
  471. RD_BIND(Variant::FLOAT, RDPipelineRasterizationState, depth_bias_slope_factor);
  472. RD_BIND(Variant::FLOAT, RDPipelineRasterizationState, line_width);
  473. RD_BIND(Variant::INT, RDPipelineRasterizationState, patch_control_points);
  474. }
  475. };
  476. class RDPipelineMultisampleState : public RefCounted {
  477. GDCLASS(RDPipelineMultisampleState, RefCounted)
  478. friend class RenderingDevice;
  479. RD::PipelineMultisampleState base;
  480. TypedArray<int64_t> sample_masks;
  481. public:
  482. RD_SETGET(RD::TextureSamples, sample_count)
  483. RD_SETGET(bool, enable_sample_shading)
  484. RD_SETGET(float, min_sample_shading)
  485. RD_SETGET(bool, enable_alpha_to_coverage)
  486. RD_SETGET(bool, enable_alpha_to_one)
  487. void set_sample_masks(const TypedArray<int64_t> &p_masks) { sample_masks = p_masks; }
  488. TypedArray<int64_t> get_sample_masks() const { return sample_masks; }
  489. protected:
  490. static void _bind_methods() {
  491. RD_BIND(Variant::INT, RDPipelineMultisampleState, sample_count);
  492. RD_BIND(Variant::BOOL, RDPipelineMultisampleState, enable_sample_shading);
  493. RD_BIND(Variant::FLOAT, RDPipelineMultisampleState, min_sample_shading);
  494. RD_BIND(Variant::BOOL, RDPipelineMultisampleState, enable_alpha_to_coverage);
  495. RD_BIND(Variant::BOOL, RDPipelineMultisampleState, enable_alpha_to_one);
  496. ClassDB::bind_method(D_METHOD("set_sample_masks", "masks"), &RDPipelineMultisampleState::set_sample_masks);
  497. ClassDB::bind_method(D_METHOD("get_sample_masks"), &RDPipelineMultisampleState::get_sample_masks);
  498. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "sample_masks", PROPERTY_HINT_ARRAY_TYPE, "int"), "set_sample_masks", "get_sample_masks");
  499. }
  500. };
  501. class RDPipelineDepthStencilState : public RefCounted {
  502. GDCLASS(RDPipelineDepthStencilState, RefCounted)
  503. friend class RenderingDevice;
  504. RD::PipelineDepthStencilState base;
  505. public:
  506. RD_SETGET(bool, enable_depth_test)
  507. RD_SETGET(bool, enable_depth_write)
  508. RD_SETGET(RD::CompareOperator, depth_compare_operator)
  509. RD_SETGET(bool, enable_depth_range)
  510. RD_SETGET(float, depth_range_min)
  511. RD_SETGET(float, depth_range_max)
  512. RD_SETGET(bool, enable_stencil)
  513. RD_SETGET_SUB(RD::StencilOperation, front_op, fail)
  514. RD_SETGET_SUB(RD::StencilOperation, front_op, pass)
  515. RD_SETGET_SUB(RD::StencilOperation, front_op, depth_fail)
  516. RD_SETGET_SUB(RD::CompareOperator, front_op, compare)
  517. RD_SETGET_SUB(uint32_t, front_op, compare_mask)
  518. RD_SETGET_SUB(uint32_t, front_op, write_mask)
  519. RD_SETGET_SUB(uint32_t, front_op, reference)
  520. RD_SETGET_SUB(RD::StencilOperation, back_op, fail)
  521. RD_SETGET_SUB(RD::StencilOperation, back_op, pass)
  522. RD_SETGET_SUB(RD::StencilOperation, back_op, depth_fail)
  523. RD_SETGET_SUB(RD::CompareOperator, back_op, compare)
  524. RD_SETGET_SUB(uint32_t, back_op, compare_mask)
  525. RD_SETGET_SUB(uint32_t, back_op, write_mask)
  526. RD_SETGET_SUB(uint32_t, back_op, reference)
  527. protected:
  528. static void _bind_methods() {
  529. RD_BIND(Variant::BOOL, RDPipelineDepthStencilState, enable_depth_test);
  530. RD_BIND(Variant::BOOL, RDPipelineDepthStencilState, enable_depth_write);
  531. RD_BIND(Variant::INT, RDPipelineDepthStencilState, depth_compare_operator);
  532. RD_BIND(Variant::BOOL, RDPipelineDepthStencilState, enable_depth_range);
  533. RD_BIND(Variant::FLOAT, RDPipelineDepthStencilState, depth_range_min);
  534. RD_BIND(Variant::FLOAT, RDPipelineDepthStencilState, depth_range_max);
  535. RD_BIND(Variant::BOOL, RDPipelineDepthStencilState, enable_stencil);
  536. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, front_op, fail);
  537. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, front_op, pass);
  538. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, front_op, depth_fail);
  539. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, front_op, compare);
  540. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, front_op, compare_mask);
  541. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, front_op, write_mask);
  542. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, front_op, reference);
  543. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, back_op, fail);
  544. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, back_op, pass);
  545. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, back_op, depth_fail);
  546. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, back_op, compare);
  547. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, back_op, compare_mask);
  548. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, back_op, write_mask);
  549. RD_BIND_SUB(Variant::INT, RDPipelineDepthStencilState, back_op, reference);
  550. }
  551. };
  552. class RDPipelineColorBlendStateAttachment : public RefCounted {
  553. GDCLASS(RDPipelineColorBlendStateAttachment, RefCounted)
  554. friend class RenderingDevice;
  555. RD::PipelineColorBlendState::Attachment base;
  556. public:
  557. RD_SETGET(bool, enable_blend)
  558. RD_SETGET(RD::BlendFactor, src_color_blend_factor)
  559. RD_SETGET(RD::BlendFactor, dst_color_blend_factor)
  560. RD_SETGET(RD::BlendOperation, color_blend_op)
  561. RD_SETGET(RD::BlendFactor, src_alpha_blend_factor)
  562. RD_SETGET(RD::BlendFactor, dst_alpha_blend_factor)
  563. RD_SETGET(RD::BlendOperation, alpha_blend_op)
  564. RD_SETGET(bool, write_r)
  565. RD_SETGET(bool, write_g)
  566. RD_SETGET(bool, write_b)
  567. RD_SETGET(bool, write_a)
  568. void set_as_mix() {
  569. base = RD::PipelineColorBlendState::Attachment();
  570. base.enable_blend = true;
  571. base.src_color_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA;
  572. base.dst_color_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  573. base.src_alpha_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA;
  574. base.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  575. }
  576. protected:
  577. static void _bind_methods() {
  578. ClassDB::bind_method(D_METHOD("set_as_mix"), &RDPipelineColorBlendStateAttachment::set_as_mix);
  579. RD_BIND(Variant::BOOL, RDPipelineColorBlendStateAttachment, enable_blend);
  580. RD_BIND(Variant::INT, RDPipelineColorBlendStateAttachment, src_color_blend_factor);
  581. RD_BIND(Variant::INT, RDPipelineColorBlendStateAttachment, dst_color_blend_factor);
  582. RD_BIND(Variant::INT, RDPipelineColorBlendStateAttachment, color_blend_op);
  583. RD_BIND(Variant::INT, RDPipelineColorBlendStateAttachment, src_alpha_blend_factor);
  584. RD_BIND(Variant::INT, RDPipelineColorBlendStateAttachment, dst_alpha_blend_factor);
  585. RD_BIND(Variant::INT, RDPipelineColorBlendStateAttachment, alpha_blend_op);
  586. RD_BIND(Variant::BOOL, RDPipelineColorBlendStateAttachment, write_r);
  587. RD_BIND(Variant::BOOL, RDPipelineColorBlendStateAttachment, write_g);
  588. RD_BIND(Variant::BOOL, RDPipelineColorBlendStateAttachment, write_b);
  589. RD_BIND(Variant::BOOL, RDPipelineColorBlendStateAttachment, write_a);
  590. }
  591. };
  592. class RDPipelineColorBlendState : public RefCounted {
  593. GDCLASS(RDPipelineColorBlendState, RefCounted)
  594. friend class RenderingDevice;
  595. RD::PipelineColorBlendState base;
  596. TypedArray<RDPipelineColorBlendStateAttachment> attachments;
  597. public:
  598. RD_SETGET(bool, enable_logic_op)
  599. RD_SETGET(RD::LogicOperation, logic_op)
  600. RD_SETGET(Color, blend_constant)
  601. void set_attachments(const TypedArray<RDPipelineColorBlendStateAttachment> &p_attachments) {
  602. attachments = p_attachments;
  603. }
  604. TypedArray<RDPipelineColorBlendStateAttachment> get_attachments() const {
  605. return attachments;
  606. }
  607. protected:
  608. static void _bind_methods() {
  609. RD_BIND(Variant::BOOL, RDPipelineColorBlendState, enable_logic_op);
  610. RD_BIND(Variant::INT, RDPipelineColorBlendState, logic_op);
  611. RD_BIND(Variant::COLOR, RDPipelineColorBlendState, blend_constant);
  612. ClassDB::bind_method(D_METHOD("set_attachments", "attachments"), &RDPipelineColorBlendState::set_attachments);
  613. ClassDB::bind_method(D_METHOD("get_attachments"), &RDPipelineColorBlendState::get_attachments);
  614. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "attachments", PROPERTY_HINT_ARRAY_TYPE, "RDPipelineColorBlendStateAttachment"), "set_attachments", "get_attachments");
  615. }
  616. };
  617. #endif // RENDERING_DEVICE_BINDS_H