Improve performance by tracing all lights in one shader

This commit is contained in:
Magnus Norddahl 2021-11-10 00:37:56 +01:00
parent be1a94931b
commit 43bdb1cbca
5 changed files with 198 additions and 156 deletions

View file

@ -22,16 +22,12 @@ layout(set = 0, binding = 4) uniform Uniforms
uint SampleIndex;
uint SampleCount;
uint PassType;
uint Padding2;
vec3 LightOrigin;
float Padding0;
float LightRadius;
float LightIntensity;
float LightInnerAngleCos;
float LightOuterAngleCos;
vec3 LightDir;
uint LightCount;
vec3 SunDir;
float SampleDistance;
vec3 LightColor;
vec3 SunColor;
float SunIntensity;
vec3 HemisphereVec;
float Padding1;
};
@ -47,9 +43,7 @@ struct SurfaceInfo
layout(set = 0, binding = 6) buffer SurfaceBuffer { SurfaceInfo surfaces[]; };
vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness);
vec2 Hammersley(uint i, uint N);
float RadicalInverse_VdC(uint bits);
vec3 ImportanceSample(vec3 N);
void main()
{
@ -62,6 +56,8 @@ void main()
data0 = imageLoad(startpositions, texelPos);
vec4 incoming = vec4(0.0, 0.0, 0.0, 1.0);
if (PassType != 0)
incoming = imageLoad(outputs, texelPos);
int surfaceIndex = int(data0.w);
if (surfaceIndex >= 0)
@ -77,13 +73,10 @@ void main()
}
else
{
incoming = imageLoad(outputs, texelPos);
if (PassType == 1)
incoming.w = 1.0f / float(SampleCount);
vec2 Xi = Hammersley(SampleIndex, SampleCount);
vec3 H = ImportanceSampleGGX(Xi, normal, 1.0f);
vec3 H = ImportanceSample(normal);
vec3 L = normalize(H * (2.0f * dot(normal, H)) - normal);
float NdotL = max(dot(normal, L), 0.0);
@ -91,11 +84,12 @@ void main()
const float p = 1 / (2 * 3.14159265359);
incoming.w *= NdotL / p;
surfaceIndex = -1;
if (NdotL > 0.0f)
{
const float minDistance = 0.1;
traceRayEXT(acc, gl_RayFlagsOpaqueEXT, 0xff, 0, 0, 0, origin, minDistance, L, 2000, 0);
traceRayEXT(acc, gl_RayFlagsOpaqueEXT, 0xff, 0, 0, 0, origin + normal * 0.1, minDistance, L, 32768, 0);
if (payload.hitAttenuation == 1.0)
{
float hitDistance = distance(origin, payload.hitPosition);
@ -122,39 +116,15 @@ void main()
imageStore(outputs, texelPos, incoming);
}
vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness)
vec3 ImportanceSample(vec3 N)
{
float a = roughness * roughness;
float phi = 2.0f * 3.14159265359 * Xi.x;
float cosTheta = sqrt((1.0f - Xi.y) / (1.0f + (a * a - 1.0f) * Xi.y));
float sinTheta = sqrt(1.0f - cosTheta * cosTheta);
// from spherical coordinates to cartesian coordinates
vec3 H = vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);
// from tangent-space vector to world-space sample vector
vec3 up = abs(N.z) < 0.999f ? vec3(0.0f, 0.0f, 1.0f) : vec3(1.0f, 0.0f, 0.0f);
vec3 up = abs(N.x) < abs(N.y) ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0);
vec3 tangent = normalize(cross(up, N));
vec3 bitangent = cross(N, tangent);
vec3 sampleVec = tangent * H.x + bitangent * H.y + N * H.z;
vec3 sampleVec = tangent * HemisphereVec.x + bitangent * HemisphereVec.y + N * HemisphereVec.z;
return normalize(sampleVec);
}
float RadicalInverse_VdC(uint bits)
{
bits = (bits << 16u) | (bits >> 16u);
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
return float(bits) * 2.3283064365386963e-10f; // / 0x100000000
}
vec2 Hammersley(uint i, uint N)
{
return vec2(float(i) / float(N), RadicalInverse_VdC(i));
}
)glsl";

View file

