Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge from main #6

Merged
merged 3 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion shaders/host_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ esTangent = 12,
esUniformGrid = 13,
esNonUniformGrid = 14,
esReflection = 15,
esNoReflection = 16
esOcclusion = 16
END_ENUM();

// Surfel data
Expand Down Expand Up @@ -227,6 +227,7 @@ struct SceneCamera
float fov;
// Extra
int nbLights;
mat4 prevViewProj;
};

struct VertexAttributes
Expand Down
92 changes: 89 additions & 3 deletions shaders/lightPass.frag
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,73 @@ vec3 hsv2rgb(vec3 c) {
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

const int KERNEL_SIZE = 64;
const vec3 kernel[KERNEL_SIZE] = vec3[KERNEL_SIZE](
vec3(0.0381, 0.0253, 0.0659),
vec3(-0.0659, 0.0462, 0.0968),
vec3(0.0483, -0.0659, 0.0771),
vec3(-0.0659, -0.0462, 0.0659),
vec3(0.0513, 0.0634, 0.0344),
vec3(-0.0234, 0.0632, 0.0865),
vec3(0.0854, -0.0234, 0.0543),
vec3(-0.0325, -0.0432, 0.0865),
vec3(0.0854, 0.0342, 0.0254),
vec3(-0.0157, 0.0654, 0.0785),
vec3(0.0864, -0.0123, 0.0432),
vec3(-0.0765, -0.0654, 0.0324),
vec3(0.0534, 0.0864, 0.0234),
vec3(-0.0435, 0.0234, 0.0876),
vec3(0.0864, -0.0543, 0.0321),
vec3(-0.0234, -0.0765, 0.0543),
vec3(0.0654, 0.0123, 0.0865),
vec3(-0.0543, 0.0876, 0.0234),
vec3(0.0234, -0.0321, 0.0987),
vec3(-0.0876, -0.0234, 0.0432),
vec3(0.0123, 0.0987, 0.0321),
vec3(-0.0321, 0.0432, 0.0876),
vec3(0.0987, -0.0876, 0.0123),
vec3(-0.0432, -0.0321, 0.0987),
vec3(0.0876, 0.0123, 0.0432),
vec3(-0.0987, 0.0876, 0.0321),
vec3(0.0321, -0.0432, 0.0876),
vec3(-0.0123, -0.0987, 0.0432),
vec3(0.0432, 0.0321, 0.0987),
vec3(-0.0876, 0.0123, 0.0432),
vec3(0.0987, -0.0876, 0.0321),
vec3(-0.0321, -0.0432, 0.0876),
vec3(0.0432, 0.0987, 0.0123),
vec3(-0.0987, 0.0321, 0.0876),
vec3(0.0123, -0.0876, 0.0432),
vec3(-0.0876, -0.0432, 0.0321),
vec3(0.0321, 0.0876, 0.0987),
vec3(-0.0432, 0.0123, 0.0432),
vec3(0.0987, -0.0987, 0.0321),
vec3(-0.0123, -0.0321, 0.0876),
vec3(0.0876, 0.0432, 0.0432),
vec3(-0.0321, 0.0987, 0.0876),
vec3(0.0432, -0.0123, 0.0321),
vec3(-0.0987, -0.0876, 0.0987),
vec3(0.0123, 0.0321, 0.0432),
vec3(-0.0876, 0.0432, 0.0876),
vec3(0.0321, -0.0987, 0.0321),
vec3(-0.0432, -0.0123, 0.0987),
vec3(0.0987, 0.0876, 0.0432),
vec3(-0.0123, 0.0321, 0.0876),
vec3(0.0876, -0.0432, 0.0321),
vec3(-0.0321, -0.0987, 0.0987),
vec3(0.0432, 0.0123, 0.0432),
vec3(-0.0987, 0.0876, 0.0876),
vec3(0.0123, -0.0321, 0.0321),
vec3(-0.0876, -0.0432, 0.0987),
vec3(0.0321, 0.0987, 0.0432),
vec3(-0.0432, 0.0123, 0.0876),
vec3(0.0987, -0.0876, 0.0321),
vec3(-0.0123, -0.0321, 0.0987),
vec3(0.0876, 0.0432, 0.0432),
vec3(-0.0321, 0.0987, 0.0876),
vec3(0.0432, -0.0123, 0.0321),
vec3(0.0278, 0.0368, 0.0165)
);

void main()
{
Expand Down Expand Up @@ -162,7 +229,26 @@ void main()
float dist = distance(randLight.position, worldPos);
//fragColor.xyz = directLighting + diffuseAlbedo * 1 / (dist * dist) * randLight.color * randLight.intensity;

fragColor.xyz = directLighting + diffuseAlbedo * indirectLight + state.mat.emission + reflectionColor;
//SSAO
float occlusion = 0.0;
float radius = 0.02;
float bias = 0.005;
vec4 viewPos = sceneCamera.view * vec4(worldPos, 1.0);
for(int i = 0; i < KERNEL_SIZE; i++) {
vec3 samplePos = viewPos.xyz + kernel[i] * radius;
vec3 sampleWorldPos = worldPos + kernel[i] * radius;
vec4 offset = sceneCamera.proj * vec4(samplePos, 1.0);
offset.xyz /= offset.w;
offset.xyz = offset.xyz * 0.5 + 0.5;
vec2 sampleuv = offset.xy;
ivec2 pixelCoord = ivec2(sampleuv * vec2(rtxState.size));
float sampleDepth = texelFetch(gbufferDepth, pixelCoord, 0).r;
float rangeCheck = smoothstep(0.0, 1.0, radius / abs(depth - sampleDepth ));
occlusion += ( sampleDepth > offset.z - bias ? 1.0 : 0.0) * rangeCheck;
}
occlusion = 1.0 - (occlusion / float(KERNEL_SIZE));

fragColor.xyz = (directLighting + diffuseAlbedo * indirectLight + state.mat.emission + reflectionColor) * occlusion;

if(rtxState.debugging_mode != eNoDebug)
{
Expand Down Expand Up @@ -197,8 +283,8 @@ void main()
fragColor.xyz = reflectionColor;
fragColor.a = 1.0;
}
else if (rtxState.debugging_mode == esNoReflection){
fragColor.a = 1.0;
else if (rtxState.debugging_mode == esOcclusion){
fragColor.xyz = vec3(occlusion);
}
else{
fragColor.xyz = indirectLight;
Expand Down
13 changes: 9 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ int main(int argc, char **argv)
#else
setenv("VK_LAYER_SETTINGS_PATH", "../vk_layer_settings.txt", 1);
#endif

InputParser parser(argc, argv);
std::string sceneFile = parser.getString("-f", "Sponza/Sponza.gltf");

// std::string sceneFile = parser.getString("-f", "Sponza/Sponza.gltf");
// std::string sceneFile = parser.getString("-f", "Street/scene.gltf");
// std::string sceneFile = parser.getString("-f", "Hospital/scene.gltf");
// std::string sceneFile = parser.getString("-f", "station.glb");
std::string sceneFile = parser.getString("-f", "station.glb");

std::string hdrFilename = parser.getString("-e", "std_env.hdr");

// Setup GLFW window
Expand Down Expand Up @@ -144,7 +145,7 @@ int main(int argc, char **argv)
assert(!compatibleDevices.empty());
vkctx.initDevice(compatibleDevices[0], contextInfo); // Use first compatible device

//

SampleExample sample;
sample.supportRayQuery(vkctx.hasDeviceExtension(VK_KHR_RAY_QUERY_EXTENSION_NAME));

Expand Down Expand Up @@ -199,6 +200,10 @@ int main(int argc, char **argv)
sample.m_busy = false; })
.detach();

// create a sequence of random numbers
sample.initHammerleySequence(16);


// Profiler measure the execution time on the GPU
nvvk::ProfilerVK profiler;
std::string profilerStats;
Expand Down
35 changes: 34 additions & 1 deletion src/sample_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ void SampleExample::updateFrame()
m_rtxState.frame++;
m_rtxState.totalFrames++;
}


// update scene frame
m_scene.setCurrFrame(m_rtxState.frame);
m_scene.setSize(m_size);
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -922,3 +925,33 @@ void SampleExample::computeReflection(const VkCommandBuffer& cmdBuf, nvvk::Profi
static_cast<uint32_t>(barriers.size()),
barriers.data());
}

