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