@ -22,16 +22,12 @@ layout(set = 0, binding = 4) uniform Uniforms
uint SampleIndex;
uint SampleCount;
uint PassType;
uint Padding2;
vec3 LightOrigin;
float Padding0;
float LightRadius;
float LightIntensity;
float LightInnerAngleCos;
float LightOuterAngleCos;
vec3 LightDir;
uint LightCount;
vec3 SunDir;
float SampleDistance;
vec3 LightColor;
vec3 SunColor;
float SunIntensity;
vec3 HemisphereVec;
float Padding1;
};
@ -45,7 +41,22 @@ struct SurfaceInfo
float Padding0, Padding1, Padding2;
};
struct LightInfo
{
vec3 Origin;
float Padding0;
float Radius;
float Intensity;
float InnerAngleCos;
float OuterAngleCos;
vec3 SpotDir;
float Padding1;
vec3 Color;
float Padding2;
};
layout(set = 0, binding = 6) buffer SurfaceBuffer { SurfaceInfo surfaces[]; };
layout(set = 0, binding = 7) buffer LightBuffer { LightInfo lights[]; };
vec2 Hammersley(uint i, uint N);
float RadicalInverse_VdC(uint bits);
@ -59,59 +70,94 @@ void main()
if (surfaceIndex < 0 || incoming.w <= 0.0)
return;
SurfaceInfo surface = surfaces[surfaceIndex];
vec3 normal = surface.Normal;
vec3 origin = data0.xyz + normal * 0.1;
const float minDistance = 0.01;
vec3 origin = data0.xyz;
float dist = distance(LightOrigin, origin);
if (dist > minDistance && dist < LightRadius)
// Sun light
{
vec3 dir = normalize(LightOrigin - origin);
const float dist = 32768.0;
SurfaceInfo surface = surfaces[surfaceIndex];
vec3 normal = surface.Normal;
float distAttenuation = max(1.0 - (dist / LightRadius), 0.0);
float angleAttenuation = max(dot(normal, dir), 0.0);
float spotAttenuation = 1.0;
if (LightOuterAngleCos > -1.0)
float attenuation = 0.0;
if (PassType == 0)
{
float cosDir = dot(dir, LightDir);
spotAttenuation = smoothstep(LightOuterAngleCos, LightInnerAngleCos, cosDir);
spotAttenuation = max(spotAttenuation, 0.0);
vec3 e0 = cross(normal, abs(normal.x) < abs(normal.y) ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0));
vec3 e1 = cross(normal, e0);
e0 = cross(normal, e1);
for (uint i = 0; i < SampleCount; i++)
{
vec2 offset = (Hammersley(i, SampleCount) - 0.5) * SampleDistance;
vec3 origin2 = origin + offset.x * e0 + offset.y * e1;
traceRayEXT(acc, gl_RayFlagsOpaqueEXT, 0xff, 2, 0, 2, origin2, minDistance, SunDir, dist, 0);
attenuation += payload.hitAttenuation;
}
attenuation *= 1.0 / float(SampleCount);
}
float attenuation = distAttenuation * angleAttenuation * spotAttenuation;
if (attenuation > 0.0)
else
{
float shadowAttenuation = 0.0;
traceRayEXT(acc, gl_RayFlagsOpaqueEXT, 0xff, 2, 0, 2, origin, minDistance, SunDir, dist, 0);
attenuation = payload.hitAttenuation;
}
incoming.rgb += SunColor * (attenuation * SunIntensity) * incoming.w;
}
if (PassType == 0)
for (uint j = 0; j < LightCount; j++)
{
LightInfo light = lights[j];
float dist = distance(light.Origin, origin);
if (dist > minDistance && dist < light.Radius)
{
vec3 dir = normalize(light.Origin - origin);
float distAttenuation = max(1.0 - (dist / light.Radius), 0.0);
float angleAttenuation = max(dot(normal, dir), 0.0);
float spotAttenuation = 1.0;
if (light.OuterAngleCos > -1.0)
{
vec3 e0 = cross(normal, abs(normal.x) < abs(normal.y) ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0));
vec3 e1 = cross(normal, e0);
e0 = cross(normal, e1);
for (uint i = 0; i < SampleCount; i++)
float cosDir = dot(dir, light.SpotDir);
spotAttenuation = smoothstep(light.OuterAngleCos, light.InnerAngleCos, cosDir);
spotAttenuation = max(spotAttenuation, 0.0);
}
float attenuation = distAttenuation * angleAttenuation * spotAttenuation;
if (attenuation > 0.0)
{
float shadowAttenuation = 0.0;
if (PassType == 0)
{
vec2 offset = (Hammersley(i, SampleCount) - 0.5) * SampleDistance;
vec3 origin2 = origin + offset.x * e0 + offset.y * e1;
vec3 e0 = cross(normal, abs(normal.x) < abs(normal.y) ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0));
vec3 e1 = cross(normal, e0);
e0 = cross(normal, e1);
for (uint i = 0; i < SampleCount; i++)
{
vec2 offset = (Hammersley(i, SampleCount) - 0.5) * SampleDistance;
vec3 origin2 = origin + offset.x * e0 + offset.y * e1;
float dist2 = distance(LightOrigin, origin2);
vec3 dir2 = normalize(LightOrigin - origin2);
float dist2 = distance(light.Origin, origin2);
vec3 dir2 = normalize(light.Origin - origin2);
traceRayEXT(acc, gl_RayFlagsOpaqueEXT, 0xff, 1, 0, 1, origin2, minDistance, dir2, dist2, 0);
shadowAttenuation += payload.hitAttenuation;
traceRayEXT(acc, gl_RayFlagsOpaqueEXT, 0xff, 1, 0, 1, origin2, minDistance, dir2, dist2, 0);
shadowAttenuation += payload.hitAttenuation;
}
shadowAttenuation *= 1.0 / float(SampleCount);
}
else
{
traceRayEXT(acc, gl_RayFlagsOpaqueEXT, 0xff, 1, 0, 1, origin, minDistance, dir, dist, 0);
shadowAttenuation = payload.hitAttenuation;
}
shadowAttenuation *= 1.0 / float(SampleCount);
}
else
{
traceRayEXT(acc, gl_RayFlagsOpaqueEXT, 0xff, 1, 0, 1, origin, minDistance, dir, dist, 0);
shadowAttenuation = payload.hitAttenuation;
}
attenuation *= shadowAttenuation;
attenuation *= shadowAttenuation;
incoming.rgb += LightColor * (attenuation * LightIntensity) * incoming.w;
incoming.rgb += light.Color * (attenuation * light.Intensity) * incoming.w;
}
}
}

