Refactoring
This commit is contained in:
parent
3e399adf0a
commit
d90ce79135
222 changed files with 9045 additions and 5734 deletions
|
|
@ -29,91 +29,86 @@
|
|||
#include "wavefront.glsl"
|
||||
|
||||
|
||||
layout(push_constant) uniform shaderInformation
|
||||
layout(push_constant) uniform _PushConstantRaster
|
||||
{
|
||||
vec3 lightPosition;
|
||||
float lightIntensity;
|
||||
vec3 lightDirection;
|
||||
float lightSpotCutoff;
|
||||
float lightSpotOuterCutoff;
|
||||
uint instanceId;
|
||||
int lightType;
|
||||
}
|
||||
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;
|
||||
|
||||
layout(buffer_reference, scalar) buffer Vertices {Vertex v[]; }; // Positions of an object
|
||||
layout(buffer_reference, scalar) buffer Indices {uint i[]; }; // Triangle indices
|
||||
layout(buffer_reference, scalar) buffer Materials {WaveFrontMaterial m[]; }; // Array of all materials on an object
|
||||
layout(buffer_reference, scalar) buffer MatIndices {int i[]; }; // Material ID for each triangle
|
||||
|
||||
layout(binding = 1, scalar) buffer SceneDesc_ { SceneDesc i[]; } sceneDesc;
|
||||
layout(binding = 2) uniform sampler2D[] textureSamplers;
|
||||
layout(binding = eObjDescs, scalar) buffer ObjDesc_ { ObjDesc i[]; } objDesc;
|
||||
layout(binding = eTextures) uniform sampler2D[] textureSamplers;
|
||||
// clang-format on
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
// Material of the object
|
||||
SceneDesc objResource = sceneDesc.i[pushC.instanceId];
|
||||
ObjDesc objResource = objDesc.i[pcRaster.objIndex];
|
||||
MatIndices matIndices = MatIndices(objResource.materialIndexAddress);
|
||||
Materials materials = Materials(objResource.materialAddress);
|
||||
|
||||
int matIndex = matIndices.i[gl_PrimitiveID];
|
||||
WaveFrontMaterial mat = materials.m[matIndex];
|
||||
|
||||
vec3 N = normalize(fragNormal);
|
||||
vec3 N = normalize(i_worldNrm);
|
||||
|
||||
// Vector toward light
|
||||
vec3 LightDir;
|
||||
float lightIntensity;
|
||||
|
||||
// Point light
|
||||
if(pushC.lightType == 0)
|
||||
if(pcRaster.lightType == 0)
|
||||
{
|
||||
vec3 lDir = pushC.lightPosition - worldPos;
|
||||
vec3 lDir = pcRaster.lightPosition - i_worldPos;
|
||||
float lightDistance = length(lDir);
|
||||
lightIntensity = pushC.lightIntensity / (lightDistance * lightDistance);
|
||||
lightIntensity = pcRaster.lightIntensity / (lightDistance * lightDistance);
|
||||
LightDir = normalize(lDir);
|
||||
}
|
||||
else if(pushC.lightType == 1)
|
||||
else if(pcRaster.lightType == 1)
|
||||
{
|
||||
vec3 lDir = pushC.lightPosition - worldPos;
|
||||
vec3 lDir = pcRaster.lightPosition - i_worldPos;
|
||||
float lightDistance = length(lDir);
|
||||
lightIntensity = pushC.lightIntensity / (lightDistance * lightDistance);
|
||||
lightIntensity = pcRaster.lightIntensity / (lightDistance * lightDistance);
|
||||
LightDir = normalize(lDir);
|
||||
float theta = dot(LightDir, normalize(-pushC.lightDirection));
|
||||
float epsilon = pushC.lightSpotCutoff - pushC.lightSpotOuterCutoff;
|
||||
float spotIntensity = clamp((theta - pushC.lightSpotOuterCutoff) / epsilon, 0.0, 1.0);
|
||||
float theta = dot(LightDir, normalize(-pcRaster.lightDirection));
|
||||
float epsilon = pcRaster.lightSpotCutoff - pcRaster.lightSpotOuterCutoff;
|
||||
float spotIntensity = clamp((theta - pcRaster.lightSpotOuterCutoff) / epsilon, 0.0, 1.0);
|
||||
lightIntensity *= spotIntensity;
|
||||
}
|
||||
else // Directional light
|
||||
{
|
||||
LightDir = normalize(-pushC.lightDirection);
|
||||
LightDir = normalize(-pcRaster.lightDirection);
|
||||
lightIntensity = 1.0;
|
||||
}
|
||||
|
||||
// Diffuse
|
||||
vec3 diffuse = computeDiffuse(mat, LightDir, N);
|
||||
|
||||
if(mat.textureId >= 0)
|
||||
{
|
||||
int txtOffset = sceneDesc.i[pushC.instanceId].txtOffset;
|
||||
int txtOffset = objDesc.i[pcRaster.objIndex].txtOffset;
|
||||
uint txtId = txtOffset + mat.textureId;
|
||||
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, LightDir, N);
|
||||
vec3 specular = computeSpecular(mat, i_viewDir, LightDir, N);
|
||||
|
||||
// Result
|
||||
outColor = vec4(lightIntensity * (diffuse + specular), 1);
|
||||
o_color = vec4(lightIntensity * (diffuse + specular), 1);
|
||||
}
|
||||
|
|
|
|||
126
ray_tracing__advance/shaders/host_device.h
Normal file
126
ray_tracing__advance/shaders/host_device.h
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* 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
|
||||
eObjDescs = 1, // Access to the object descriptions
|
||||
eTextures = 2, // Access to textures
|
||||
eImplicits = 3 // Implicit objects
|
||||
END_BINDING();
|
||||
|
||||
START_BINDING(RtxBindings)
|
||||
eTlas = 0, // Top-level acceleration structure
|
||||
eOutImage = 1 // Ray tracer output image
|
||||
END_BINDING();
|
||||
// clang-format on
|
||||
|
||||
// Information of a obj model when referenced in a shader
|
||||
struct ObjDesc
|
||||
{
|
||||
int txtOffset; // Texture index offset in the array of textures
|
||||
uint64_t vertexAddress; // Address of the Vertex buffer
|
||||
uint64_t indexAddress; // Address of the index buffer
|
||||
uint64_t materialAddress; // Address of the material buffer
|
||||
uint64_t materialIndexAddress; // Address of the triangle material index buffer
|
||||
};
|
||||
|
||||
// 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;
|
||||
vec3 lightDirection;
|
||||
float lightSpotCutoff;
|
||||
float lightSpotOuterCutoff;
|
||||
float lightIntensity;
|
||||
int lightType;
|
||||
int frame;
|
||||
};
|
||||
|
||||
|
||||
// Push constant structure for the ray tracer
|
||||
struct PushConstantRay
|
||||
{
|
||||
vec4 clearColor;
|
||||
vec3 lightPosition;
|
||||
uint objIndex;
|
||||
vec3 lightDirection;
|
||||
float lightSpotCutoff;
|
||||
float lightSpotOuterCutoff;
|
||||
float lightIntensity;
|
||||
int lightType;
|
||||
int frame;
|
||||
};
|
||||
|
||||
struct Vertex // See ObjLoader, copy of VertexObj, could be compressed for device
|
||||
{
|
||||
vec3 pos;
|
||||
vec3 nrm;
|
||||
vec3 color;
|
||||
vec2 texCoord;
|
||||
};
|
||||
|
||||
struct WaveFrontMaterial // See ObjLoader, copy of MaterialObj, could be compressed for device
|
||||
{
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 transmittance;
|
||||
vec3 emission;
|
||||
float shininess;
|
||||
float ior; // index of refraction
|
||||
float dissolve; // 1 == opaque; 0 == fully transparent
|
||||
int illum; // illumination model (see http://www.fileformat.info/format/material/)
|
||||
int textureId;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -20,24 +20,21 @@
|
|||
#version 460 core
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
#extension GL_GOOGLE_include_directive : enable
|
||||
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
|
||||
|
||||
#include "raycommon.glsl"
|
||||
#include "host_device.h"
|
||||
|
||||
layout(location = 3) callableDataInEXT rayLight cLight;
|
||||
|
||||
layout(push_constant) uniform Constants
|
||||
layout(push_constant) uniform _PushConstantRay
|
||||
{
|
||||
vec4 clearColor;
|
||||
vec3 lightPosition;
|
||||
float lightIntensity;
|
||||
vec3 lightDirection;
|
||||
float lightSpotCutoff;
|
||||
float lightSpotOuterCutoff;
|
||||
int lightType;
|
||||
PushConstantRay pcRay;
|
||||
};
|
||||
|
||||
void main()
|
||||
{
|
||||
cLight.outLightDistance = 10000000;
|
||||
cLight.outIntensity = 1.0;
|
||||
cLight.outLightDir = normalize(-lightDirection);
|
||||
cLight.outLightDir = normalize(-pcRay.lightDirection);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,25 +20,23 @@
|
|||
#version 460 core
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
#extension GL_GOOGLE_include_directive : enable
|
||||
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
|
||||
|
||||
#include "raycommon.glsl"
|
||||
#include "host_device.h"
|
||||
|
||||
layout(location = 3) callableDataInEXT rayLight cLight;
|
||||
|
||||
layout(push_constant) uniform Constants
|
||||
layout(push_constant) uniform _PushConstantRay
|
||||
{
|
||||
vec4 clearColor;
|
||||
vec3 lightPosition;
|
||||
float lightIntensity;
|
||||
vec3 lightDirection;
|
||||
float lightSpotCutoff;
|
||||
float lightSpotOuterCutoff;
|
||||
int lightType;
|
||||
PushConstantRay pcRay;
|
||||
};
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 lDir = lightPosition - cLight.inHitPosition;
|
||||
vec3 lDir = pcRay.lightPosition - cLight.inHitPosition;
|
||||
cLight.outLightDistance = length(lDir);
|
||||
cLight.outIntensity = lightIntensity / (cLight.outLightDistance * cLight.outLightDistance);
|
||||
cLight.outIntensity = pcRay.lightIntensity / (cLight.outLightDistance * cLight.outLightDistance);
|
||||
cLight.outLightDir = normalize(lDir);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,29 +20,26 @@
|
|||
#version 460 core
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
#extension GL_GOOGLE_include_directive : enable
|
||||
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
|
||||
|
||||
#include "raycommon.glsl"
|
||||
#include "host_device.h"
|
||||
|
||||
layout(location = 3) callableDataInEXT rayLight cLight;
|
||||
|
||||
layout(push_constant) uniform Constants
|
||||
layout(push_constant) uniform _PushConstantRay
|
||||
{
|
||||
vec4 clearColor;
|
||||
vec3 lightPosition;
|
||||
float lightIntensity;
|
||||
vec3 lightDirection;
|
||||
float lightSpotCutoff;
|
||||
float lightSpotOuterCutoff;
|
||||
int lightType;
|
||||
PushConstantRay pcRay;
|
||||
};
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 lDir = lightPosition - cLight.inHitPosition;
|
||||
vec3 lDir = pcRay.lightPosition - cLight.inHitPosition;
|
||||
cLight.outLightDistance = length(lDir);
|
||||
cLight.outIntensity = lightIntensity / (cLight.outLightDistance * cLight.outLightDistance);
|
||||
cLight.outIntensity = pcRay.lightIntensity / (cLight.outLightDistance * cLight.outLightDistance);
|
||||
cLight.outLightDir = normalize(lDir);
|
||||
float theta = dot(cLight.outLightDir, normalize(-lightDirection));
|
||||
float epsilon = lightSpotCutoff - lightSpotOuterCutoff;
|
||||
float spotIntensity = clamp((theta - lightSpotOuterCutoff) / epsilon, 0.0, 1.0);
|
||||
float theta = dot(cLight.outLightDir, normalize(-pcRay.lightDirection));
|
||||
float epsilon = pcRay.lightSpotCutoff - pcRay.lightSpotOuterCutoff;
|
||||
float spotIntensity = clamp((theta - pcRay.lightSpotOuterCutoff) / epsilon, 0.0, 1.0);
|
||||
cLight.outIntensity *= spotIntensity;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,13 +36,13 @@ layout(buffer_reference, scalar) buffer Vertices {Vertex v[]; }; // Positions of
|
|||
layout(buffer_reference, scalar) buffer Indices {uint i[]; }; // Triangle indices
|
||||
layout(buffer_reference, scalar) buffer Materials {WaveFrontMaterial m[]; }; // Array of all materials on an object
|
||||
layout(buffer_reference, scalar) buffer MatIndices {int i[]; }; // Material ID for each triangle
|
||||
layout(binding = 1, set = 1, scalar) buffer SceneDesc_ { SceneDesc i[]; } sceneDesc;
|
||||
layout(set = 1, binding = eObjDescs, scalar) buffer ObjDesc_ { ObjDesc i[]; } objDesc;
|
||||
// clang-format on
|
||||
|
||||
void main()
|
||||
{
|
||||
// Object of this instance
|
||||
SceneDesc objResource = sceneDesc.i[gl_InstanceCustomIndexEXT];
|
||||
ObjDesc objResource = objDesc.i[gl_InstanceCustomIndexEXT];
|
||||
MatIndices matIndices = MatIndices(objResource.materialIndexAddress);
|
||||
Materials materials = Materials(objResource.materialAddress);
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@
|
|||
#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
|
||||
|
||||
|
|
@ -39,22 +38,13 @@ layout(buffer_reference, scalar) buffer Vertices {Vertex v[]; }; // Positions of
|
|||
layout(buffer_reference, scalar) buffer Indices {ivec3 i[]; }; // Triangle indices
|
||||
layout(buffer_reference, scalar) buffer Materials {WaveFrontMaterial m[]; }; // Array of all materials on an object
|
||||
layout(buffer_reference, scalar) buffer MatIndices {int i[]; }; // Material ID for each triangle
|
||||
layout(binding = 0, set = 0) uniform accelerationStructureEXT topLevelAS;
|
||||
layout(binding = 1, set = 1, scalar) buffer SceneDesc_ { SceneDesc i[]; } sceneDesc;
|
||||
layout(binding = 2, set = 1) uniform sampler2D textureSamplers[];
|
||||
layout(set = 0, binding = eTlas) uniform accelerationStructureEXT topLevelAS;
|
||||
layout(set = 1, binding = eObjDescs, scalar) buffer ObjDesc_ { ObjDesc i[]; } objDesc;
|
||||
layout(set = 1, binding = eTextures) uniform sampler2D textureSamplers[];
|
||||
|
||||
layout(push_constant) uniform _PushConstantRay { PushConstantRay pcRay; };
|
||||
// clang-format on
|
||||
|
||||
layout(push_constant) uniform Constants
|
||||
{
|
||||
vec4 clearColor;
|
||||
vec3 lightPosition;
|
||||
float lightIntensity;
|
||||
vec3 lightDirection;
|
||||
float lightSpotCutoff;
|
||||
float lightSpotOuterCutoff;
|
||||
int lightType;
|
||||
}
|
||||
pushC;
|
||||
|
||||
layout(location = 3) callableDataEXT rayLight cLight;
|
||||
|
||||
|
|
@ -62,7 +52,7 @@ layout(location = 3) callableDataEXT rayLight cLight;
|
|||
void main()
|
||||
{
|
||||
// Object data
|
||||
SceneDesc objResource = sceneDesc.i[gl_InstanceCustomIndexEXT];
|
||||
ObjDesc objResource = objDesc.i[gl_InstanceCustomIndexEXT];
|
||||
MatIndices matIndices = MatIndices(objResource.materialIndexAddress);
|
||||
Materials materials = Materials(objResource.materialAddress);
|
||||
Indices indices = Indices(objResource.indexAddress);
|
||||
|
|
@ -81,45 +71,44 @@ void main()
|
|||
// Computing the normal at hit position
|
||||
vec3 normal = v0.nrm * barycentrics.x + v1.nrm * barycentrics.y + v2.nrm * barycentrics.z;
|
||||
// Transforming the normal to world space
|
||||
normal = normalize(vec3(sceneDesc.i[gl_InstanceCustomIndexEXT].transfoIT * vec4(normal, 0.0)));
|
||||
|
||||
normal = normalize(vec3(normal * gl_WorldToObjectEXT));
|
||||
|
||||
// Computing the coordinates of the hit position
|
||||
vec3 worldPos = v0.pos * barycentrics.x + v1.pos * barycentrics.y + v2.pos * barycentrics.z;
|
||||
// Transforming the position to world space
|
||||
worldPos = vec3(sceneDesc.i[gl_InstanceCustomIndexEXT].transfo * vec4(worldPos, 1.0));
|
||||
worldPos = vec3(gl_ObjectToWorldEXT * vec4(worldPos, 1.0));
|
||||
|
||||
cLight.inHitPosition = worldPos;
|
||||
//#define DONT_USE_CALLABLE
|
||||
#if defined(DONT_USE_CALLABLE)
|
||||
// Point light
|
||||
if(pushC.lightType == 0)
|
||||
if(pcRay.lightType == 0)
|
||||
{
|
||||
vec3 lDir = pushC.lightPosition - cLight.inHitPosition;
|
||||
vec3 lDir = pcRay.lightPosition - cLight.inHitPosition;
|
||||
float lightDistance = length(lDir);
|
||||
cLight.outIntensity = pushC.lightIntensity / (lightDistance * lightDistance);
|
||||
cLight.outIntensity = pcRay.lightIntensity / (lightDistance * lightDistance);
|
||||
cLight.outLightDir = normalize(lDir);
|
||||
cLight.outLightDistance = lightDistance;
|
||||
}
|
||||
else if(pushC.lightType == 1)
|
||||
else if(pcRay.lightType == 1)
|
||||
{
|
||||
vec3 lDir = pushC.lightPosition - cLight.inHitPosition;
|
||||
vec3 lDir = pcRay.lightPosition - cLight.inHitPosition;
|
||||
cLight.outLightDistance = length(lDir);
|
||||
cLight.outIntensity = pushC.lightIntensity / (cLight.outLightDistance * cLight.outLightDistance);
|
||||
cLight.outIntensity = pcRay.lightIntensity / (cLight.outLightDistance * cLight.outLightDistance);
|
||||
cLight.outLightDir = normalize(lDir);
|
||||
float theta = dot(cLight.outLightDir, normalize(-pushC.lightDirection));
|
||||
float epsilon = pushC.lightSpotCutoff - pushC.lightSpotOuterCutoff;
|
||||
float spotIntensity = clamp((theta - pushC.lightSpotOuterCutoff) / epsilon, 0.0, 1.0);
|
||||
float theta = dot(cLight.outLightDir, normalize(-pcRay.lightDirection));
|
||||
float epsilon = pcRay.lightSpotCutoff - pcRay.lightSpotOuterCutoff;
|
||||
float spotIntensity = clamp((theta - pcRay.lightSpotOuterCutoff) / epsilon, 0.0, 1.0);
|
||||
cLight.outIntensity *= spotIntensity;
|
||||
}
|
||||
else // Directional light
|
||||
{
|
||||
cLight.outLightDir = normalize(-pushC.lightDirection);
|
||||
cLight.outLightDir = normalize(-pcRay.lightDirection);
|
||||
cLight.outIntensity = 1.0;
|
||||
cLight.outLightDistance = 10000000;
|
||||
}
|
||||
#else
|
||||
executeCallableEXT(pushC.lightType, 3);
|
||||
executeCallableEXT(pcRay.lightType, 3);
|
||||
#endif
|
||||
|
||||
// Material of the object
|
||||
|
|
@ -131,7 +120,7 @@ void main()
|
|||
vec3 diffuse = computeDiffuse(mat, cLight.outLightDir, normal);
|
||||
if(mat.textureId >= 0)
|
||||
{
|
||||
uint txtId = mat.textureId + sceneDesc.i[gl_InstanceCustomIndexEXT].txtOffset;
|
||||
uint txtId = mat.textureId + objDesc.i[gl_InstanceCustomIndexEXT].txtOffset;
|
||||
vec2 texCoord = v0.texCoord * barycentrics.x + v1.texCoord * barycentrics.y + v2.texCoord * barycentrics.z;
|
||||
diffuse *= texture(textureSamplers[nonuniformEXT(txtId)], texCoord).xyz;
|
||||
}
|
||||
|
|
@ -183,6 +172,5 @@ void main()
|
|||
prd.rayDir = rayDir;
|
||||
}
|
||||
|
||||
|
||||
prd.hitValue = vec3(cLight.outIntensity * attenuation * (diffuse + specular));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,42 +20,27 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : require
|
||||
#extension GL_GOOGLE_include_directive : enable
|
||||
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
|
||||
|
||||
#include "random.glsl"
|
||||
#include "raycommon.glsl"
|
||||
#include "wavefront.glsl"
|
||||
|
||||
layout(binding = 0, set = 0) uniform accelerationStructureEXT topLevelAS;
|
||||
layout(binding = 1, set = 0, rgba32f) uniform image2D image;
|
||||
|
||||
// clang-format off
|
||||
layout(location = 0) rayPayloadEXT hitPayload prd;
|
||||
|
||||
layout(binding = 0, set = 1) uniform CameraProperties
|
||||
{
|
||||
mat4 view;
|
||||
mat4 proj;
|
||||
mat4 viewInverse;
|
||||
mat4 projInverse;
|
||||
}
|
||||
cam;
|
||||
|
||||
layout(push_constant) uniform Constants
|
||||
{
|
||||
vec4 clearColor;
|
||||
vec3 lightPosition;
|
||||
float lightIntensity;
|
||||
vec3 lightDirection;
|
||||
float lightSpotCutoff;
|
||||
float lightSpotOuterCutoff;
|
||||
int lightType;
|
||||
int frame;
|
||||
}
|
||||
pushC;
|
||||
layout(set = 0, binding = eTlas) uniform accelerationStructureEXT topLevelAS;
|
||||
layout(set = 0, binding = eOutImage, rgba32f) uniform image2D image;
|
||||
layout(set = 1, binding = eGlobals) uniform _GlobalUniforms { GlobalUniforms uni; };
|
||||
layout(push_constant) uniform _PushConstantRay { PushConstantRay pcRay; };
|
||||
// clang-format on
|
||||
|
||||
const int NBSAMPLES = 5;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Initialize the random number
|
||||
uint seed = tea(gl_LaunchIDEXT.y * gl_LaunchSizeEXT.x + gl_LaunchIDEXT.x, pushC.frame * NBSAMPLES);
|
||||
uint seed = tea(gl_LaunchIDEXT.y * gl_LaunchSizeEXT.x + gl_LaunchIDEXT.x, pcRay.frame * NBSAMPLES);
|
||||
prd.seed = seed;
|
||||
|
||||
vec3 hitValues = vec3(0);
|
||||
|
|
@ -67,7 +52,7 @@ void main()
|
|||
float r2 = rnd(seed);
|
||||
// Subpixel jitter: send the ray through a different position inside the pixel
|
||||
// each time, to provide antialiasing.
|
||||
vec2 subpixel_jitter = pushC.frame == 0 ? vec2(0.5f, 0.5f) : vec2(r1, r2);
|
||||
vec2 subpixel_jitter = pcRay.frame == 0 ? vec2(0.5f, 0.5f) : vec2(r1, r2);
|
||||
|
||||
const vec2 pixelCenter = vec2(gl_LaunchIDEXT.xy) + subpixel_jitter;
|
||||
|
||||
|
|
@ -75,9 +60,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_RayFlagsNoneEXT;
|
||||
float tMin = 0.001;
|
||||
|
|
@ -120,9 +105,9 @@ void main()
|
|||
prd.hitValue = hitValues / NBSAMPLES;
|
||||
|
||||
// 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, prd.hitValue, a), 1.f));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@
|
|||
|
||||
hitAttributeEXT vec3 HitAttribute;
|
||||
|
||||
layout(binding = 3, set = 1, scalar) buffer allImpl_
|
||||
layout(set = 1, binding = eImplicits, scalar) buffer allImpl_
|
||||
{
|
||||
Implicit i[];
|
||||
}
|
||||
|
|
@ -77,6 +77,7 @@ float hitAabb(const Aabb aabb, const Ray r)
|
|||
|
||||
void main()
|
||||
{
|
||||
|
||||
Ray ray;
|
||||
ray.origin = gl_WorldRayOriginEXT;
|
||||
ray.direction = gl_WorldRayDirectionEXT;
|
||||
|
|
|
|||
|
|
@ -20,16 +20,19 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : require
|
||||
#extension GL_GOOGLE_include_directive : enable
|
||||
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
|
||||
|
||||
#include "raycommon.glsl"
|
||||
#include "wavefront.glsl"
|
||||
|
||||
layout(location = 0) rayPayloadInEXT hitPayload prd;
|
||||
|
||||
layout(push_constant) uniform Constants
|
||||
layout(push_constant) uniform _PushConstantRay
|
||||
{
|
||||
vec4 clearColor;
|
||||
PushConstantRay pcRay;
|
||||
};
|
||||
|
||||
void main()
|
||||
{
|
||||
prd.hitValue = clearColor.xyz * 0.8;
|
||||
prd.hitValue = pcRay.clearColor.xyz * 0.8;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,8 +36,8 @@ layout(buffer_reference, scalar) buffer Vertices {Vertex v[]; }; // Positions of
|
|||
layout(buffer_reference, scalar) buffer Indices {uint i[]; }; // Triangle indices
|
||||
layout(buffer_reference, scalar) buffer Materials {WaveFrontMaterial m[]; }; // Array of all materials on an object
|
||||
layout(buffer_reference, scalar) buffer MatIndices {int i[]; }; // Material ID for each triangle
|
||||
layout(binding = 1, set = 1, scalar) buffer SceneDesc_ { SceneDesc i[]; } sceneDesc;
|
||||
layout(binding = 3, set = 1, scalar) buffer allImplicits_ {Implicit i[];} allImplicits;
|
||||
layout(set = 1, binding = eObjDescs, scalar) buffer ObjDesc_ { ObjDesc i[]; } objDesc;
|
||||
layout(set = 1, binding = eImplicits, scalar) buffer allImplicits_ {Implicit i[];} allImplicits;
|
||||
// clang-format on
|
||||
|
||||
void main()
|
||||
|
|
@ -45,7 +45,7 @@ void main()
|
|||
// Material of the object
|
||||
Implicit impl = allImplicits.i[gl_PrimitiveID];
|
||||
|
||||
SceneDesc objResource = sceneDesc.i[gl_InstanceCustomIndexEXT];
|
||||
ObjDesc objResource = objDesc.i[gl_InstanceCustomIndexEXT];
|
||||
Materials materials = Materials(objResource.materialAddress);
|
||||
|
||||
WaveFrontMaterial mat = materials.m[impl.matId];
|
||||
|
|
|
|||
|
|
@ -41,22 +41,12 @@ layout(buffer_reference, scalar) buffer Vertices {Vertex v[]; }; // Positions of
|
|||
layout(buffer_reference, scalar) buffer Indices {uint i[]; }; // Triangle indices
|
||||
layout(buffer_reference, scalar) buffer Materials {WaveFrontMaterial m[]; }; // Array of all materials on an object
|
||||
layout(buffer_reference, scalar) buffer MatIndices {int i[]; }; // Material ID for each triangle
|
||||
layout(binding = 1, set = 1, scalar) buffer SceneDesc_ { SceneDesc i[]; } sceneDesc;
|
||||
layout(binding = 3, set = 1, scalar) buffer allImplicits_ {Implicit i[];} allImplicits;
|
||||
layout(set = 1, binding = eObjDescs, scalar) buffer ObjDesc_ { ObjDesc i[]; } objDesc;
|
||||
layout(set = 1, binding = eImplicits, scalar) buffer allImplicits_ {Implicit i[];} allImplicits;
|
||||
|
||||
layout(push_constant) uniform _PushConstantRay { PushConstantRay pcRay; };
|
||||
// clang-format on
|
||||
|
||||
layout(push_constant) uniform Constants
|
||||
{
|
||||
vec4 clearColor;
|
||||
vec3 lightPosition;
|
||||
float lightIntensity;
|
||||
vec3 lightDirection;
|
||||
float lightSpotCutoff;
|
||||
float lightSpotOuterCutoff;
|
||||
int lightType;
|
||||
}
|
||||
pushC;
|
||||
|
||||
layout(location = 3) callableDataEXT rayLight cLight;
|
||||
|
||||
|
|
@ -92,10 +82,10 @@ void main()
|
|||
}
|
||||
|
||||
cLight.inHitPosition = worldPos;
|
||||
executeCallableEXT(pushC.lightType, 3);
|
||||
executeCallableEXT(pcRay.lightType, 3);
|
||||
|
||||
// Material of the object
|
||||
SceneDesc objResource = sceneDesc.i[gl_InstanceCustomIndexEXT];
|
||||
ObjDesc objResource = objDesc.i[gl_InstanceCustomIndexEXT];
|
||||
Materials materials = Materials(objResource.materialAddress);
|
||||
WaveFrontMaterial mat = materials.m[impl.matId];
|
||||
|
||||
|
|
|
|||
|
|
@ -26,41 +26,26 @@
|
|||
|
||||
#include "wavefront.glsl"
|
||||
|
||||
// clang-format off
|
||||
layout(binding = 1, scalar) buffer SceneDesc_ { SceneDesc i[]; } 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;
|
||||
float lightIntensity;
|
||||
vec3 lightDirection;
|
||||
float lightSpotCutoff;
|
||||
float lightSpotOuterCutoff;
|
||||
uint instanceId;
|
||||
int lightType;
|
||||
}
|
||||
pushC;
|
||||
PushConstantRaster pcRaster;
|
||||
};
|
||||
|
||||
layout(location = 0) in vec3 inPosition;
|
||||
layout(location = 1) in vec3 inNormal;
|
||||
layout(location = 2) in vec3 inColor;
|
||||
layout(location = 3) in vec2 inTexCoord;
|
||||
layout(location = 0) in vec3 i_position;
|
||||
layout(location = 1) in vec3 i_normal;
|
||||
layout(location = 2) in vec3 i_color;
|
||||
layout(location = 3) 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,16 +55,12 @@ out gl_PerVertex
|
|||
|
||||
void main()
|
||||
{
|
||||
mat4 objMatrix = sceneDesc.i[pushC.instanceId].transfo;
|
||||
mat4 objMatrixIT = sceneDesc.i[pushC.instanceId].transfoIT;
|
||||
vec3 origin = vec3(uni.viewInverse * vec4(0, 0, 0, 1));
|
||||
|
||||
vec3 origin = vec3(ubo.viewI * vec4(0, 0, 0, 1));
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,41 +17,7 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
struct Vertex
|
||||
{
|
||||
vec3 pos;
|
||||
vec3 nrm;
|
||||
vec3 color;
|
||||
vec2 texCoord;
|
||||
};
|
||||
|
||||
struct WaveFrontMaterial
|
||||
{
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
vec3 specular;
|
||||
vec3 transmittance;
|
||||
vec3 emission;
|
||||
float shininess;
|
||||
float ior; // index of refraction
|
||||
float dissolve; // 1 == opaque; 0 == fully transparent
|
||||
int illum; // illumination model (see http://www.fileformat.info/format/material/)
|
||||
int textureId;
|
||||
};
|
||||
|
||||
struct SceneDesc
|
||||
{
|
||||
mat4 transfo;
|
||||
mat4 transfoIT;
|
||||
int objId;
|
||||
int txtOffset;
|
||||
|
||||
uint64_t vertexAddress;
|
||||
uint64_t indexAddress;
|
||||
uint64_t materialAddress;
|
||||
uint64_t materialIndexAddress;
|
||||
};
|
||||
|
||||
#include "host_device.h"
|
||||
|
||||
vec3 computeDiffuse(WaveFrontMaterial mat, vec3 lightDir, vec3 normal)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue