Skip to content

Commit

Permalink
Minor shading tweaks
Browse files Browse the repository at this point in the history
- Add camera position and basic specular with fixed power
- Use cull mask 0xff for now as it's faster on amdvlk
  • Loading branch information
zeux committed Dec 1, 2024
1 parent 7dd21a0 commit f25d8d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/niagara.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ bool occlusionEnabled = true;
bool clusterOcclusionEnabled = true;
bool taskShadingEnabled = false; // disabled to have good performance on AMD HW
bool shadingEnabled = true;

int debugLodStep = 0;

VkSemaphore createSemaphore(VkDevice device)
Expand Down Expand Up @@ -121,8 +122,10 @@ struct alignas(16) Globals

struct alignas(16) ShadeData
{
vec3 cameraPosition;
float pad0;
vec3 sunDirection;
float padding;
float pad1;

mat4 inverseViewProjection;

Expand Down Expand Up @@ -1269,7 +1272,7 @@ int main(int argc, const char** argv)

if (clusterSubmit)
{
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, postPass == 1 ? clusterpostPipeline : clusterPipeline);
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, postPass >= 1 ? clusterpostPipeline : clusterPipeline);

DescriptorInfo descriptors[] = { dcb.buffer, db.buffer, mlb.buffer, mdb.buffer, vb.buffer, cib.buffer, DescriptorInfo(), textureSampler };
vkCmdPushDescriptorSetWithTemplateKHR(commandBuffer, clusterProgram.updateTemplate, clusterProgram.layout, 0, descriptors);
Expand All @@ -1281,7 +1284,7 @@ int main(int argc, const char** argv)
}
else if (taskSubmit)
{
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, postPass == 1 ? meshtaskpostPipeline : late ? meshtasklatePipeline
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, postPass >= 1 ? meshtaskpostPipeline : late ? meshtasklatePipeline
: meshtaskPipeline);

DescriptorInfo pyramidDesc(depthSampler, depthPyramid.imageView, VK_IMAGE_LAYOUT_GENERAL);
Expand All @@ -1295,7 +1298,7 @@ int main(int argc, const char** argv)
}
else
{
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, postPass == 1 ? meshpostPipeline : meshPipeline);
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, postPass >= 1 ? meshpostPipeline : meshPipeline);

DescriptorInfo descriptors[] = { dcb.buffer, db.buffer, vb.buffer, DescriptorInfo(), DescriptorInfo(), DescriptorInfo(), DescriptorInfo(), textureSampler };
vkCmdPushDescriptorSetWithTemplateKHR(commandBuffer, meshProgram.updateTemplate, meshProgram.layout, 0, descriptors);
Expand Down Expand Up @@ -1438,6 +1441,7 @@ int main(int argc, const char** argv)
vkCmdPushDescriptorSetWithTemplateKHR(commandBuffer, shadeProgram.updateTemplate, shadeProgram.layout, 0, descriptors);

ShadeData shadeData = {};
shadeData.cameraPosition = camera.position;
shadeData.sunDirection = sunDirection;
shadeData.inverseViewProjection = inverse(projection * view);
shadeData.imageSize = vec2(float(swapchain.width), float(swapchain.height));
Expand Down
16 changes: 12 additions & 4 deletions src/shaders/shade.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

struct ShadeData
{
vec3 cameraPosition;
vec3 sunDirection;
float padding;

mat4 inverseViewProjection;

Expand Down Expand Up @@ -49,17 +49,25 @@ void main()
vec4 wposh = shadeData.inverseViewProjection * clip;
vec3 wpos = wposh.xyz / wposh.w;

vec3 view = normalize(shadeData.cameraPosition - wpos);
vec3 halfv = normalize(view + shadeData.sunDirection);
float ndoth = max(dot(normal, halfv), 0.0);
float specular = pow(ndoth, 64);

float shadow = 1;

#if RAYTRACE
uint rayflags = gl_RayFlagsTerminateOnFirstHitEXT | gl_RayFlagsCullNoOpaqueEXT;
uint cullmask = 0xff; // note: 0xff is faster on amdvlk but 1 is faster on radv

rayQueryEXT rq;
rayQueryInitializeEXT(rq, tlas, rayflags, /* cullMask= */ 1, wpos, 1e-2, shadeData.sunDirection, 1e3);
rayQueryInitializeEXT(rq, tlas, rayflags, cullmask, wpos, 1e-2, shadeData.sunDirection, 1e3);
rayQueryProceedEXT(rq);

ndotl *= (rayQueryGetIntersectionTypeEXT(rq, true) == gl_RayQueryCommittedIntersectionNoneEXT) ? 1.0 : 0.05;
shadow = (rayQueryGetIntersectionTypeEXT(rq, true) == gl_RayQueryCommittedIntersectionNoneEXT) ? 1.0 : 0.0;
#endif

vec3 outputColor = albedo.rgb * sqrt(ndotl + 0.05) + emissive;
vec3 outputColor = albedo.rgb * sqrt(ndotl * shadow + 0.05) + vec3(specular * shadow) + emissive;

imageStore(outImage, ivec2(pos), vec4(outputColor, 1.0));
}

0 comments on commit f25d8d9

Please sign in to comment.