rendering_device_binds.h 29 KB

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