vec2 Hammersley(float i, float numSamples)
{
uint b = uint(i);

b = (b << 16u) | (b >> 16u);
b = ((b & 0x55555555u) << 1u) | ((b & 0xAAAAAAAAu) >> 1u);
b = ((b & 0x33333333u) << 2u) | ((b & 0xCCCCCCCCu) >> 2u);
b = ((b & 0x0F0F0F0Fu) << 4u) | ((b & 0xF0F0F0F0u) >> 4u);
b = ((b & 0x00FF00FFu) << 8u) | ((b & 0xFF00FF00u) >> 8u);

float radicalInverseVDC = float(b) * 2.3283064365386963e-10;

return vec2((i / numSamples), radicalInverseVDC);
}

std::vector<vec2> SampleExample::hammersleySequence(int maxNumberPoints)
{
std::vector<vec2> points;
for (int i = 0; i < maxNumberPoints; i++)
{
points.push_back(Hammersley(i, maxNumberPoints));
}
return points;
}

void SampleExample::initHammerleySequence(int maxNumberPoints)
{
m_scene.setHammersleySequence(hammersleySequence(maxNumberPoints));
}
2 changes: 2 additions & 0 deletions src/sample_example.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ class SampleExample : public nvvkhl::AppBaseVk

void calculateSurfels(const VkCommandBuffer& cmdBuf, nvvk::ProfilerVK& profiler);
void computeReflection(const VkCommandBuffer& cmdBuf, nvvk::ProfilerVK& profiler);
std::vector<vec2> hammersleySequence(int maxNumberPoints);
void initHammerleySequence(int maxNumberPoints);