View file

@ -22,16 +22,12 @@ layout(set = 0, binding = 4) uniform Uniforms
uint SampleIndex;
uint SampleCount;
uint PassType;
uint Padding2;
vec3 LightOrigin;
float Padding0;
float LightRadius;
float LightIntensity;
float LightInnerAngleCos;
float LightOuterAngleCos;
vec3 LightDir;
uint LightCount;
vec3 SunDir;
float SampleDistance;
vec3 LightColor;
vec3 SunColor;
float SunIntensity;
vec3 HemisphereVec;
float Padding1;
};
@ -80,18 +76,18 @@ void main()
vec2 offset = (Hammersley(i, SampleCount) - 0.5) * SampleDistance;
vec3 origin2 = origin + offset.x * e0 + offset.y * e1;
traceRayEXT(acc, gl_RayFlagsOpaqueEXT, 0xff, 2, 0, 2, origin2, minDistance, LightDir, dist, 0);
traceRayEXT(acc, gl_RayFlagsOpaqueEXT, 0xff, 2, 0, 2, origin2, minDistance, SunDir, dist, 0);
attenuation += payload.hitAttenuation;
}
attenuation *= 1.0 / float(SampleCount);
}
else
{
traceRayEXT(acc, gl_RayFlagsOpaqueEXT, 0xff, 2, 0, 2, origin, minDistance, LightDir, dist, 0);
traceRayEXT(acc, gl_RayFlagsOpaqueEXT, 0xff, 2, 0, 2, origin, minDistance, SunDir, dist, 0);
attenuation = payload.hitAttenuation;
}
incoming.rgb += LightColor * (attenuation * LightIntensity) * incoming.w;
incoming.rgb += SunColor * (attenuation * SunIntensity) * incoming.w;
imageStore(outputs, texelPos, incoming);
}

View file

