summaryrefslogtreecommitdiff
path: root/drivers/gles3/rasterizer_scene_gles3.h
blob: 24b6a36fcca811b045b3e20330033aeafbb29806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#ifndef RASTERIZERSCENEGLES3_H
#define RASTERIZERSCENEGLES3_H

#include "rasterizer_storage_gles3.h"
#include "drivers/gles3/shaders/scene.glsl.h"
#include "drivers/gles3/shaders/cube_to_dp.glsl.h"

class RasterizerSceneGLES3 : public RasterizerScene {
public:

	enum ShadowFilterMode {
		SHADOW_FILTER_NEAREST,
		SHADOW_FILTER_PCF5,
		SHADOW_FILTER_PCF13,
	};


	ShadowFilterMode shadow_filter_mode;

	uint64_t shadow_atlas_realloc_tolerance_msec;


	uint64_t render_pass;
	uint64_t scene_pass;
	uint32_t current_material_index;
	uint32_t current_geometry_index;

	RID default_material;
	RID default_material_twosided;
	RID default_shader;
	RID default_shader_twosided;

	RasterizerStorageGLES3 *storage;


	struct State {



		bool texscreen_copied;
		int current_blend_mode;
		float current_line_width;
		int current_depth_draw;

		SceneShaderGLES3 scene_shader;
		CubeToDpShaderGLES3 cube_to_dp_shader;


		struct SceneDataUBO {

			float projection_matrix[16];
			float camera_inverse_matrix[16];
			float camera_matrix[16];
			float time[4];
			float ambient_light_color[4];
			float bg_color[4];
			float ambient_energy;
			float bg_energy;
			float shadow_z_offset;
			float shadow_slope_scale;
			float shadow_dual_paraboloid_render_zfar;
			float shadow_dual_paraboloid_render_side;
			float shadow_atlas_pixel_size[2];
			float shadow_directional_pixel_size[2];

		} ubo_data;

		GLuint scene_ubo;

		struct EnvironmentRadianceUBO {

			float transform[16];
			float box_min[4]; //unused for now
			float box_max[4];
			float ambient_contribution;

		} env_radiance_data;

		GLuint env_radiance_ubo;

		GLuint brdf_texture;

		GLuint skybox_verts;
		GLuint skybox_array;

		GLuint directional_ubo;

		GLuint spot_array_ubo;
		GLuint omni_array_ubo;

		uint32_t ubo_light_size;
		uint8_t *spot_array_tmp;
		uint8_t *omni_array_tmp;

		int max_ubo_lights;
		int max_forward_lights_per_object;

		int spot_light_count;
		int omni_light_count;
		int directional_light_count;

		bool cull_front;

	} state;

	/* SHADOW ATLAS API */

	struct ShadowAtlas : public RID_Data {

		enum {
			QUADRANT_SHIFT=27,
			SHADOW_INDEX_MASK=(1<<QUADRANT_SHIFT)-1,
			SHADOW_INVALID=0xFFFFFFFF
		};

		struct Quadrant {

			uint32_t subdivision;

			struct Shadow {
				RID owner;
				uint64_t version;
				uint64_t alloc_tick;

				Shadow() {
					version=0;
					alloc_tick=0;
				}
			};

			Vector<Shadow> shadows;

			Quadrant() {
				subdivision=0; //not in use
			}

		} quadrants[4];

		int size_order[4];
		uint32_t smallest_subdiv;

		int size;

		GLuint fbo;
		GLuint depth;

		Map<RID,uint32_t> shadow_owners;
	};

	struct ShadowCubeMap {

		GLuint fbo_id[6];
		GLuint cubemap;
		int size;
	};

	Vector<ShadowCubeMap> shadow_cubemaps;

	RID_Owner<ShadowAtlas> shadow_atlas_owner;

	RID shadow_atlas_create();
	void shadow_atlas_set_size(RID p_atlas,int p_size);
	void shadow_atlas_set_quadrant_subdivision(RID p_atlas,int p_quadrant,int p_subdivision);
	bool _shadow_atlas_find_shadow(ShadowAtlas *shadow_atlas, int *p_in_quadrants, int p_quadrant_count, int p_current_subdiv, uint64_t p_tick, int &r_quadrant, int &r_shadow);
	bool shadow_atlas_update_light(RID p_atlas,RID p_light_intance,float p_coverage,uint64_t p_light_version);


	struct DirectionalShadow {
		GLuint fbo;
		GLuint depth;
		int light_count;
		int size;
		int current_light;
	} directional_shadow;

	virtual int get_directional_light_shadow_size(RID p_light_intance);
	virtual void set_directional_shadow_count(int p_count);


	/* ENVIRONMENT API */

	struct Environment : public RID_Data {

		VS::EnvironmentBG bg_mode;

		RID skybox_color;
		RID skybox_radiance;
		float skybox_scale;

		Color bg_color;
		float bg_energy;
		float skybox_ambient;

		Color ambient_color;
		float ambient_energy;
		float ambient_skybox_contribution;

