Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/gles3/rasterizer_canvas_gles3.h
9973 views
1
/**************************************************************************/
2
/* rasterizer_canvas_gles3.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
#ifdef GLES3_ENABLED
34
35
#include "rasterizer_scene_gles3.h"
36
#include "servers/rendering/renderer_canvas_render.h"
37
#include "servers/rendering/renderer_compositor.h"
38
#include "storage/material_storage.h"
39
#include "storage/texture_storage.h"
40
41
#include "drivers/gles3/shaders/canvas.glsl.gen.h"
42
#include "drivers/gles3/shaders/canvas_occlusion.glsl.gen.h"
43
44
class RasterizerSceneGLES3;
45
46
class RasterizerCanvasGLES3 : public RendererCanvasRender {
47
static RasterizerCanvasGLES3 *singleton;
48
49
_FORCE_INLINE_ void _update_transform_2d_to_mat2x4(const Transform2D &p_transform, float *p_mat2x4);
50
_FORCE_INLINE_ void _update_transform_2d_to_mat2x3(const Transform2D &p_transform, float *p_mat2x3);
51
52
_FORCE_INLINE_ void _update_transform_2d_to_mat4(const Transform2D &p_transform, float *p_mat4);
53
_FORCE_INLINE_ void _update_transform_to_mat4(const Transform3D &p_transform, float *p_mat4);
54
55
enum {
56
INSTANCE_FLAGS_LIGHT_COUNT_SHIFT = 0, // 4 bits for light count.
57
58
INSTANCE_FLAGS_CLIP_RECT_UV = (1 << 4),
59
INSTANCE_FLAGS_TRANSPOSE_RECT = (1 << 5),
60
INSTANCE_FLAGS_USE_MSDF = (1 << 6),
61
INSTANCE_FLAGS_USE_LCD = (1 << 7),
62
63
INSTANCE_FLAGS_NINEPACH_DRAW_CENTER = (1 << 8),
64
INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT = 9,
65
INSTANCE_FLAGS_NINEPATCH_V_MODE_SHIFT = 11,
66
67
INSTANCE_FLAGS_SHADOW_MASKED_SHIFT = 13, // 16 bits.
68
};
69
70
enum {
71
BATCH_FLAGS_INSTANCING_MASK = 0x7F,
72
BATCH_FLAGS_INSTANCING_HAS_COLORS = (1 << 7),
73
BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA = (1 << 8),
74
75
BATCH_FLAGS_DEFAULT_NORMAL_MAP_USED = (1 << 9),
76
BATCH_FLAGS_DEFAULT_SPECULAR_MAP_USED = (1 << 10),
77
};
78
79
enum {
80
LIGHT_FLAGS_TEXTURE_MASK = 0xFFFF,
81
LIGHT_FLAGS_BLEND_SHIFT = 16,
82
LIGHT_FLAGS_BLEND_MASK = (3 << 16),
83
LIGHT_FLAGS_BLEND_MODE_ADD = (0 << 16),
84
LIGHT_FLAGS_BLEND_MODE_SUB = (1 << 16),
85
LIGHT_FLAGS_BLEND_MODE_MIX = (2 << 16),
86
LIGHT_FLAGS_BLEND_MODE_MASK = (3 << 16),
87
LIGHT_FLAGS_HAS_SHADOW = (1 << 20),
88
LIGHT_FLAGS_FILTER_SHIFT = 22
89
90
};
91
92
enum {
93
MAX_RENDER_ITEMS = 256 * 1024,
94
MAX_LIGHT_TEXTURES = 1024,
95
MAX_LIGHTS_PER_ITEM = 16,
96
DEFAULT_MAX_LIGHTS_PER_RENDER = 256,
97
};
98
99
/******************/
100
/**** LIGHTING ****/
101
/******************/
102
103
struct CanvasLight {
104
RID texture;
105
struct {
106
bool enabled = false;
107
float z_far;
108
float y_offset;
109
Transform2D directional_xform;
110
} shadow;
111
};
112
113
RID_Owner<CanvasLight> canvas_light_owner;
114
115
struct OccluderPolygon {
116
RS::CanvasOccluderPolygonCullMode cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
117
int line_point_count = 0;
118
GLuint vertex_buffer = 0;
119
GLuint vertex_array = 0;
120
GLuint index_buffer = 0;
121
122
int sdf_point_count = 0;
123
int sdf_index_count = 0;
124
GLuint sdf_vertex_buffer = 0;
125
GLuint sdf_vertex_array = 0;
126
GLuint sdf_index_buffer = 0;
127
bool sdf_is_lines = false;
128
};
129
130
RID_Owner<OccluderPolygon> occluder_polygon_owner;
131
132
void _update_shadow_atlas();
133
134
struct {
135
CanvasOcclusionShaderGLES3 shader;
136
RID shader_version;
137
} shadow_render;
138
139
struct LightUniform {
140
float matrix[8]; //light to texture coordinate matrix
141
float shadow_matrix[8]; //light to shadow coordinate matrix
142
float color[4];
143
144
uint8_t shadow_color[4];
145
uint32_t flags; //index to light texture
146
float shadow_pixel_size;
147
float height;
148
149
float position[2];
150
float shadow_z_far_inv;
151
float shadow_y_ofs;
152
153
float atlas_rect[4];
154
};
155
156
static_assert(sizeof(LightUniform) % 16 == 0, "2D light UBO size must be a multiple of 16 bytes");
157
158
public:
159
enum {
160
BASE_UNIFORM_LOCATION = 0,
161
GLOBAL_UNIFORM_LOCATION = 1,
162
LIGHT_UNIFORM_LOCATION = 2,
163
INSTANCE_UNIFORM_LOCATION = 3,
164
MATERIAL_UNIFORM_LOCATION = 4,
165
};
166
167
struct StateBuffer {
168
float canvas_transform[16];
169
float screen_transform[16];
170
float canvas_normal_transform[16];
171
float canvas_modulate[4];
172
173
float screen_pixel_size[2];
174
float time;
175
uint32_t use_pixel_snap;
176
177
float sdf_to_tex[4];
178
float sdf_to_screen[2];
179
float screen_to_sdf[2];
180
181
uint32_t directional_light_count;
182
float tex_to_sdf;
183
uint32_t pad1;
184
uint32_t pad2;
185
};
186
187
static_assert(sizeof(StateBuffer) % 16 == 0, "2D state UBO size must be a multiple of 16 bytes");
188
189
struct PolygonBuffers {
190
GLuint vertex_buffer = 0;
191
GLuint vertex_array = 0;
192
GLuint index_buffer = 0;
193
int count = 0;
194
bool color_disabled = false;
195
Color color = Color(1.0, 1.0, 1.0, 1.0);
196
};
197
198
struct {
199
HashMap<PolygonID, PolygonBuffers> polygons;
200
PolygonID last_id = 0;
201
} polygon_buffers;
202
203
RendererCanvasRender::PolygonID request_polygon(const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), const Vector<int> &p_bones = Vector<int>(), const Vector<float> &p_weights = Vector<float>(), int p_count = -1) override;
204
void free_polygon(PolygonID p_polygon) override;
205
206
struct InstanceData {
207
float world[6];
208
float color_texture_pixel_size[2];
209
union {
210
//rect
211
struct {
212
float modulation[4];
213
union {
214
float msdf[4];
215
float ninepatch_margins[4];
216
};
217
float dst_rect[4];
218
float src_rect[4];
219
float pad[2];
220
};
221
//primitive
222
struct {
223
float points[6]; // vec2 points[3]
224
float uvs[6]; // vec2 points[3]
225
uint32_t colors[6]; // colors encoded as half
226
};
227
};
228
uint32_t flags;
229
uint32_t instance_uniforms_ofs;
230
uint32_t lights[4];
231
};
232
233
static_assert(sizeof(InstanceData) == 128, "2D instance data struct size must be 128 bytes");
234
235
struct Data {
236
GLuint canvas_quad_vertices;
237
GLuint canvas_quad_array;
238
239
GLuint indexed_quad_buffer;
240
GLuint indexed_quad_array;
241
242
GLuint particle_quad_vertices;
243
GLuint particle_quad_array;
244
245
GLuint ninepatch_vertices;
246
GLuint ninepatch_elements;
247
248
RID canvas_shader_default_version;
249
250
uint32_t max_lights_per_render = 256;
251
uint32_t max_lights_per_item = 16;
252
uint32_t max_instances_per_buffer = 16384;
253
uint32_t max_instance_buffer_size = 16384 * 128;
254
} data;
255
256
struct Batch {
257
// Position in the UBO measured in bytes
258
uint32_t start = 0;
259
uint32_t instance_count = 0;
260
uint32_t instance_buffer_index = 0;
261
262
RID tex;
263
RS::CanvasItemTextureFilter filter = RS::CANVAS_ITEM_TEXTURE_FILTER_MAX;
264
RS::CanvasItemTextureRepeat repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX;
265
266
GLES3::CanvasShaderData::BlendMode blend_mode = GLES3::CanvasShaderData::BLEND_MODE_MIX;
267
Color blend_color = Color(1.0, 1.0, 1.0, 1.0);
268
269
Item *clip = nullptr;
270
271
RID material;
272
GLES3::CanvasMaterialData *material_data = nullptr;
273
uint64_t vertex_input_mask = RS::ARRAY_FORMAT_VERTEX | RS::ARRAY_FORMAT_COLOR | RS::ARRAY_FORMAT_TEX_UV;
274
uint64_t specialization = 0;
275
276
const Item::Command *command = nullptr;
277
Item::Command::Type command_type = Item::Command::TYPE_ANIMATION_SLICE; // Can default to any type that doesn't form a batch.
278
uint32_t primitive_points = 0;
279
280
uint32_t flags = 0;
281
uint32_t specular_shininess = 0.0;
282
283
bool lights_disabled = false;
284
};
285
286
// DataBuffer contains our per-frame data. I.e. the resources that are updated each frame.
287
// We track them and ensure that they don't get reused until at least 2 frames have passed
288
// to avoid the GPU stalling to wait for a resource to become available.
289
struct DataBuffer {
290
Vector<GLuint> instance_buffers;
291
GLuint light_ubo = 0;
292
GLuint state_ubo = 0;
293
uint64_t last_frame_used = -3;
294
GLsync fence = GLsync();
295
};
296
297
struct State {
298
LocalVector<DataBuffer> canvas_instance_data_buffers;
299
LocalVector<Batch> canvas_instance_batches;
300
uint32_t current_data_buffer_index = 0;
301
uint32_t current_instance_buffer_index = 0;
302
uint32_t current_batch_index = 0;
303
uint32_t last_item_index = 0;
304
305
InstanceData *instance_data_array = nullptr;
306
307
LightUniform *light_uniforms = nullptr;
308
309
GLuint shadow_texture = 0;
310
GLuint shadow_depth_buffer = 0;
311
GLuint shadow_fb = 0;
312
int shadow_texture_size = 2048;
313
314
bool using_directional_lights = false;
315
316
RID current_tex;
317
RS::CanvasItemTextureFilter current_filter_mode = RS::CANVAS_ITEM_TEXTURE_FILTER_MAX;
318
RS::CanvasItemTextureRepeat current_repeat_mode = RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX;
319
320
bool transparent_render_target = false;
321
322
double time = 0.0;
323
324
RS::CanvasItemTextureFilter default_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
325
RS::CanvasItemTextureRepeat default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
326
} state;
327
328
Item *items[MAX_RENDER_ITEMS];
329
330
RID default_canvas_texture;
331
RID default_canvas_group_material;
332
RID default_canvas_group_shader;
333
RID default_clip_children_material;
334
RID default_clip_children_shader;
335
336
typedef void Texture;
337
338
void canvas_begin(RID p_to_render_target, bool p_to_backbuffer, bool p_backbuffer_has_mipmaps);
339
340
//virtual void draw_window_margins(int *black_margin, RID *black_image) override;
341
void draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample);
342
343
void reset_canvas();
344
345
RID light_create() override;
346
void light_set_texture(RID p_rid, RID p_texture) override;
347
void light_set_use_shadow(RID p_rid, bool p_enable) override;
348
void light_update_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders, const Rect2 &p_light_rect) override;
349
void light_update_directional_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_cull_distance, const Rect2 &p_clip_rect, LightOccluderInstance *p_occluders) override;
350
351
void render_sdf(RID p_render_target, LightOccluderInstance *p_occluders) override;
352
RID occluder_polygon_create() override;
353
void occluder_polygon_set_shape(RID p_occluder, const Vector<Vector2> &p_points, bool p_closed) override;
354
void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) override;
355
void set_shadow_texture_size(int p_size) override;
356
357
bool free(RID p_rid) override;
358
void update() override;
359
360
void _bind_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat);
361
void _prepare_canvas_texture(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, uint32_t &r_index, Size2 &r_texpixel_size);
362
363
void canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, Light *p_directional_list, const Transform2D &p_canvas_transform, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel, bool &r_sdf_used, RenderingMethod::RenderInfo *r_render_info = nullptr) override;
364
void _render_items(RID p_to_render_target, int p_item_count, const Transform2D &p_canvas_transform_inverse, Light *p_lights, bool &r_sdf_used, bool p_to_backbuffer = false, RenderingMethod::RenderInfo *r_render_info = nullptr, bool p_backbuffer_has_mipmaps = false);
365
void _record_item_commands(const Item *p_item, RID p_render_target, const Transform2D &p_canvas_transform_inverse, Item *&current_clip, GLES3::CanvasShaderData::BlendMode p_blend_mode, Light *p_lights, uint32_t &r_index, bool &r_break_batch, bool &r_sdf_used, const Point2 &p_repeat_offset);
366
void _render_batch(Light *p_lights, uint32_t p_index, RenderingMethod::RenderInfo *r_render_info = nullptr);
367
bool _bind_material(GLES3::CanvasMaterialData *p_material_data, CanvasShaderGLES3::ShaderVariant p_variant, uint64_t p_specialization);
368
void _new_batch(bool &r_batch_broken);
369
void _add_to_batch(uint32_t &r_index, bool &r_batch_broken);
370
void _allocate_instance_data_buffer();
371
void _allocate_instance_buffer();
372
void _enable_attributes(uint32_t p_start, bool p_primitive, uint32_t p_rate = 1);
373
374
void set_time(double p_time);
375
376
virtual void set_debug_redraw(bool p_enabled, double p_time, const Color &p_color) override {
377
if (p_enabled) {
378
WARN_PRINT_ONCE("Debug CanvasItem Redraw is not available yet when using the Compatibility renderer.");
379
}
380
}
381
382
virtual uint32_t get_pipeline_compilations(RS::PipelineSource p_source) override { return 0; }
383
384
static RasterizerCanvasGLES3 *get_singleton();
385
RasterizerCanvasGLES3();
386
~RasterizerCanvasGLES3();
387
};
388
389
#endif // GLES3_ENABLED
390
391