@ -74,6 +74,19 @@ void GPURaytracer::Raytrace(LevelMesh* level)
}
}
std::vector<Vec3> HemisphereVectors;
HemisphereVectors.reserve(bounceSampleCount);
for (int i = 0; i < bounceSampleCount; i++)
{
Vec2 Xi = Hammersley(i, bounceSampleCount);
Vec3 H;
H.x = Xi.x * 2.0f - 1.0f;
H.y = Xi.y * 2.0f - 1.0f;
H.z = RadicalInverse_VdC(i) + 0.01f;
H.Normalize();
HemisphereVectors.push_back(H);
}
size_t maxTasks = (size_t)rayTraceImageSize * rayTraceImageSize;
for (size_t startTask = 0; startTask < tasks.size(); startTask += maxTasks)
{
@ -84,56 +97,36 @@ void GPURaytracer::Raytrace(LevelMesh* level)
Uniforms uniforms = {};
uniforms.SampleDistance = (float)mesh->samples;
uniforms.SampleCount = sampleCount;
uniforms.LightCount = mesh->map->ThingLights.Size();
uniforms.SunDir = mesh->map->GetSunDirection();
uniforms.SunColor = mesh->map->GetSunColor();
uniforms.SunIntensity = 1.0f;
uniforms.SampleIndex = 0;
uniforms.SampleCount = bounceSampleCount;
uniforms.PassType = 0;
RunTrace(uniforms, rgenBounceRegion);
uniforms.LightDir = mesh->map->GetSunDirection();
uniforms.LightColor = mesh->map->GetSunColor();
uniforms.LightIntensity = 1.0f;
RunTrace(uniforms, rgenSunRegion);
uniforms.SampleCount = coverageSampleCount;
RunTrace(uniforms, rgenLightRegion);
for (ThingLight& light : mesh->map->ThingLights)
{
uniforms.LightOrigin = light.LightOrigin();
uniforms.LightRadius = light.LightRadius();
uniforms.LightIntensity = light.intensity;
uniforms.LightInnerAngleCos = light.innerAngleCos;
uniforms.LightOuterAngleCos = light.outerAngleCos;
uniforms.LightDir = light.SpotDir();
uniforms.LightColor = light.rgb;
RunTrace(uniforms, rgenLightRegion);
}
for (uint32_t i = 0; i < uniforms.SampleCount; i++)
for (uint32_t i = 0; i < (uint32_t)bounceSampleCount; i++)
{
uniforms.PassType = 1;
uniforms.SampleIndex = i;
uniforms.SampleCount = bounceSampleCount;
uniforms.HemisphereVec = HemisphereVectors[uniforms.SampleIndex];
RunTrace(uniforms, rgenBounceRegion);
for (int bounce = 0; bounce < LightBounce; bounce++)
{
uniforms.LightDir = mesh->map->GetSunDirection();
uniforms.LightColor = mesh->map->GetSunColor();
uniforms.LightIntensity = 1.0f;
RunTrace(uniforms, rgenSunRegion);
for (ThingLight& light : mesh->map->ThingLights)
{
uniforms.LightOrigin = light.LightOrigin();
uniforms.LightRadius = light.LightRadius();
uniforms.LightIntensity = light.intensity;
uniforms.LightInnerAngleCos = light.innerAngleCos;
uniforms.LightOuterAngleCos = light.outerAngleCos;
uniforms.LightDir = light.SpotDir();
uniforms.LightColor = light.rgb;
RunTrace(uniforms, rgenLightRegion);
}
uniforms.SampleCount = coverageSampleCount;
RunTrace(uniforms, rgenLightRegion);
uniforms.PassType = 2;
uniforms.SampleCount = bounceSampleCount;
uniforms.SampleIndex = (i + bounce) % uniforms.SampleCount;
uniforms.HemisphereVec = HemisphereVectors[uniforms.SampleIndex];
RunTrace(uniforms, rgenBounceRegion);
}
}
@ -186,8 +179,7 @@ void GPURaytracer::UploadTasks(const SurfaceTask* tasks, size_t size)
const SurfaceTask& task = tasks[i];
Surface* surface = mesh->surfaces[task.surf].get();
Vec3 normal = surface->plane.Normal();
Vec3 pos = surface->lightmapOrigin + normal + surface->lightmapSteps[0] * (float)task.x + surface->lightmapSteps[1] * (float)task.y;
Vec3 pos = surface->lightmapOrigin + surface->lightmapSteps[0] * (float)task.x + surface->lightmapSteps[1] * (float)task.y;
startPositions[i] = Vec4(pos, (float)task.surf);
}
@ -367,15 +359,31 @@ void GPURaytracer::CreateVertexAndIndexBuffers()
surfaces.push_back(info);
}
std::vector<LightInfo> lights;
for (ThingLight& light : mesh->map->ThingLights)
{
LightInfo info;
info.Origin = light.LightOrigin();
info.Radius = light.LightRadius();
info.Intensity = light.intensity;
info.InnerAngleCos = light.innerAngleCos;
info.OuterAngleCos = light.outerAngleCos;
info.SpotDir = light.SpotDir();
info.Color = light.rgb;
lights.push_back(info);
}
size_t vertexbuffersize = (size_t)mesh->MeshVertices.Size() * sizeof(Vec3);
size_t indexbuffersize = (size_t)mesh->MeshElements.Size() * sizeof(uint32_t);
size_t surfaceindexbuffersize = (size_t)mesh->MeshSurfaces.Size() * sizeof(uint32_t);
size_t surfacebuffersize = (size_t)surfaces.size() * sizeof(SurfaceInfo);
size_t transferbuffersize = vertexbuffersize + indexbuffersize + surfaceindexbuffersize + surfacebuffersize;
size_t lightbuffersize = (size_t)lights.size() * sizeof(LightInfo);
size_t transferbuffersize = vertexbuffersize + indexbuffersize + surfaceindexbuffersize + surfacebuffersize + lightbuffersize;
size_t vertexoffset = 0;
size_t indexoffset = vertexoffset + vertexbuffersize;
size_t surfaceindexoffset = indexoffset + indexbuffersize;
size_t surfaceoffset = surfaceindexoffset + surfaceindexbuffersize;
size_t lightoffset = surfaceoffset + surfacebuffersize;
BufferBuilder vbuilder;
vbuilder.setUsage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
@ -401,6 +409,12 @@ void GPURaytracer::CreateVertexAndIndexBuffers()
surfaceBuffer = sbuilder.create(device.get());
surfaceBuffer->SetDebugName("surfaceBuffer");
BufferBuilder lbuilder;
lbuilder.setUsage(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
lbuilder.setSize(lightbuffersize);
lightBuffer = sbuilder.create(device.get());
lightBuffer->SetDebugName("lightBuffer");
BufferBuilder tbuilder;
tbuilder.setUsage(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY);
tbuilder.setSize(transferbuffersize);
@ -411,12 +425,14 @@ void GPURaytracer::CreateVertexAndIndexBuffers()
memcpy(data + indexoffset, mesh->MeshElements.Data(), indexbuffersize);
memcpy(data + surfaceindexoffset, mesh->MeshSurfaces.Data(), surfaceindexbuffersize);
memcpy(data + surfaceoffset, surfaces.data(), surfacebuffersize);
memcpy(data + lightoffset, lights.data(), lightbuffersize);
transferBuffer->Unmap();
cmdbuffer->copyBuffer(transferBuffer.get(), vertexBuffer.get(), vertexoffset);
cmdbuffer->copyBuffer(transferBuffer.get(), indexBuffer.get(), indexoffset);
cmdbuffer->copyBuffer(transferBuffer.get(), surfaceIndexBuffer.get(), surfaceindexoffset);
cmdbuffer->copyBuffer(transferBuffer.get(), surfaceBuffer.get(), surfaceoffset);
cmdbuffer->copyBuffer(transferBuffer.get(), lightBuffer.get(), lightoffset);
VkMemoryBarrier barrier = { VK_STRUCTURE_TYPE_MEMORY_BARRIER };
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
@ -663,6 +679,7 @@ void GPURaytracer::CreatePipeline()
setbuilder.addBinding(4, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
setbuilder.addBinding(5, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR);
setbuilder.addBinding(6, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_RAYGEN_BIT_KHR | VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR);
setbuilder.addBinding(7, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
descriptorSetLayout = setbuilder.create(device.get());
descriptorSetLayout->SetDebugName("descriptorSetLayout");
@ -837,7 +854,7 @@ void GPURaytracer::CreateDescriptorSet()
poolbuilder.addPoolSize(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 1);
poolbuilder.addPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 3);
poolbuilder.addPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1);
poolbuilder.addPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2);
poolbuilder.addPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 3);
poolbuilder.setMaxSets(1);
descriptorPool = poolbuilder.create(device.get());
descriptorPool->SetDebugName("descriptorPool");
@ -853,6 +870,7 @@ void GPURaytracer::CreateDescriptorSet()
write.addBuffer(descriptorSet.get(), 4, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, uniformBuffer.get());
write.addBuffer(descriptorSet.get(), 5, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, surfaceIndexBuffer.get());
write.addBuffer(descriptorSet.get(), 6, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, surfaceBuffer.get());
write.addBuffer(descriptorSet.get(), 7, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, lightBuffer.get());
write.updateSets(device.get());
}
@ -894,15 +912,15 @@ void GPURaytracer::RaytraceProbeSample(LightProbeSample* probe)
{ 0.0f, 0.0f, 1.0f, },
{ 0.0f, 0.0f, -1.0f, }
};
for (int i = 0; i < sampleCount; i++)
for (int i = 0; i < bounceSampleCount; i++)
{
const Vec3& normal = directions[i % 6];
Vec2 Xi = Hammersley(i, sampleCount);
Vec2 Xi = Hammersley(i, bounceSampleCount);
Vec3 H = ImportanceSampleGGX(Xi, normal, 1.0f);
Vec3 L = Vec3::Normalize(H * (2.0f * Vec3::Dot(normal, H)) - normal);
incoming += TracePath(probe->Position, L, i);
}
incoming = incoming / (float)sampleCount / (float)LightBounce;
incoming = incoming / (float)bounceSampleCount / (float)LightBounce;
}
for (ThingLight& light : mesh->map->ThingLights)
@ -985,7 +1003,7 @@ Vec3 GPURaytracer::TracePath(const Vec3& pos, const Vec3& dir, int sampleIndex,
emittance += mesh->map->GetSunColor() * (attenuation * 0.5f);
}
Vec2 Xi = Hammersley(sampleIndex, sampleCount);
Vec2 Xi = Hammersley(sampleIndex, bounceSampleCount);
Vec3 H = ImportanceSampleGGX(Xi, normal, 1.0f);
Vec3 L = Vec3::Normalize(H * (2.0f * Vec3::Dot(normal, H)) - normal);
@ -994,7 +1012,7 @@ Vec3 GPURaytracer::TracePath(const Vec3& pos, const Vec3& dir, int sampleIndex,
return emittance;
const float p = 1 / (2 * M_PI);
Vec3 incoming = TracePath(hitpos, L, (sampleIndex + depth + 1) % sampleCount, depth + 1);
Vec3 incoming = TracePath(hitpos, L, (sampleIndex + depth + 1) % bounceSampleCount, depth + 1);
return emittance + incoming * NdotL / p;
}

