// Shared data structures for raytracing shaders.
// Requires: GL_EXT_buffer_reference, GL_ARB_gpu_shader_int64, raytracing_inc.glsl (for OFFSET_NONE/FLAG_*)
// ============================================================================
// BUFFER REFERENCES
// ============================================================================
layout(buffer_reference, std430) readonly buffer FloatBuffer {
float v[];
};
layout(buffer_reference, std430) readonly buffer Uint32Buffer {
uint v[];
};
// ============================================================================
// GEOMETRY DATA (matches C++ RT_GeometryData, 128 bytes)
// ============================================================================
struct GeometryData {
uint64_t vertex_address;
uint64_t attribute_address;
uint64_t index_address;
uint vertex_count;
uint position_stride;
uint normal_byte_offset;
uint normal_stride;
uint tangent_byte_offset;
uint tangent_stride;
uint attribute_stride;
uint uv_byte_offset;
uint uv_scale_packed; // fp16 x, fp16 y
uint index_format;
uint primitive_count;
uint flags;
float aabb_size_x;
float aabb_size_y;
float aabb_size_z;
// Byte offset of the vertex color attribute inside `attribute_stride`,
// or OFFSET_NONE if the mesh has no vertex colors.
uint color_byte_offset;
float aabb_pos_x;
float aabb_pos_y;
float aabb_pos_z;
// For deformed geometry: previous-frame position buffer used for motion vectors.
uint prev_vertex_address_lo;
uint prev_vertex_address_hi;
// CUSTOM0-3 location: bits 0-23 byte offset in attribute_stride, bits 24-31
// ARRAY_CUSTOM_* format. OFFSET_NONE when the surface has no such array.
uint custom_packed[4];
uint _pad[1];
};
void get_aabb_compression_xforms(GeometryData geom, out mat4 aabb_xform, out mat4 inv_aabb_xform) {
if ((geom.flags & FLAG_COMPRESSED) == 0u) {
aabb_xform = mat4(1.0);
inv_aabb_xform = mat4(1.0);
return;
}
vec3 aabb_sz = vec3(geom.aabb_size_x, geom.aabb_size_y, geom.aabb_size_z);
vec3 aabb_po = vec3(geom.aabb_pos_x, geom.aabb_pos_y, geom.aabb_pos_z);
aabb_xform = mat4(
vec4(aabb_sz.x, 0.0, 0.0, 0.0),
vec4(0.0, aabb_sz.y, 0.0, 0.0),
vec4(0.0, 0.0, aabb_sz.z, 0.0),
vec4(aabb_po, 1.0));
vec3 inv_sz = 1.0 / aabb_sz;
inv_aabb_xform = mat4(
vec4(inv_sz.x, 0.0, 0.0, 0.0),
vec4(0.0, inv_sz.y, 0.0, 0.0),
vec4(0.0, 0.0, inv_sz.z, 0.0),
vec4(-aabb_po * inv_sz, 1.0));
}
// ============================================================================
// PER-INSTANCE MOTION DATA (matches C++ RT_InstanceMotionData, 48 bytes)
// ============================================================================
struct InstanceMotionData {
float prev_xform[12]; // Previous object-to-world (mat3x4, transposed 3x4)
};
// ============================================================================
// MATERIAL DATA (matches C++ layout, 96 bytes)
// ============================================================================
struct MaterialData {
uint albedo_texture_idx;
uint normal_texture_idx;
uint orm_texture_idx;
uint emission_texture_idx;
vec4 albedo_color;
vec3 emission_color;
float emission_strength;
float metallic;
float roughness;
float ao_strength;
uint flags; // Bit 0: has_normal_map, Bit 1: has_emission
vec2 uv1_scale; // UV1 scale (default 1,1)
vec2 uv1_offset; // UV1 offset (default 0,0)
float normal_map_depth; // Normal map strength (default 1.0)
float specular; // Dielectric specular [0..1], default 0.5 -> F0 = 0.04.
uint64_t uniform_address; // BDA for custom shader uniform buffer (0 = none)
};