From 0194c08e2d9ec779eac8417df79df36900554ef9 Mon Sep 17 00:00:00 2001 From: Turanszki Janos Date: Thu, 1 Apr 2021 12:22:40 +0200 Subject: [PATCH] raytracing updates --- Editor/Editor.cpp | 2 +- WickedEngine/shaders/objectHF.hlsli | 5 +- WickedEngine/shaders/raytraceCS.hlsl | 94 +++++++++++-------- WickedEngine/shaders/renderlightmapPS.hlsl | 101 +++++++++++++++++---- WickedEngine/shaders/rtaoLIB.hlsl | 2 +- WickedEngine/shaders/rtreflectionLIB.hlsl | 7 +- WickedEngine/shaders/rtshadowLIB.hlsl | 2 +- WickedEngine/wiVersion.cpp | 2 +- 8 files changed, 147 insertions(+), 68 deletions(-) diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index 28b64164f..249651037 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -1058,7 +1058,7 @@ void EditorComponent::Update(float dt) wiInput::HidePointer(false); } - const float buttonrotSpeed = 2.0f / 60.0f; + const float buttonrotSpeed = 2.0f * dt; if (wiInput::Down(wiInput::KEYBOARD_BUTTON_LEFT)) { xDif -= buttonrotSpeed; diff --git a/WickedEngine/shaders/objectHF.hlsli b/WickedEngine/shaders/objectHF.hlsli index 1856db698..c18c2dbbf 100644 --- a/WickedEngine/shaders/objectHF.hlsli +++ b/WickedEngine/shaders/objectHF.hlsli @@ -1321,8 +1321,9 @@ float4 main(PixelInput input) : SV_TARGET if (GetMaterial().uvset_baseColorMap >= 0 && (g_xFrame_Options & OPTION_BIT_DISABLE_ALBEDO_MAPS) == 0) { const float2 UV_baseColorMap = GetMaterial().uvset_baseColorMap == 0 ? input.uvsets.xy : input.uvsets.zw; - color = texture_basecolormap.Sample(sampler_objectshader, UV_baseColorMap); - color.rgb = DEGAMMA(color.rgb); + float4 baseColorMap = texture_basecolormap.Sample(sampler_objectshader, UV_baseColorMap); + baseColorMap.rgb = DEGAMMA(baseColorMap.rgb); + color *= baseColorMap; } #endif // OBJECTSHADER_USE_UVSETS diff --git a/WickedEngine/shaders/raytraceCS.hlsl b/WickedEngine/shaders/raytraceCS.hlsl index 4467ac65f..e794692a4 100644 --- a/WickedEngine/shaders/raytraceCS.hlsl +++ b/WickedEngine/shaders/raytraceCS.hlsl @@ -142,8 +142,9 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) if (material.texture_basecolormap_index >= 0 && (g_xFrame_Options & OPTION_BIT_DISABLE_ALBEDO_MAPS) == 0) { const float2 UV_baseColorMap = material.uvset_baseColorMap == 0 ? uvsets.xy : uvsets.zw; - baseColor = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_linear_wrap, UV_baseColorMap, 2); - baseColor.rgb *= DEGAMMA(baseColor.rgb); + float4 baseColorMap = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_linear_wrap, UV_baseColorMap, 2); + baseColorMap.rgb *= DEGAMMA(baseColorMap.rgb); + baseColor *= baseColorMap; } [branch] @@ -200,6 +201,14 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) surface.emissiveColor *= emissiveMap; } + float transmission = material.transmission; + if (material.texture_transmissionmap_index >= 0) + { + const float2 UV_transmissionMap = material.uvset_transmissionMap == 0 ? uvsets.xy : uvsets.zw; + float transmissionMap = bindless_textures[material.texture_transmissionmap_index].SampleLevel(sampler_linear_wrap, UV_transmissionMap, 2).r; + transmission *= transmissionMap; + } + #else // ray origin updated for next bounce: @@ -223,19 +232,15 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) uvsets = frac(uvsets); // emulate wrap - float4 baseColor; + float4 baseColor = material.baseColor * color; [branch] if (material.uvset_baseColorMap >= 0 && (g_xFrame_Options & OPTION_BIT_DISABLE_ALBEDO_MAPS) == 0) { const float2 UV_baseColorMap = material.uvset_baseColorMap == 0 ? uvsets.xy : uvsets.zw; - baseColor = materialTextureAtlas.SampleLevel(sampler_linear_clamp, UV_baseColorMap * material.baseColorAtlasMulAdd.xy + material.baseColorAtlasMulAdd.zw, 0); - baseColor.rgb = DEGAMMA(baseColor.rgb); + float4 baseColorMap = materialTextureAtlas.SampleLevel(sampler_linear_clamp, UV_baseColorMap * material.baseColorAtlasMulAdd.xy + material.baseColorAtlasMulAdd.zw, 0); + baseColorMap.rgb = DEGAMMA(baseColorMap.rgb); + baseColor *= baseColorMap; } - else - { - baseColor = 1; - } - baseColor *= color; float4 surfaceMap = 1; [branch] @@ -268,6 +273,8 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) N = normalize(lerp(N, mul(normalMap, TBN), material.normalMapStrength)); } + float transmission = material.transmission; + #endif // RTAPI float3 P = ray.origin; @@ -299,7 +306,7 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) } else { - const float refractChance = material.transmission; + const float refractChance = transmission; roulette = rand(seed, screenUV); if (roulette < refractChance) { @@ -469,8 +476,7 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) if (NdotL > 0 && dist > 0) { - lighting.direct.diffuse = max(0, lighting.direct.diffuse); - lighting.direct.specular = max(0, lighting.direct.specular); + float3 shadow = NdotL * current_energy; float3 sampling_offset = float3(rand(seed, screenUV), rand(seed, screenUV), rand(seed, screenUV)) * 2 - 1; // todo: should be specific to light surface @@ -501,56 +507,64 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) { continue; } - [branch] - if (material.texture_basecolormap_index < 0) - { - q.CommitNonOpaqueTriangleHit(); - continue; - } uint startIndex = q.CandidatePrimitiveIndex() * 3 + subset.indexOffset; uint i0 = bindless_ib[mesh.ib][startIndex + 0]; uint i1 = bindless_ib[mesh.ib][startIndex + 1]; uint i2 = bindless_ib[mesh.ib][startIndex + 2]; - float2 uv0 = 0, uv1 = 0, uv2 = 0; + float4 uv0 = 0, uv1 = 0, uv2 = 0; [branch] - if (mesh.vb_uv0 >= 0 && material.uvset_baseColorMap == 0) + if (mesh.vb_uv0 >= 0) { - uv0 = unpack_half2(bindless_buffers[mesh.vb_uv0].Load(i0 * 4)); - uv1 = unpack_half2(bindless_buffers[mesh.vb_uv0].Load(i1 * 4)); - uv2 = unpack_half2(bindless_buffers[mesh.vb_uv0].Load(i2 * 4)); + uv0.xy = unpack_half2(bindless_buffers[mesh.vb_uv0].Load(i0 * 4)); + uv1.xy = unpack_half2(bindless_buffers[mesh.vb_uv0].Load(i1 * 4)); + uv2.xy = unpack_half2(bindless_buffers[mesh.vb_uv0].Load(i2 * 4)); } - else if (mesh.vb_uv1 >= 0 && material.uvset_baseColorMap != 0) + [branch] + if (mesh.vb_uv1 >= 0) { - uv0 = unpack_half2(bindless_buffers[mesh.vb_uv1].Load(i0 * 4)); - uv1 = unpack_half2(bindless_buffers[mesh.vb_uv1].Load(i1 * 4)); - uv2 = unpack_half2(bindless_buffers[mesh.vb_uv1].Load(i2 * 4)); - } - else - { - q.CommitNonOpaqueTriangleHit(); - continue; + uv0.zw = unpack_half2(bindless_buffers[mesh.vb_uv1].Load(i0 * 4)); + uv1.zw = unpack_half2(bindless_buffers[mesh.vb_uv1].Load(i1 * 4)); + uv2.zw = unpack_half2(bindless_buffers[mesh.vb_uv1].Load(i2 * 4)); } float2 barycentrics = q.CandidateTriangleBarycentrics(); float u = barycentrics.x; float v = barycentrics.y; float w = 1 - u - v; - float2 uv = uv0 * w + uv1 * u + uv2 * v; - float alpha = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_point_wrap, uv, 2).a; + float4 uvsets = uv0 * w + uv1 * u + uv2 * v; + + float4 baseColor = material.baseColor; + if (material.texture_basecolormap_index >= 0 && (g_xFrame_Options & OPTION_BIT_DISABLE_ALBEDO_MAPS) == 0) + { + const float2 UV_baseColorMap = material.uvset_baseColorMap == 0 ? uvsets.xy : uvsets.zw; + float4 baseColorMap = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_linear_wrap, UV_baseColorMap, 2); + baseColorMap.rgb = DEGAMMA(baseColorMap.rgb); + baseColor *= baseColorMap; + } + + float transmission = material.transmission; + if (material.texture_transmissionmap_index >= 0) + { + const float2 UV_transmissionMap = material.uvset_transmissionMap == 0 ? uvsets.xy : uvsets.zw; + float transmissionMap = bindless_textures[material.texture_transmissionmap_index].SampleLevel(sampler_linear_wrap, UV_transmissionMap, 2).r; + transmission *= transmissionMap; + } + + shadow *= lerp(1, baseColor.rgb * transmission, baseColor.a); [branch] - if (alpha - material.alphaTest > 0) + if (!any(shadow)) { q.CommitNonOpaqueTriangleHit(); } } - bool hit = q.CommittedStatus() == COMMITTED_TRIANGLE_HIT; + shadow = q.CommittedStatus() == COMMITTED_TRIANGLE_HIT ? 0 : shadow; #else - bool hit = TraceRay_Any(newRay, dist, groupIndex); + shadow = TraceRay_Any(newRay, dist, groupIndex) ? 0 : shadow; #endif // RTAPI - if (!hit) + if (any(shadow)) { - result += max(0, current_energy * NdotL * (surface.albedo * lighting.direct.diffuse + lighting.direct.specular)); + result += max(0, shadow * (surface.albedo * lighting.direct.diffuse + lighting.direct.specular)); } } } diff --git a/WickedEngine/shaders/renderlightmapPS.hlsl b/WickedEngine/shaders/renderlightmapPS.hlsl index 1702d0dda..40624b8bb 100644 --- a/WickedEngine/shaders/renderlightmapPS.hlsl +++ b/WickedEngine/shaders/renderlightmapPS.hlsl @@ -146,7 +146,7 @@ float4 main(Input input) : SV_TARGET if (NdotL > 0 && dist > 0) { - lighting.direct.diffuse = max(0.0f, lighting.direct.diffuse); + float3 shadow = NdotL * ray.energy; float3 sampling_offset = float3(rand(seed, uv), rand(seed, uv), rand(seed, uv)) * 2 - 1; @@ -162,17 +162,83 @@ float4 main(Input input) : SV_TARGET apiray.Origin = newRay.origin; apiray.Direction = newRay.direction; RayQuery< - RAY_FLAG_FORCE_OPAQUE | - RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES | - RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH + RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES > q; - q.TraceRayInline(scene_acceleration_structure, 0, 0xFF, apiray); - q.Proceed(); - bool hit = q.CommittedStatus() == COMMITTED_TRIANGLE_HIT; + q.TraceRayInline( + scene_acceleration_structure, // RaytracingAccelerationStructure AccelerationStructure + 0, // uint RayFlags + 0xFF, // uint InstanceInclusionMask + apiray // RayDesc Ray + ); + while (q.Proceed()) + { + ShaderMesh mesh = bindless_buffers[q.CandidateInstanceID()].Load(0); + ShaderMeshSubset subset = bindless_subsets[mesh.subsetbuffer][q.CandidateGeometryIndex()]; + ShaderMaterial material = bindless_buffers[subset.material].Load(0); + [branch] + if (!material.IsCastingShadow()) + { + continue; + } + uint startIndex = q.CandidatePrimitiveIndex() * 3 + subset.indexOffset; + uint i0 = bindless_ib[mesh.ib][startIndex + 0]; + uint i1 = bindless_ib[mesh.ib][startIndex + 1]; + uint i2 = bindless_ib[mesh.ib][startIndex + 2]; + float4 uv0 = 0, uv1 = 0, uv2 = 0; + [branch] + if (mesh.vb_uv0 >= 0) + { + uv0.xy = unpack_half2(bindless_buffers[mesh.vb_uv0].Load(i0 * 4)); + uv1.xy = unpack_half2(bindless_buffers[mesh.vb_uv0].Load(i1 * 4)); + uv2.xy = unpack_half2(bindless_buffers[mesh.vb_uv0].Load(i2 * 4)); + } + [branch] + if (mesh.vb_uv1 >= 0) + { + uv0.zw = unpack_half2(bindless_buffers[mesh.vb_uv1].Load(i0 * 4)); + uv1.zw = unpack_half2(bindless_buffers[mesh.vb_uv1].Load(i1 * 4)); + uv2.zw = unpack_half2(bindless_buffers[mesh.vb_uv1].Load(i2 * 4)); + } + + float2 barycentrics = q.CandidateTriangleBarycentrics(); + float u = barycentrics.x; + float v = barycentrics.y; + float w = 1 - u - v; + float4 uvsets = uv0 * w + uv1 * u + uv2 * v; + + float4 baseColor = material.baseColor; + if (material.texture_basecolormap_index >= 0 && (g_xFrame_Options & OPTION_BIT_DISABLE_ALBEDO_MAPS) == 0) + { + const float2 UV_baseColorMap = material.uvset_baseColorMap == 0 ? uvsets.xy : uvsets.zw; + float4 baseColorMap = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_linear_wrap, UV_baseColorMap, 2); + baseColorMap.rgb = DEGAMMA(baseColorMap.rgb); + baseColor *= baseColorMap; + } + + float transmission = material.transmission; + if (material.texture_transmissionmap_index >= 0) + { + const float2 UV_transmissionMap = material.uvset_transmissionMap == 0 ? uvsets.xy : uvsets.zw; + float transmissionMap = bindless_textures[material.texture_transmissionmap_index].SampleLevel(sampler_linear_wrap, UV_transmissionMap, 2).r; + transmission *= transmissionMap; + } + + shadow *= lerp(1, baseColor.rgb * transmission, baseColor.a); + + [branch] + if (!any(shadow)) + { + q.CommitNonOpaqueTriangleHit(); + } + } + shadow = q.CommittedStatus() == COMMITTED_TRIANGLE_HIT ? 0 : shadow; #else - bool hit = TraceRay_Any(newRay, dist); + shadow = TraceRay_Any(newRay, dist) ? 0 : shadow; #endif // RTAPI - result += max(0, ray.energy * (hit ? 0 : NdotL) * lighting.direct.diffuse / PI); + if (any(shadow)) + { + result += max(0, shadow * lighting.direct.diffuse / PI); + } } } @@ -273,8 +339,9 @@ float4 main(Input input) : SV_TARGET if (material.texture_basecolormap_index >= 0 && (g_xFrame_Options & OPTION_BIT_DISABLE_ALBEDO_MAPS) == 0) { const float2 UV_baseColorMap = material.uvset_baseColorMap == 0 ? uvsets.xy : uvsets.zw; - baseColor = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_linear_wrap, UV_baseColorMap, 2); - baseColor.rgb *= DEGAMMA(baseColor.rgb); + float4 baseColorMap = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_linear_wrap, UV_baseColorMap, 2); + baseColorMap.rgb *= DEGAMMA(baseColorMap.rgb); + baseColor *= baseColorMap; } [branch] @@ -351,19 +418,15 @@ float4 main(Input input) : SV_TARGET uvsets = frac(uvsets); // emulate wrap - float4 baseColor; + float4 baseColor = material.baseColor * color; [branch] if (material.uvset_baseColorMap >= 0 && (g_xFrame_Options & OPTION_BIT_DISABLE_ALBEDO_MAPS) == 0) { const float2 UV_baseColorMap = material.uvset_baseColorMap == 0 ? uvsets.xy : uvsets.zw; - baseColor = materialTextureAtlas.SampleLevel(sampler_linear_clamp, UV_baseColorMap * material.baseColorAtlasMulAdd.xy + material.baseColorAtlasMulAdd.zw, 0); - baseColor.rgb = DEGAMMA(baseColor.rgb); + float4 baseColorMap = materialTextureAtlas.SampleLevel(sampler_linear_clamp, UV_baseColorMap * material.baseColorAtlasMulAdd.xy + material.baseColorAtlasMulAdd.zw, 0); + baseColorMap.rgb = DEGAMMA(baseColorMap.rgb); + baseColor *= baseColorMap; } - else - { - baseColor = 1; - } - baseColor *= color; float4 surfaceMap = 1; [branch] diff --git a/WickedEngine/shaders/rtaoLIB.hlsl b/WickedEngine/shaders/rtaoLIB.hlsl index 7ad5ce704..bdb13edb4 100644 --- a/WickedEngine/shaders/rtaoLIB.hlsl +++ b/WickedEngine/shaders/rtaoLIB.hlsl @@ -112,7 +112,7 @@ void RTAO_AnyHit(inout RayPayload payload, in BuiltInTriangleIntersectionAttribu float v = attr.barycentrics.y; float w = 1 - u - v; float2 uv = uv0 * w + uv1 * u + uv2 * v; - float alpha = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_point_wrap, uv, 2).a; + float alpha = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_linear_wrap, uv, 2).a; [branch] if (alpha - material.alphaTest > 0) diff --git a/WickedEngine/shaders/rtreflectionLIB.hlsl b/WickedEngine/shaders/rtreflectionLIB.hlsl index a5b5915f5..eec0aa5b0 100644 --- a/WickedEngine/shaders/rtreflectionLIB.hlsl +++ b/WickedEngine/shaders/rtreflectionLIB.hlsl @@ -160,8 +160,9 @@ void RTReflection_ClosestHit(inout RayPayload payload, in BuiltInTriangleInterse if (material.texture_basecolormap_index >= 0 && (g_xFrame_Options & OPTION_BIT_DISABLE_ALBEDO_MAPS) == 0) { const float2 UV_baseColorMap = material.uvset_baseColorMap == 0 ? uvsets.xy : uvsets.zw; - baseColor = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_linear_wrap, UV_baseColorMap, 2); - baseColor.rgb *= DEGAMMA(baseColor.rgb); + float4 baseColorMap = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_linear_wrap, UV_baseColorMap, 2); + baseColorMap.rgb *= DEGAMMA(baseColorMap.rgb); + baseColor *= baseColorMap; } [branch] @@ -315,7 +316,7 @@ void RTReflection_AnyHit(inout RayPayload payload, in BuiltInTriangleIntersectio float v = attr.barycentrics.y; float w = 1 - u - v; float2 uv = uv0 * w + uv1 * u + uv2 * v; - float alpha = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_point_wrap, uv, 2).a; + float alpha = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_linear_wrap, uv, 2).a; [branch] if (alpha - material.alphaTest < 0) diff --git a/WickedEngine/shaders/rtshadowLIB.hlsl b/WickedEngine/shaders/rtshadowLIB.hlsl index 2d9e41998..0ad9dc571 100644 --- a/WickedEngine/shaders/rtshadowLIB.hlsl +++ b/WickedEngine/shaders/rtshadowLIB.hlsl @@ -274,7 +274,7 @@ void RTShadow_AnyHit(inout RayPayload payload, in BuiltInTriangleIntersectionAtt float v = attr.barycentrics.y; float w = 1 - u - v; float2 uv = uv0 * w + uv1 * u + uv2 * v; - float alpha = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_point_wrap, uv, 2).a; + float alpha = bindless_textures[material.texture_basecolormap_index].SampleLevel(sampler_linear_wrap, uv, 2).a; [branch] if (alpha - material.alphaTest > 0) diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 42ba86d52..16224c032 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates, breaking compatibility changes const int minor = 55; // minor bug fixes, alterations, refactors, updates - const int revision = 7; + const int revision = 8; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);