		int canvas_max_layer;


		Environment() {
			bg_mode=VS::ENV_BG_CLEAR_COLOR;
			skybox_scale=1.0;
			bg_energy=1.0;
			skybox_ambient=0;
			ambient_energy=1.0;
			ambient_skybox_contribution=0.0;
			canvas_max_layer=0;
		}
	};

	RID_Owner<Environment> environment_owner;

	virtual RID environment_create();

	virtual void environment_set_background(RID p_env,VS::EnvironmentBG p_bg);
	virtual void environment_set_skybox(RID p_env,RID p_skybox,int p_radiance_size);
	virtual void environment_set_skybox_scale(RID p_env,float p_scale);
	virtual void environment_set_bg_color(RID p_env,const Color& p_color);
	virtual void environment_set_bg_energy(RID p_env,float p_energy);
	virtual void environment_set_canvas_max_layer(RID p_env,int p_max_layer);
	virtual void environment_set_ambient_light(RID p_env,const Color& p_color,float p_energy=1.0,float p_skybox_contribution=0.0);

	virtual void environment_set_glow(RID p_env,bool p_enable,int p_radius,float p_intensity,float p_strength,float p_bloom_treshold,VS::EnvironmentGlowBlendMode p_blend_mode);
	virtual void environment_set_fog(RID p_env,bool p_enable,float p_begin,float p_end,RID p_gradient_texture);

	virtual void environment_set_tonemap(RID p_env,bool p_enable,float p_exposure,float p_white,float p_min_luminance,float p_max_luminance,float p_auto_exp_speed,float p_auto_exp_scale,VS::EnvironmentToneMapper p_tone_mapper);
	virtual void environment_set_adjustment(RID p_env,bool p_enable,float p_brightness,float p_contrast,float p_saturation,RID p_ramp);


	/* LIGHT INSTANCE */

	struct LightDataUBO {

		float light_pos_inv_radius[4];
		float light_direction_attenuation[4];
		float light_color_energy[4];
		float light_params[4]; //spot attenuation, spot angle, specular, shadow enabled
		float light_clamp[4];
		float light_shadow_color[4];
		float shadow_matrix1[16]; //up to here for spot and omni, rest is for directional
		float shadow_matrix2[16];
		float shadow_matrix3[16];
		float shadow_matrix4[16];
		float shadow_split_offsets[4];

	};

	struct LightInstance : public RID_Data {

		struct ShadowTransform {

			CameraMatrix camera;
			Transform transform;
			float far;
			float split;
		};



		ShadowTransform shadow_transform[4];

		RID self;
		RID light;
		RasterizerStorageGLES3::Light *light_ptr;
		Transform transform;

		Vector3 light_vector;
		Vector3 spot_vector;
		float linear_att;

		uint64_t shadow_pass;
		uint64_t last_scene_pass;
		uint64_t last_scene_shadow_pass;
		uint64_t last_pass;
		uint16_t light_index;
		uint16_t light_directional_index;

		uint32_t current_shadow_atlas_key;

		Vector2 dp;

		Rect2 directional_rect;


		Set<RID> shadow_atlases; //shadow atlases where this light is registered

		LightInstance() { }

	};

	mutable RID_Owner<LightInstance> light_instance_owner;

	virtual RID light_instance_create(RID p_light);
	virtual void light_instance_set_transform(RID p_light_instance,const Transform& p_transform);
	virtual void light_instance_set_shadow_transform(RID p_light_instance,const CameraMatrix& p_projection,const Transform& p_transform,float p_far,float p_split,int p_pass);
	virtual void light_instance_mark_visible(RID p_light_instance);

	/* RENDER LIST */

	struct RenderList {

		enum {
			DEFAULT_MAX_ELEMENTS=65536,
			SORT_FLAG_SKELETON=1,
			SORT_FLAG_INSTANCING=2,
			MAX_DIRECTIONAL_LIGHTS=16,
			MAX_LIGHTS=4096,


			SORT_KEY_DEPTH_LAYER_SHIFT=60,
			SORT_KEY_UNSHADED_FLAG=uint64_t(1)<<59,
			SORT_KEY_NO_DIRECTIONAL_FLAG=uint64_t(1)<<58,
			SORT_KEY_SHADING_SHIFT=58,
			SORT_KEY_SHADING_MASK=3,
			SORT_KEY_MATERIAL_INDEX_SHIFT=40,
			SORT_KEY_GEOMETRY_INDEX_SHIFT=20,
			SORT_KEY_GEOMETRY_TYPE_SHIFT=15,
			SORT_KEY_SKELETON_FLAG=2,
			SORT_KEY_MIRROR_FLAG=1

		};

		int max_elements;

		struct Element {

			RasterizerScene::InstanceBase *instance;
			RasterizerStorageGLES3::Geometry *geometry;
			RasterizerStorageGLES3::Material *material;
			RasterizerStorageGLES3::GeometryOwner *owner;
			uint64_t sort_key;

		};


