Using buffer reference instead of un-sized array

This commit is contained in:
mklefrancois 2021-06-11 12:25:06 +02:00
parent e3a57e6d63
commit c8a0122dd6
248 changed files with 2593 additions and 2660 deletions

View file

@ -16,13 +16,17 @@
* SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#version 460
#extension GL_EXT_ray_tracing : require
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_EXT_scalar_block_layout : enable
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_buffer_reference2 : require
#include "binding.glsl"
#include "gltf.glsl"
#include "raycommon.glsl"
@ -36,14 +40,18 @@ layout(location = 1) rayPayloadEXT bool isShadowed;
layout(set = 0, binding = 0 ) uniform accelerationStructureEXT topLevelAS;
layout(set = 0, binding = 2) readonly buffer _InstanceInfo {PrimMeshInfo primInfo[];};
layout(set = 1, binding = B_VERTICES) readonly buffer _VertexBuf {float vertices[];};
layout(set = 1, binding = B_INDICES) readonly buffer _Indices {uint indices[];};
layout(set = 1, binding = B_NORMALS) readonly buffer _NormalBuf {float normals[];};
layout(set = 1, binding = B_TEXCOORDS) readonly buffer _TexCoordBuf {float texcoord0[];};
layout(set = 1, binding = B_MATERIALS) readonly buffer _MaterialBuffer {GltfShadeMaterial materials[];};
layout(set = 1, binding = B_TEXTURES) uniform sampler2D texturesMap[]; // all textures
//layout(set = 1, binding = B_MATERIALS) readonly buffer _MaterialBuffer {GltfShadeMaterial materials[];};
layout(buffer_reference, scalar) readonly buffer Vertices { vec3 v[]; };
layout(buffer_reference, scalar) readonly buffer Indices { uint i[]; };
layout(buffer_reference, scalar) readonly buffer Normals { vec3 n[]; };
layout(buffer_reference, scalar) readonly buffer TexCoords { vec2 t[]; };
layout(buffer_reference, scalar) readonly buffer Materials { GltfShadeMaterial m[]; };
layout(set = 1, binding = B_SCENEDESC ) readonly buffer SceneDesc_ { SceneDesc sceneDesc; };
layout(set = 1, binding = B_TEXTURES) uniform sampler2D texturesMap[]; // all textures
// clang-format on
layout(push_constant) uniform Constants
@ -55,33 +63,6 @@ layout(push_constant) uniform Constants
}
pushC;
// Return the vertex position
vec3 getVertex(uint index)
{
vec3 vp;
vp.x = vertices[3 * index + 0];
vp.y = vertices[3 * index + 1];
vp.z = vertices[3 * index + 2];
return vp;
}
vec3 getNormal(uint index)
{
vec3 vp;
vp.x = normals[3 * index + 0];
vp.y = normals[3 * index + 1];
vp.z = normals[3 * index + 2];
return vp;
}
vec2 getTexCoord(uint index)
{
vec2 vp;
vp.x = texcoord0[2 * index + 0];
vp.y = texcoord0[2 * index + 1];
return vp;
}
void main()
{
@ -93,33 +74,39 @@ void main()
uint vertexOffset = pinfo.vertexOffset; // Vertex offset as defined in glTF
uint matIndex = max(0, pinfo.materialIndex); // material of primitive mesh
Materials gltfMat = Materials(sceneDesc.materialAddress);
Vertices vertices = Vertices(sceneDesc.vertexAddress);
Indices indices = Indices(sceneDesc.indexAddress);
Normals normals = Normals(sceneDesc.normalAddress);
TexCoords texCoords = TexCoords(sceneDesc.uvAddress);
Materials materials = Materials(sceneDesc.materialAddress);
// Getting the 3 indices of the triangle (local)
ivec3 triangleIndex = ivec3(indices[nonuniformEXT(indexOffset + 0)], //
indices[nonuniformEXT(indexOffset + 1)], //
indices[nonuniformEXT(indexOffset + 2)]);
ivec3 triangleIndex = ivec3(indices.i[indexOffset + 0], indices.i[indexOffset + 1], indices.i[indexOffset + 2]);
triangleIndex += ivec3(vertexOffset); // (global)
const vec3 barycentrics = vec3(1.0 - attribs.x - attribs.y, attribs.x, attribs.y);
// Vertex of the triangle
const vec3 pos0 = getVertex(triangleIndex.x);
const vec3 pos1 = getVertex(triangleIndex.y);
const vec3 pos2 = getVertex(triangleIndex.z);
const vec3 pos0 = vertices.v[triangleIndex.x];
const vec3 pos1 = vertices.v[triangleIndex.y];
const vec3 pos2 = vertices.v[triangleIndex.z];
const vec3 position = pos0 * barycentrics.x + pos1 * barycentrics.y + pos2 * barycentrics.z;
const vec3 world_position = vec3(gl_ObjectToWorldEXT * vec4(position, 1.0));
// Normal
const vec3 nrm0 = getNormal(triangleIndex.x);
const vec3 nrm1 = getNormal(triangleIndex.y);
const vec3 nrm2 = getNormal(triangleIndex.z);
vec3 normal = normalize(nrm0 * barycentrics.x + nrm1 * barycentrics.y + nrm2 * barycentrics.z);
const vec3 nrm0 = normals.n[triangleIndex.x];
const vec3 nrm1 = normals.n[triangleIndex.y];
const vec3 nrm2 = normals.n[triangleIndex.z];
vec3 normal = normalize(nrm0 * barycentrics.x + nrm1 * barycentrics.y + nrm2 * barycentrics.z);
const vec3 world_normal = normalize(vec3(normal * gl_WorldToObjectEXT));
const vec3 geom_normal = normalize(cross(pos1 - pos0, pos2 - pos0));
// TexCoord
const vec2 uv0 = getTexCoord(triangleIndex.x);
const vec2 uv1 = getTexCoord(triangleIndex.y);
const vec2 uv2 = getTexCoord(triangleIndex.z);
const vec2 uv0 = texCoords.t[triangleIndex.x];
const vec2 uv1 = texCoords.t[triangleIndex.y];
const vec2 uv2 = texCoords.t[triangleIndex.z];
const vec2 texcoord0 = uv0 * barycentrics.x + uv1 * barycentrics.y + uv2 * barycentrics.z;
// Vector toward the light
@ -140,7 +127,7 @@ void main()
}
// Material of the object
GltfShadeMaterial mat = materials[nonuniformEXT(matIndex)];
GltfShadeMaterial mat = materials.m[matIndex];
// Diffuse
vec3 diffuse = computeDiffuse(mat, L, world_normal);
@ -160,9 +147,8 @@ void main()
float tMax = lightDistance;
vec3 origin = gl_WorldRayOriginEXT + gl_WorldRayDirectionEXT * gl_HitTEXT;
vec3 rayDir = L;
uint flags = gl_RayFlagsTerminateOnFirstHitEXT | gl_RayFlagsOpaqueEXT
| gl_RayFlagsSkipClosestHitShaderEXT;
isShadowed = true;
uint flags = gl_RayFlagsTerminateOnFirstHitEXT | gl_RayFlagsOpaqueEXT | gl_RayFlagsSkipClosestHitShaderEXT;
isShadowed = true;
traceRayEXT(topLevelAS, // acceleration structure
flags, // rayFlags
0xFF, // cullMask