rendering_device_binds.h 30 KB

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