Refactoring
This commit is contained in:
parent
3e399adf0a
commit
d90ce79135
222 changed files with 9045 additions and 5734 deletions
|
|
@ -26,34 +26,26 @@
|
|||
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
|
||||
#extension GL_EXT_buffer_reference2 : require
|
||||
|
||||
#include "binding.glsl"
|
||||
#include "gltf.glsl"
|
||||
#include "host_device.h"
|
||||
|
||||
|
||||
layout(push_constant) uniform shaderInformation
|
||||
layout(push_constant) uniform _PushConstantRaster
|
||||
{
|
||||
vec3 lightPosition;
|
||||
uint instanceId;
|
||||
float lightIntensity;
|
||||
int lightType;
|
||||
int matetrialId;
|
||||
}
|
||||
pushC;
|
||||
PushConstantRaster pcRaster;
|
||||
};
|
||||
|
||||
// clang-format off
|
||||
// Incoming
|
||||
layout(location = 1) in vec2 fragTexCoord;
|
||||
layout(location = 2) in vec3 fragNormal;
|
||||
layout(location = 3) in vec3 viewDir;
|
||||
layout(location = 4) in vec3 worldPos;
|
||||
layout(location = 1) in vec3 i_worldPos;
|
||||
layout(location = 2) in vec3 i_worldNrm;
|
||||
layout(location = 3) in vec3 i_viewDir;
|
||||
layout(location = 4) in vec2 i_texCoord;
|
||||
// Outgoing
|
||||
layout(location = 0) out vec4 outColor;
|
||||
layout(location = 0) out vec4 o_color;
|
||||
// Buffers
|
||||
|
||||
layout(buffer_reference, scalar) buffer Matrices { mat4 m[]; };
|
||||
layout(buffer_reference, scalar) buffer GltfMaterial { GltfShadeMaterial m[]; };
|
||||
layout(set = 0, binding = B_SCENEDESC ) readonly buffer SceneDesc_ { SceneDesc sceneDesc; } ;
|
||||
layout(set = 0, binding = B_TEXTURES) uniform sampler2D[] textureSamplers;
|
||||
layout(set = 0, binding = eSceneDesc ) readonly buffer SceneDesc_ { SceneDesc sceneDesc; } ;
|
||||
layout(set = 0, binding = eTextures) uniform sampler2D[] textureSamplers;
|
||||
// clang-format on
|
||||
|
||||
|
||||
|
|
@ -61,23 +53,23 @@ void main()
|
|||
{
|
||||
// Material of the object
|
||||
GltfMaterial gltfMat = GltfMaterial(sceneDesc.materialAddress);
|
||||
GltfShadeMaterial mat = gltfMat.m[pushC.matetrialId];
|
||||
GltfShadeMaterial mat = gltfMat.m[pcRaster.materialId];
|
||||
|
||||
vec3 N = normalize(fragNormal);
|
||||
vec3 N = normalize(i_worldNrm);
|
||||
|
||||
// Vector toward light
|
||||
vec3 L;
|
||||
float lightIntensity = pushC.lightIntensity;
|
||||
if(pushC.lightType == 0)
|
||||
float lightIntensity = pcRaster.lightIntensity;
|
||||
if(pcRaster.lightType == 0)
|
||||
{
|
||||
vec3 lDir = pushC.lightPosition - worldPos;
|
||||
vec3 lDir = pcRaster.lightPosition - i_worldPos;
|
||||
float d = length(lDir);
|
||||
lightIntensity = pushC.lightIntensity / (d * d);
|
||||
lightIntensity = pcRaster.lightIntensity / (d * d);
|
||||
L = normalize(lDir);
|
||||
}
|
||||
else
|
||||
{
|
||||
L = normalize(pushC.lightPosition - vec3(0));
|
||||
L = normalize(pcRaster.lightPosition);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -86,13 +78,13 @@ void main()
|
|||
if(mat.pbrBaseColorTexture > -1)
|
||||
{
|
||||
uint txtId = mat.pbrBaseColorTexture;
|
||||
vec3 diffuseTxt = texture(textureSamplers[nonuniformEXT(txtId)], fragTexCoord).xyz;
|
||||
vec3 diffuseTxt = texture(textureSamplers[nonuniformEXT(txtId)], i_texCoord).xyz;
|
||||
diffuse *= diffuseTxt;
|
||||
}
|
||||
|
||||
// Specular
|
||||
vec3 specular = computeSpecular(mat, viewDir, L, N);
|
||||
vec3 specular = computeSpecular(mat, i_viewDir, L, N);
|
||||
|
||||
// Result
|
||||
outColor = vec4(lightIntensity * (diffuse + specular), 1);
|
||||
o_color = vec4(lightIntensity * (diffuse + specular), 1);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,32 +17,8 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
struct GltfShadeMaterial
|
||||
{
|
||||
vec4 pbrBaseColorFactor;
|
||||
vec3 emissiveFactor;
|
||||
int pbrBaseColorTexture;
|
||||
};
|
||||
|
||||
#ifndef __cplusplus
|
||||
struct PrimMeshInfo
|
||||
{
|
||||
uint indexOffset;
|
||||
uint vertexOffset;
|
||||
int materialIndex;
|
||||
};
|
||||
|
||||
struct SceneDesc
|
||||
{
|
||||
uint64_t vertexAddress;
|
||||
uint64_t normalAddress;
|
||||
uint64_t uvAddress;
|
||||
uint64_t indexAddress;
|
||||
uint64_t materialAddress;
|
||||
uint64_t matrixAddress;
|
||||
uint64_t rtPrimAddress;
|
||||
};
|
||||
|
||||
#include "host_device.h"
|
||||
|
||||
vec3 computeDiffuse(GltfShadeMaterial mat, vec3 lightDir, vec3 normal)
|
||||
{
|
||||
|
|
@ -65,4 +41,3 @@ vec3 computeSpecular(GltfShadeMaterial mat, vec3 viewDir, vec3 lightDir, vec3 no
|
|||
|
||||
return vec3(specular);
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
112
ray_tracing_gltf/shaders/host_device.h
Normal file
112
ray_tracing_gltf/shaders/host_device.h
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
|
||||
#ifndef COMMON_HOST_DEVICE
|
||||
#define COMMON_HOST_DEVICE
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include "nvmath/nvmath.h"
|
||||
// GLSL Type
|
||||
using vec2 = nvmath::vec2f;
|
||||
using vec3 = nvmath::vec3f;
|
||||
using vec4 = nvmath::vec4f;
|
||||
using mat4 = nvmath::mat4f;
|
||||
using uint = unsigned int;
|
||||
#endif
|
||||
|
||||
// clang-format off
|
||||
#ifdef __cplusplus // Descriptor binding helper for C++ and GLSL
|
||||
#define START_BINDING(a) enum a {
|
||||
#define END_BINDING() }
|
||||
#else
|
||||
#define START_BINDING(a) const uint
|
||||
#define END_BINDING()
|
||||
#endif
|
||||
|
||||
START_BINDING(SceneBindings)
|
||||
eGlobals = 0, // Global uniform containing camera matrices
|
||||
eSceneDesc = 1, // Access to the scene buffers
|
||||
eTextures = 2 // Access to textures
|
||||
END_BINDING();
|
||||
|
||||
START_BINDING(RtxBindings)
|
||||
eTlas = 0, // Top-level acceleration structure
|
||||
eOutImage = 1, // Ray tracer output image
|
||||
ePrimLookup = 2 // Lookup of objects
|
||||
END_BINDING();
|
||||
// clang-format on
|
||||
|
||||
// Scene buffer addresses
|
||||
struct SceneDesc
|
||||
{
|
||||
uint64_t vertexAddress; // Address of the Vertex buffer
|
||||
uint64_t normalAddress; // Address of the Normal buffer
|
||||
uint64_t uvAddress; // Address of the texture coordinates buffer
|
||||
uint64_t indexAddress; // Address of the triangle indices buffer
|
||||
uint64_t materialAddress; // Address of the Materials buffer (GltfShadeMaterial)
|
||||
uint64_t primInfoAddress; // Address of the mesh primitives buffer (PrimMeshInfo)
|
||||
};
|
||||
|
||||
// Uniform buffer set at each frame
|
||||
struct GlobalUniforms
|
||||
{
|
||||
mat4 viewProj; // Camera view * projection
|
||||
mat4 viewInverse; // Camera inverse view matrix
|
||||
mat4 projInverse; // Camera inverse projection matrix
|
||||
};
|
||||
|
||||
// Push constant structure for the raster
|
||||
struct PushConstantRaster
|
||||
{
|
||||
mat4 modelMatrix; // matrix of the instance
|
||||
vec3 lightPosition;
|
||||
uint objIndex;
|
||||
float lightIntensity;
|
||||
int lightType;
|
||||
int materialId;
|
||||
};
|
||||
|
||||
|
||||
// Push constant structure for the ray tracer
|
||||
struct PushConstantRay
|
||||
{
|
||||
vec4 clearColor;
|
||||
vec3 lightPosition;
|
||||
float lightIntensity;
|
||||
int lightType;
|
||||
int frame;
|
||||
};
|
||||
|
||||
// Structure used for retrieving the primitive information in the closest hit
|
||||
struct PrimMeshInfo
|
||||
{
|
||||
uint indexOffset;
|
||||
uint vertexOffset;
|
||||
int materialIndex;
|
||||
};
|
||||
|
||||
struct GltfShadeMaterial
|
||||
{
|
||||
vec4 pbrBaseColorFactor;
|
||||
vec3 emissiveFactor;
|
||||
int pbrBaseColorTexture;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -27,11 +27,10 @@
|
|||
#extension GL_EXT_buffer_reference2 : require
|
||||
|
||||
|
||||
#include "binding.glsl"
|
||||
#include "gltf.glsl"
|
||||
#include "raycommon.glsl"
|
||||
#include "sampling.glsl"
|
||||
|
||||
#include "host_device.h"
|
||||
|
||||
hitAttributeEXT vec2 attribs;
|
||||
|
||||
|
|
@ -49,20 +48,12 @@ 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
|
||||
layout(set = 1, binding = eSceneDesc ) readonly buffer SceneDesc_ { SceneDesc sceneDesc; };
|
||||
layout(set = 1, binding = eTextures) uniform sampler2D texturesMap[]; // all textures
|
||||
|
||||
layout(push_constant) uniform _PushConstantRay { PushConstantRay pcRay; };
|
||||
// clang-format on
|
||||
|
||||
layout(push_constant) uniform Constants
|
||||
{
|
||||
vec4 clearColor;
|
||||
vec3 lightPosition;
|
||||
float lightIntensity;
|
||||
int lightType;
|
||||
}
|
||||
pushC;
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -21,35 +21,22 @@
|
|||
#extension GL_EXT_ray_tracing : require
|
||||
#extension GL_GOOGLE_include_directive : enable
|
||||
#extension GL_ARB_shader_clock : enable
|
||||
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
|
||||
|
||||
|
||||
#include "binding.glsl"
|
||||
#include "raycommon.glsl"
|
||||
#include "sampling.glsl"
|
||||
#include "host_device.h"
|
||||
|
||||
// clang-format off
|
||||
layout(location = 0) rayPayloadEXT hitPayload prd;
|
||||
|
||||
layout(set = 0, binding = 0) uniform accelerationStructureEXT topLevelAS;
|
||||
layout(set = 0, binding = 1, rgba32f) uniform image2D image;
|
||||
|
||||
layout(location = 0) rayPayloadEXT hitPayload prd;
|
||||
|
||||
layout(set = 1, binding = B_CAMERA) uniform CameraProperties
|
||||
{
|
||||
mat4 view;
|
||||
mat4 proj;
|
||||
mat4 viewInverse;
|
||||
mat4 projInverse;
|
||||
}
|
||||
cam;
|
||||
|
||||
layout(push_constant) uniform Constants
|
||||
{
|
||||
vec4 clearColor;
|
||||
vec3 lightPosition;
|
||||
float lightIntensity;
|
||||
int lightType;
|
||||
int frame;
|
||||
}
|
||||
pushC;
|
||||
layout(set = 1, binding = 0) uniform _GlobalUniforms { GlobalUniforms uni; };
|
||||
layout(push_constant) uniform _PushConstantRay { PushConstantRay pcRay; };
|
||||
// clang-format on
|
||||
|
||||
void main()
|
||||
{
|
||||
|
|
@ -60,9 +47,9 @@ void main()
|
|||
const vec2 inUV = pixelCenter / vec2(gl_LaunchSizeEXT.xy);
|
||||
vec2 d = inUV * 2.0 - 1.0;
|
||||
|
||||
vec4 origin = cam.viewInverse * vec4(0, 0, 0, 1);
|
||||
vec4 target = cam.projInverse * vec4(d.x, d.y, 1, 1);
|
||||
vec4 direction = cam.viewInverse * vec4(normalize(target.xyz), 0);
|
||||
vec4 origin = uni.viewInverse * vec4(0, 0, 0, 1);
|
||||
vec4 target = uni.projInverse * vec4(d.x, d.y, 1, 1);
|
||||
vec4 direction = uni.viewInverse * vec4(normalize(target.xyz), 0);
|
||||
|
||||
uint rayFlags = gl_RayFlagsOpaqueEXT;
|
||||
float tMin = 0.001;
|
||||
|
|
@ -98,9 +85,9 @@ void main()
|
|||
}
|
||||
|
||||
// Do accumulation over time
|
||||
if(pushC.frame > 0)
|
||||
if(pcRay.frame > 0)
|
||||
{
|
||||
float a = 1.0f / float(pushC.frame + 1);
|
||||
float a = 1.0f / float(pcRay.frame + 1);
|
||||
vec3 old_color = imageLoad(image, ivec2(gl_LaunchIDEXT.xy)).xyz;
|
||||
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(mix(old_color, hitValue, a), 1.f));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,9 +27,9 @@
|
|||
#extension GL_EXT_buffer_reference2 : require
|
||||
|
||||
|
||||
#include "binding.glsl"
|
||||
#include "gltf.glsl"
|
||||
#include "raycommon.glsl"
|
||||
#include "host_device.h"
|
||||
|
||||
hitAttributeEXT vec2 attribs;
|
||||
|
||||
|
|
@ -49,8 +49,8 @@ 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
|
||||
layout(set = 1, binding = eSceneDesc ) readonly buffer SceneDesc_ { SceneDesc sceneDesc; };
|
||||
layout(set = 1, binding = eTextures) uniform sampler2D texturesMap[]; // all textures
|
||||
|
||||
// clang-format on
|
||||
|
||||
|
|
|
|||
|
|
@ -20,22 +20,21 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : require
|
||||
#extension GL_GOOGLE_include_directive : enable
|
||||
#include "binding.glsl"
|
||||
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
|
||||
|
||||
#include "raycommon.glsl"
|
||||
#include "sampling.glsl"
|
||||
#include "host_device.h"
|
||||
|
||||
// clang-format off
|
||||
layout(location = 0) rayPayloadEXT hitPayload prd;
|
||||
|
||||
layout(set = 0, binding = 0) uniform accelerationStructureEXT topLevelAS;
|
||||
layout(set = 0, binding = 1, rgba32f) uniform image2D image;
|
||||
|
||||
layout(location = 0) rayPayloadEXT hitPayload prd;
|
||||
|
||||
layout(set = 1, binding = B_CAMERA) uniform CameraProperties
|
||||
{
|
||||
mat4 view;
|
||||
mat4 proj;
|
||||
mat4 viewInverse;
|
||||
mat4 projInverse;
|
||||
}
|
||||
cam;
|
||||
layout(set = 1, binding = 0) uniform _GlobalUniforms { GlobalUniforms uni; };
|
||||
layout(push_constant) uniform _PushConstantRay { PushConstantRay pcRay; };
|
||||
// clang-format on
|
||||
|
||||
void main()
|
||||
{
|
||||
|
|
@ -43,9 +42,9 @@ void main()
|
|||
const vec2 inUV = pixelCenter / vec2(gl_LaunchSizeEXT.xy);
|
||||
vec2 d = inUV * 2.0 - 1.0;
|
||||
|
||||
vec4 origin = cam.viewInverse * vec4(0, 0, 0, 1);
|
||||
vec4 target = cam.projInverse * vec4(d.x, d.y, 1, 1);
|
||||
vec4 direction = cam.viewInverse * vec4(normalize(target.xyz), 0);
|
||||
vec4 origin = uni.viewInverse * vec4(0, 0, 0, 1);
|
||||
vec4 target = uni.projInverse * vec4(d.x, d.y, 1, 1);
|
||||
vec4 direction = uni.viewInverse * vec4(normalize(target.xyz), 0);
|
||||
|
||||
uint rayFlags = gl_RayFlagsOpaqueEXT;
|
||||
float tMin = 0.001;
|
||||
|
|
|
|||
|
|
@ -21,46 +21,30 @@
|
|||
#extension GL_ARB_separate_shader_objects : 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 "host_device.h"
|
||||
|
||||
// clang-format off
|
||||
layout(buffer_reference, scalar) buffer Matrices { mat4 m[]; };
|
||||
layout( set = 0, binding = B_SCENEDESC ) readonly buffer SceneDesc_ { SceneDesc sceneDesc; };
|
||||
// clang-format on
|
||||
|
||||
layout(binding = 0) uniform UniformBufferObject
|
||||
layout(binding = 0) uniform _GlobalUniforms
|
||||
{
|
||||
mat4 view;
|
||||
mat4 proj;
|
||||
mat4 viewI;
|
||||
}
|
||||
ubo;
|
||||
GlobalUniforms uni;
|
||||
};
|
||||
|
||||
layout(push_constant) uniform shaderInformation
|
||||
layout(push_constant) uniform _PushConstantRaster
|
||||
{
|
||||
vec3 lightPosition;
|
||||
uint instanceId;
|
||||
float lightIntensity;
|
||||
int lightType;
|
||||
int materialId;
|
||||
}
|
||||
pushC;
|
||||
PushConstantRaster pcRaster;
|
||||
};
|
||||
|
||||
layout(location = 0) in vec3 inPosition;
|
||||
layout(location = 1) in vec3 inNormal;
|
||||
layout(location = 2) in vec2 inTexCoord;
|
||||
layout(location = 0) in vec3 i_position;
|
||||
layout(location = 1) in vec3 i_normal;
|
||||
layout(location = 2) in vec2 i_texCoord;
|
||||
|
||||
|
||||
//layout(location = 0) flat out int matIndex;
|
||||
layout(location = 1) out vec2 fragTexCoord;
|
||||
layout(location = 2) out vec3 fragNormal;
|
||||
layout(location = 3) out vec3 viewDir;
|
||||
layout(location = 4) out vec3 worldPos;
|
||||
layout(location = 1) out vec3 o_worldPos;
|
||||
layout(location = 2) out vec3 o_worldNrm;
|
||||
layout(location = 3) out vec3 o_viewDir;
|
||||
layout(location = 4) out vec2 o_texCoord;
|
||||
|
||||
out gl_PerVertex
|
||||
{
|
||||
|
|
@ -70,18 +54,12 @@ out gl_PerVertex
|
|||
|
||||
void main()
|
||||
{
|
||||
Matrices matrices = Matrices(sceneDesc.matrixAddress);
|
||||
mat4 objMatrix = matrices.m[pushC.instanceId];
|
||||
vec3 origin = vec3(uni.viewInverse * vec4(0, 0, 0, 1));
|
||||
|
||||
mat4 objMatrixIT = transpose(inverse(objMatrix));
|
||||
o_worldPos = vec3(pcRaster.modelMatrix * vec4(i_position, 1.0));
|
||||
o_viewDir = vec3(o_worldPos - origin);
|
||||
o_texCoord = i_texCoord;
|
||||
o_worldNrm = mat3(pcRaster.modelMatrix) * i_normal;
|
||||
|
||||
vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1));
|
||||
|
||||
worldPos = vec3(objMatrix * vec4(inPosition, 1.0));
|
||||
viewDir = vec3(worldPos - origin);
|
||||
fragTexCoord = inTexCoord;
|
||||
fragNormal = vec3(objMatrixIT * vec4(inNormal, 0.0));
|
||||
// matIndex = inMatID;
|
||||
|
||||
gl_Position = ubo.proj * ubo.view * vec4(worldPos, 1.0);
|
||||
gl_Position = uni.viewProj * vec4(o_worldPos, 1.0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue