20 #ifndef _VERTEX_BUFFER_H_
21 #define _VERTEX_BUFFER_H_
23 #define MAX_VERT_BSP (1 << 21)
25 #define MAX_VERT_MODEL (1 << 22)
26 #define MAX_IDX_MODEL (1 << 22)
27 #define MAX_PRIM_MODEL (MAX_IDX_MODEL / 3)
29 #define MAX_VERT_INSTANCED (1 << 21)
30 #define MAX_IDX_INSTANCED (MAX_VERT_INSTANCED / 3)
32 #define MAX_LIGHT_LISTS (1 << 14)
33 #define MAX_LIGHT_LIST_NODES (1 << 19)
35 #define MAX_LIGHT_POLYS 4096
36 #define LIGHT_POLY_VEC4S 4
39 #define MAX_PBR_MATERIALS 4096
41 #define LIGHT_TEXTURE_SCALE 0
43 #define ALIGN_SIZE_4(x, n) ((x * n + 3) & (~3))
45 #define VERTEX_BUFFER_BINDING_IDX 0
46 #define LIGHT_BUFFER_BINDING_IDX 1
47 #define READBACK_BUFFER_BINDING_IDX 2
48 #define TONE_MAPPING_BUFFER_BINDING_IDX 3
49 #define SUN_COLOR_BUFFER_BINDING_IDX 4
50 #define SUN_COLOR_UBO_BINDING_IDX 5
51 #define LIGHT_STATS_BUFFER_BINDING_IDX 6
53 #define SUN_COLOR_ACCUMULATOR_FIXED_POINT_SCALE 0x100000
54 #define SKY_COLOR_ACCUMULATOR_FIXED_POINT_SCALE 0x100
60 #define VERTEX_BUFFER_LIST \
61 VERTEX_BUFFER_LIST_DO(float, 3, positions_bsp, (MAX_VERT_BSP )) \
62 VERTEX_BUFFER_LIST_DO(float, 2, tex_coords_bsp, (MAX_VERT_BSP )) \
63 VERTEX_BUFFER_LIST_DO(float, 3, tangents_bsp, (MAX_VERT_BSP / 3 )) \
64 VERTEX_BUFFER_LIST_DO(uint32_t, 1, materials_bsp, (MAX_VERT_BSP / 3 )) \
65 VERTEX_BUFFER_LIST_DO(uint32_t, 1, clusters_bsp, (MAX_VERT_BSP / 3 )) \
66 VERTEX_BUFFER_LIST_DO(float, 1, texel_density_bsp, (MAX_VERT_BSP / 3 )) \
68 VERTEX_BUFFER_LIST_DO(float, 3, positions_model, (MAX_VERT_MODEL )) \
69 VERTEX_BUFFER_LIST_DO(float, 3, normals_model, (MAX_VERT_MODEL )) \
70 VERTEX_BUFFER_LIST_DO(float, 2, tex_coords_model, (MAX_VERT_MODEL )) \
71 VERTEX_BUFFER_LIST_DO(float, 4, tangents_model, (MAX_VERT_MODEL )) \
72 VERTEX_BUFFER_LIST_DO(uint32_t, 3, idx_model, (MAX_IDX_MODEL )) \
74 VERTEX_BUFFER_LIST_DO(float, 3, positions_instanced, (MAX_VERT_MODEL )) \
75 VERTEX_BUFFER_LIST_DO(float, 3, pos_prev_instanced, (MAX_VERT_MODEL )) \
76 VERTEX_BUFFER_LIST_DO(float, 3, normals_instanced, (MAX_VERT_MODEL )) \
77 VERTEX_BUFFER_LIST_DO(float, 3, tangents_instanced, (MAX_PRIM_MODEL )) \
78 VERTEX_BUFFER_LIST_DO(float, 2, tex_coords_instanced, (MAX_VERT_MODEL )) \
79 VERTEX_BUFFER_LIST_DO(float, 1, alpha_instanced, (MAX_PRIM_MODEL )) \
80 VERTEX_BUFFER_LIST_DO(uint32_t, 1, clusters_instanced, (MAX_PRIM_MODEL )) \
81 VERTEX_BUFFER_LIST_DO(uint32_t, 1, materials_instanced, (MAX_PRIM_MODEL )) \
82 VERTEX_BUFFER_LIST_DO(uint32_t, 1, instance_id_instanced, (MAX_PRIM_MODEL )) \
83 VERTEX_BUFFER_LIST_DO(float, 1, texel_density_instanced, (MAX_PRIM_MODEL )) \
85 VERTEX_BUFFER_LIST_DO(uint32_t, 1, sky_visibility, (MAX_LIGHT_LISTS / 32)) \
88 #define LIGHT_BUFFER_LIST \
89 LIGHT_BUFFER_LIST_DO(uint32_t, 4, material_table, (MAX_PBR_MATERIALS)) \
90 LIGHT_BUFFER_LIST_DO(float, 4, light_polys, (MAX_LIGHT_POLYS * LIGHT_POLY_VEC4S)) \
91 LIGHT_BUFFER_LIST_DO(uint32_t, 1, light_list_offsets, (MAX_LIGHT_LISTS )) \
92 LIGHT_BUFFER_LIST_DO(uint32_t, 1, light_list_lights, (MAX_LIGHT_LIST_NODES)) \
93 LIGHT_BUFFER_LIST_DO(float, 1, light_styles, (MAX_LIGHT_STYLES )) \
94 LIGHT_BUFFER_LIST_DO(uint32_t, 1, cluster_debug_mask, (MAX_LIGHT_LISTS / 32)) \
98 #define VERTEX_BUFFER_LIST_DO(type, dim, name, size) \
99 type name[ALIGN_SIZE_4(size, dim)];
103 #undef VERTEX_BUFFER_LIST_DO
108 #define LIGHT_BUFFER_LIST_DO(type, dim, name, size) \
109 type name[ALIGN_SIZE_4(size, dim)];
113 #undef LIGHT_BUFFER_LIST_DO
129 #define ivec3_t ivec3
130 #define ivec4_t ivec4
170 uint diffuse_texture;
171 uint normals_texture;
172 uint emissive_texture;
174 float roughness_override;
175 float specular_scale;
176 float emissive_scale;
177 float light_style_scale;
186 float light_style_scale;
187 float prev_style_scale;
190 #ifdef VERTEX_READONLY
222 } light_stats_bufers[3];
225 #define GET_float_1(buf,name) \
227 get_##name(uint idx) \
229 return buf.name[idx]; \
232 #define GET_float_2(buf,name) \
234 get_##name(uint idx) \
236 return vec2(buf.name[idx * 2 + 0], buf.name[idx * 2 + 1]); \
239 #define GET_float_3(buf,name) \
241 get_##name(uint idx) \
243 return vec3(buf.name[idx * 3 + 0], buf.name[idx * 3 + 1], buf.name[idx * 3 + 2]); \
246 #define GET_float_4(buf,name) \
248 get_##name(uint idx) \
250 return vec4(buf.name[idx * 4 + 0], buf.name[idx * 4 + 1], buf.name[idx * 4 + 2], buf.name[idx * 4 + 3]); \
253 #define GET_uint32_t_1(buf,name) \
255 get_##name(uint idx) \
257 return buf.name[idx]; \
260 #define GET_uint32_t_3(buf,name) \
262 get_##name(uint idx) \
264 return uvec3(buf.name[idx * 3 + 0], buf.name[idx * 3 + 1], buf.name[idx * 3 + 2]); \
267 #define GET_uint32_t_4(buf,name) \
269 get_##name(uint idx) \
271 return uvec4(buf.name[idx * 4 + 0], buf.name[idx * 4 + 1], buf.name[idx * 4 + 2], buf.name[idx * 4 + 3]); \
274 #ifndef VERTEX_READONLY
275 #define SET_float_1(buf,name) \
277 set_##name(uint idx, float v) \
282 #define SET_float_2(buf,name) \
284 set_##name(uint idx, vec2 v) \
286 buf.name[idx * 2 + 0] = v[0]; \
287 buf.name[idx * 2 + 1] = v[1]; \
290 #define SET_float_3(buf,name) \
292 set_##name(uint idx, vec3 v) \
294 buf.name[idx * 3 + 0] = v[0]; \
295 buf.name[idx * 3 + 1] = v[1]; \
296 buf.name[idx * 3 + 2] = v[2]; \
299 #define SET_float_4(buf,name) \
301 set_##name(uint idx, vec4 v) \
303 buf.name[idx * 4 + 0] = v[0]; \
304 buf.name[idx * 4 + 1] = v[1]; \
305 buf.name[idx * 4 + 2] = v[2]; \
306 buf.name[idx * 4 + 3] = v[3]; \
309 #define SET_uint32_t_1(buf,name) \
311 set_##name(uint idx, uint u) \
316 #define SET_uint32_t_3(buf,name) \
318 set_##name(uint idx, uvec3 v) \
320 buf.name[idx * 3 + 0] = v[0]; \
321 buf.name[idx * 3 + 1] = v[1]; \
322 buf.name[idx * 3 + 2] = v[2]; \
326 #ifdef VERTEX_READONLY
327 #define VERTEX_BUFFER_LIST_DO(type, dim, name, size) \
328 GET_##type##_##dim(vbo,name)
330 #undef VERTEX_BUFFER_LIST_DO
332 #define VERTEX_BUFFER_LIST_DO(type, dim, name, size) \
333 GET_##type##_##dim(vbo,name) \
334 SET_##type##_##dim(vbo,name)
336 #undef VERTEX_BUFFER_LIST_DO
339 #define LIGHT_BUFFER_LIST_DO(type, dim, name, size) \
340 GET_##type##_##dim(lbo,name)
342 #undef LIGHT_BUFFER_LIST_DO
347 mat3x3 positions_prev;
358 get_bsp_triangle(
uint prim_id)
361 t.positions[0] = get_positions_bsp(prim_id * 3 + 0);
362 t.positions[1] = get_positions_bsp(prim_id * 3 + 1);
363 t.positions[2] = get_positions_bsp(prim_id * 3 + 2);
365 t.positions_prev = t.positions;
367 vec3 normal = normalize(cross(
368 t.positions[1] - t.positions[0],
369 t.positions[2] - t.positions[0]));
371 t.normals[0] = normal;
372 t.normals[1] = normal;
373 t.normals[2] = normal;
375 t.tex_coords[0] = get_tex_coords_bsp(prim_id * 3 + 0);
376 t.tex_coords[1] = get_tex_coords_bsp(prim_id * 3 + 1);
377 t.tex_coords[2] = get_tex_coords_bsp(prim_id * 3 + 2);
379 t.tangent = get_tangents_bsp(prim_id);
381 t.material_id = get_materials_bsp(prim_id);
383 t.cluster = get_clusters_bsp(prim_id);
385 t.texel_density = get_texel_density_bsp(prim_id);
393 get_model_triangle(
uint prim_id,
uint idx_offset,
uint vert_offset)
395 uvec3 idx = get_idx_model(prim_id + idx_offset / 3);
399 t.positions[0] = get_positions_model(idx[0]);
400 t.positions[1] = get_positions_model(idx[1]);
401 t.positions[2] = get_positions_model(idx[2]);
403 t.normals[0] = get_normals_model(idx[0]);
404 t.normals[1] = get_normals_model(idx[1]);
405 t.normals[2] = get_normals_model(idx[2]);
407 t.tex_coords[0] = get_tex_coords_model(idx[0]);
408 t.tex_coords[1] = get_tex_coords_model(idx[1]);
409 t.tex_coords[2] = get_tex_coords_model(idx[2]);
411 vec4 tangent = get_tangents_model(idx[0]);
412 t.tangent = tangent.xyz;
425 get_instanced_triangle(
uint prim_id)
428 t.positions[0] = get_positions_instanced(prim_id * 3 + 0);
429 t.positions[1] = get_positions_instanced(prim_id * 3 + 1);
430 t.positions[2] = get_positions_instanced(prim_id * 3 + 2);
432 t.positions_prev[0] = get_pos_prev_instanced(prim_id * 3 + 0);
433 t.positions_prev[1] = get_pos_prev_instanced(prim_id * 3 + 1);
434 t.positions_prev[2] = get_pos_prev_instanced(prim_id * 3 + 2);
436 t.normals[0] = get_normals_instanced(prim_id * 3 + 0);
437 t.normals[1] = get_normals_instanced(prim_id * 3 + 1);
438 t.normals[2] = get_normals_instanced(prim_id * 3 + 2);
440 t.tangent = get_tangents_instanced(prim_id);
442 t.tex_coords[0] = get_tex_coords_instanced(prim_id * 3 + 0);
443 t.tex_coords[1] = get_tex_coords_instanced(prim_id * 3 + 1);
444 t.tex_coords[2] = get_tex_coords_instanced(prim_id * 3 + 2);
446 t.material_id = get_materials_instanced(prim_id);
448 t.cluster = get_clusters_instanced(prim_id);
450 t.alpha = get_alpha_instanced(prim_id);
452 t.texel_density = get_texel_density_instanced(prim_id);
457 #ifndef VERTEX_READONLY
459 store_instanced_triangle(Triangle t,
uint instance_id,
uint prim_id)
461 set_positions_instanced(prim_id * 3 + 0, t.positions[0]);
462 set_positions_instanced(prim_id * 3 + 1, t.positions[1]);
463 set_positions_instanced(prim_id * 3 + 2, t.positions[2]);
465 set_pos_prev_instanced(prim_id * 3 + 0, t.positions_prev[0]);
466 set_pos_prev_instanced(prim_id * 3 + 1, t.positions_prev[1]);
467 set_pos_prev_instanced(prim_id * 3 + 2, t.positions_prev[2]);
469 set_normals_instanced(prim_id * 3 + 0, t.normals[0]);
470 set_normals_instanced(prim_id * 3 + 1, t.normals[1]);
471 set_normals_instanced(prim_id * 3 + 2, t.normals[2]);
473 set_tangents_instanced(prim_id, t.tangent);
475 set_tex_coords_instanced(prim_id * 3 + 0, t.tex_coords[0]);
476 set_tex_coords_instanced(prim_id * 3 + 1, t.tex_coords[1]);
477 set_tex_coords_instanced(prim_id * 3 + 2, t.tex_coords[2]);
479 set_materials_instanced(prim_id, t.material_id);
481 set_instance_id_instanced(prim_id, instance_id);
483 set_clusters_instanced(prim_id, t.cluster);
485 set_alpha_instanced(prim_id, t.alpha);
487 set_texel_density_instanced(prim_id, t.texel_density);
492 get_material_info(
uint material_id)
497 minfo.diffuse_texture = data.x & 0xffff;
498 minfo.normals_texture = data.x >> 16;
499 minfo.emissive_texture = data.y & 0xffff;
500 minfo.num_frames = (data.y >> 28) & 0x000f;
501 minfo.next_frame = (data.y >> 16) & 0x0fff;
502 minfo.bump_scale = unpackHalf2x16(data.z).x;
503 minfo.roughness_override = unpackHalf2x16(data.z).y;
504 minfo.specular_scale = unpackHalf2x16(data.w).x;
505 minfo.emissive_scale = unpackHalf2x16(data.w).y;
514 minfo.emissive_scale *= get_light_styles(light_style);
522 get_light_polygon(
uint index)
530 light.positions = mat3x3(p0.xyz, p1.xyz, p2.xyz);
531 light.color = vec3(p0.w, p1.w, p2.w);
532 light.light_style_scale = p3.x;
533 light.prev_style_scale = p3.y;