View file

@ -11,16 +11,12 @@ struct Uniforms
uint32_t SampleIndex;
uint32_t SampleCount;
uint32_t PassType;
uint32_t Padding2;
Vec3 LightOrigin;
float Padding0;
float LightRadius;
float LightIntensity;
float LightInnerAngleCos;
float LightOuterAngleCos;
Vec3 LightDir;
uint32_t LightCount;
Vec3 SunDir;
float SampleDistance;
Vec3 LightColor;
Vec3 SunColor;
float SunIntensity;
Vec3 HemisphereVec;
float Padding1;
};
@ -34,6 +30,20 @@ struct SurfaceInfo
float Padding0, Padding1, Padding2;
};
struct LightInfo
{
Vec3 Origin;
float Padding0;
float Radius;
float Intensity;
float InnerAngleCos;
float OuterAngleCos;
Vec3 SpotDir;
float Padding1;
Vec3 Color;
float Padding2;
};
struct SurfaceTask
{
int surf, x, y;
@ -78,7 +88,8 @@ private:
static Vec2 Hammersley(uint32_t i, uint32_t N);
static Vec3 ImportanceSampleGGX(Vec2 Xi, Vec3 N, float roughness);
const int sampleCount = 1024;
const int coverageSampleCount = 256;
const int bounceSampleCount = 2048;
const int uniformStructs = 256;
int rayTraceImageSize = 1024;
@ -94,6 +105,7 @@ private:
std::unique_ptr<VulkanBuffer> transferBuffer;
std::unique_ptr<VulkanBuffer> surfaceIndexBuffer;
std::unique_ptr<VulkanBuffer> surfaceBuffer;
std::unique_ptr<VulkanBuffer> lightBuffer;
std::unique_ptr<VulkanBuffer> blScratchBuffer;
std::unique_ptr<VulkanBuffer> blAccelStructBuffer;