		Element *_elements;
		Element **elements;

		int element_count;
		int alpha_element_count;


		void clear() {

			element_count=0;
			alpha_element_count=0;
		}

		//should eventually be replaced by radix

		struct SortByKey {

			_FORCE_INLINE_ bool operator()(const Element* A,  const Element* B ) const {
				return A->sort_key < B->sort_key;
			}
		};

		void sort_by_key(bool p_alpha) {

			SortArray<Element*,SortByKey> sorter;
			if (p_alpha) {
				sorter.sort(&elements[max_elements-alpha_element_count],alpha_element_count);
			} else {
				sorter.sort(elements,element_count);
			}
		}

		struct SortByDepth {

			_FORCE_INLINE_ bool operator()(const Element* A,  const Element* B ) const {
				return A->instance->depth > B->instance->depth;
			}
		};

		void sort_by_depth(bool p_alpha) {

			SortArray<Element*,SortByDepth> sorter;
			if (p_alpha) {
				sorter.sort(&elements[max_elements-alpha_element_count],alpha_element_count);
			} else {
				sorter.sort(elements,element_count);
			}
		}


		_FORCE_INLINE_ Element* add_element() {

			if (element_count+alpha_element_count>=max_elements)
				return NULL;
			elements[element_count]=&_elements[element_count];
			return elements[element_count++];
		}

		_FORCE_INLINE_ Element* add_alpha_element() {

			if (element_count+alpha_element_count>=max_elements)
				return NULL;
			int idx = max_elements-alpha_element_count-1;
			elements[idx]=&_elements[idx];
			alpha_element_count++;
			return elements[idx];
		}

		void init() {

			element_count = 0;
			alpha_element_count =0;
			elements=memnew_arr(Element*,max_elements);
			_elements=memnew_arr(Element,max_elements);
			for (int i=0;i<max_elements;i++)
				elements[i]=&_elements[i]; // assign elements

		}


		RenderList() {

			max_elements=DEFAULT_MAX_ELEMENTS;
		}

		~RenderList() {
			memdelete_arr(elements);
			memdelete_arr(_elements);
		}
	};


	LightInstance *directional_light;
	LightInstance *directional_lights[RenderList::MAX_DIRECTIONAL_LIGHTS];



	RenderList render_list;

	_FORCE_INLINE_ void _set_cull(bool p_front,bool p_reverse_cull);

	_FORCE_INLINE_ bool _setup_material(RasterizerStorageGLES3::Material* p_material,bool p_alpha_pass);
	_FORCE_INLINE_ void _setup_transform(InstanceBase *p_instance,const Transform& p_view_transform,const CameraMatrix& p_projection);
	_FORCE_INLINE_ void _setup_geometry(RenderList::Element *e);
	_FORCE_INLINE_ void _render_geometry(RenderList::Element *e);
	_FORCE_INLINE_ void _setup_light(RenderList::Element *e);

	void _render_list(RenderList::Element **p_elements, int p_element_count, const Transform& p_view_transform, const CameraMatrix& p_projection, RasterizerStorageGLES3::Texture *p_base_env, bool p_reverse_cull, bool p_alpha_pass, bool p_shadow, bool p_directional_add);


	_FORCE_INLINE_ void _add_geometry(  RasterizerStorageGLES3::Geometry* p_geometry,  InstanceBase *p_instance, RasterizerStorageGLES3::GeometryOwner *p_owner,int p_material,bool p_shadow);

	void _draw_skybox(RID p_skybox, const CameraMatrix& p_projection, const Transform& p_transform, bool p_vflip, float p_scale);

	void _setup_environment(Environment *env, const CameraMatrix &p_cam_projection, const Transform& p_cam_transform);
	void _setup_directional_light(int p_index, const Transform &p_camera_inverse_transform);
	void _setup_lights(RID *p_light_cull_result, int p_light_cull_count, const Transform &p_camera_inverse_transform, const CameraMatrix& p_camera_projection, RID p_shadow_atlas);
	void _copy_screen();
	void _copy_to_front_buffer(Environment *env);
	void _copy_texture_to_front_buffer(GLuint p_texture); //used for debug

	void _fill_render_list(InstanceBase** p_cull_result,int p_cull_count,bool p_shadow);

	virtual void render_scene(const Transform& p_cam_transform,const CameraMatrix& p_cam_projection,bool p_cam_ortogonal,InstanceBase** p_cull_result,int p_cull_count,RID* p_light_cull_result,int p_light_cull_count,RID p_environment,RID p_shadow_atlas);
	virtual void render_shadow(RID p_light,RID p_shadow_atlas,int p_pass,InstanceBase** p_cull_result,int p_cull_count);
	virtual bool free(RID p_rid);

	void _generate_brdf();

	virtual void set_scene_pass(uint64_t p_pass);

	void iteration();
	void initialize();
	void finalize();
	RasterizerSceneGLES3();
};

#endif // RASTERIZERSCENEGLES3_H