RtxState m_rtxState{
0, // frame;
Expand Down
2 changes: 1 addition & 1 deletion src/sample_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ bool SampleGUI::guiRayTracing()
"UniformGrid",
"NonUniformGrid",
"ReflectionOnly",
"NoReflection"
"Occlusion"
});

if(rtxState.debugging_mode == eHeatmap)
Expand Down
1 change: 1 addition & 0 deletions src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ void Scene::updateCamera(const VkCommandBuffer &cmdBuf, float aspectRatio)
auto proj = glm::perspectiveRH_ZO(glm::radians(CameraManip.getFov()), aspectRatio, 1.f, 1000.0f);
proj[1][1] *= -1;


// Apply jittering
if (!m_hammersleySeq.empty())
{
Expand Down
9 changes: 9 additions & 0 deletions src/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ class Scene
void setDirty(bool dirty) { m_lights.dirty = dirty; }
bool getDirty() { return m_lights.dirty; }

void setHammersleySequence(const std::vector<vec2>& seq) { m_hammersleySeq = seq; }
void setCurrFrame(int frame) { m_frame = frame; }
void setSize(VkExtent2D size) { m_size = size; }

private:
void createTextureImages(VkCommandBuffer cmdBuf, tinygltf::Model& gltfModel);
void createDescriptorSet(const nvh::GltfScene& gltf);
Expand Down Expand Up @@ -128,4 +132,9 @@ class Scene
VkDescriptorPool m_descPool{VK_NULL_HANDLE};
VkDescriptorSetLayout m_descSetLayout{VK_NULL_HANDLE};
VkDescriptorSet m_descSet{VK_NULL_HANDLE};

// Hammerley sequence
std::vector<vec2> m_hammersleySeq;
int m_frame{ 0 };
VkExtent2D m_size{ 0, 0 };
};