Skip to content

Commit 67f76d0

Browse files
authored
decal and probe matrix access improvement (#1652)
1 parent 00aad02 commit 67f76d0

7 files changed

Lines changed: 34 additions & 52 deletions

File tree

WickedEngine/shaders/cullingShaderHF.hlsli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ void AABBfromMinMax(inout AABB aabb, float3 _min, float3 _max)
194194
aabb.c = (_min + _max) * 0.5f;
195195
aabb.e = abs(_max - aabb.c);
196196
}
197-
void AABBtransform(inout AABB aabb, float4x4 mat)
197+
template<typename T>
198+
void AABBtransform(inout AABB aabb, T mat)
198199
{
199200
float3 _min = aabb.getMin();
200201
float3 _max = aabb.getMax();

WickedEngine/shaders/lightCullingCS.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void main(uint3 Gid : SV_GroupID, uint3 DTid : SV_DispatchThreadID, uint3 GTid :
250250

251251
// frustum AABB in world space transformed into the space of the probe/decal OBB:
252252
AABB b = GroupAABB_WS;
253-
AABBtransform(b, load_entitymatrix(entity.GetMatrixIndex()));
253+
AABBtransform(b, (float3x4)load_entitymatrix(i)); // note: straight entity-matrix mapping ok
254254

255255
if (IntersectAABB(a, b))
256256
{
@@ -280,7 +280,7 @@ void main(uint3 Gid : SV_GroupID, uint3 DTid : SV_DispatchThreadID, uint3 GTid :
280280

281281
// frustum AABB in world space transformed into the space of the probe/decal OBB:
282282
AABB b = GroupAABB_WS;
283-
AABBtransform(b, load_entitymatrix(entity.GetMatrixIndex()));
283+
AABBtransform(b, (float3x4)load_entitymatrix(i)); // note: straight entity-matrix mapping ok
284284

285285
if (IntersectAABB(a, b))
286286
{

WickedEngine/shaders/lightingHF.hlsli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ inline half3 EnvironmentReflection_Global(in Surface surface)
607607
// clipSpacePos: world space pixel position transformed into OBB space by probeProjection matrix
608608
// MIP: mip level to sample
609609
// return: color of the environment map (rgb), blend factor of the environment map (a)
610-
inline half4 EnvironmentReflection_Local(in TextureCube<half4> cubemap, in Surface surface, in ShaderEntity probe, in float4x4 probeProjection, in half3 clipSpacePos)
610+
inline half4 EnvironmentReflection_Local(in TextureCube<half4> cubemap, in Surface surface, in ShaderEntity probe, in float3x4 probeProjection, in half3 clipSpacePos)
611611
{
612612
if ((probe.layerMask & surface.layerMask) == 0)
613613
return 0; // early exit: layer mismatch

WickedEngine/shaders/shadingHF.hlsli

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,13 @@ inline void ForwardLighting(inout Surface surface, inout Lighting lighting)
5959
{
6060
// Retrieve global entity index from local bucket, then remove bit from local bucket:
6161
const uint bucket_bit_index = firstbitlow(bucket_bits);
62-
const uint entity_index = bucket_bit_index;
6362
bucket_bits ^= 1u << bucket_bit_index;
6463

64+
const uint entity_index = probes().first_item() + bucket_bit_index;
6565
ShaderEntity probe = load_entity(probes().first_item() + entity_index);
6666

67-
float4x4 probeProjection = load_entitymatrix(probe.GetMatrixIndex());
68-
const int probeTexture = asint(probeProjection[3][0]);
69-
probeProjection[3] = float4(0, 0, 0, 1);
70-
TextureCube<half4> cubemap = bindless_cubemaps_half4[descriptor_index(probeTexture)];
67+
float3x4 probeProjection = load_entitymatrix(entity_index); // note: straight entity-matrix mapping ok
68+
TextureCube<half4> cubemap = bindless_cubemaps_half4[descriptor_index(probe.GetTextureIndex())];
7169

7270
const half3 clipSpacePos = mul(probeProjection, float4(surface.P, 1)).xyz;
7371
const half3 uvw = box_to_uv(clipSpacePos.xyz);
@@ -180,22 +178,21 @@ inline void ForwardDecals(inout Surface surface, inout half4 surfaceMap, Sampler
180178
{
181179
// Retrieve global entity index from local bucket, then remove bit from local bucket:
182180
const uint bucket_bit_index = firstbitlow(bucket_bits);
183-
const uint entity_index = bucket_bit_index;
184181
bucket_bits ^= 1u << bucket_bit_index;
185182

186-
ShaderEntity decal = load_entity(decals().first_item() + entity_index);
183+
const uint entity_index = decals().first_item() + bucket_bit_index;
184+
ShaderEntity decal = load_entity(entity_index);
187185

188-
float4x4 decalProjection = load_entitymatrix(decal.GetMatrixIndex());
186+
float4x4 decalProjection = load_entitymatrix(entity_index); // note: straight entity-matrix mapping ok
189187
const int decalTexture = asint(decalProjection[3][0]);
190188
const int decalNormal = asint(decalProjection[3][1]);
191189
const int decalSurfacemap = asint(decalProjection[3][2]);
192190
const int decalDisplacementmap = asint(decalProjection[3][3]);
193-
decalProjection[3] = float4(0, 0, 0, 1);
194191

195192
// under here will be VGPR!
196193
if ((decal.layerMask & surface.layerMask) == 0)
197194
continue;
198-
const float3 clipSpacePos = mul(decalProjection, float4(surface.P, 1)).xyz;
195+
const float3 clipSpacePos = mul((float3x4)decalProjection, float4(surface.P, 1)).xyz;
199196
float3 uvw = box_to_uv(clipSpacePos.xyz);
200197
[branch]
201198
if (is_saturated(uvw))
@@ -306,10 +303,8 @@ inline void TiledLighting(inout Surface surface, inout Lighting lighting, uint f
306303

307304
ShaderEntity probe = load_entity(entity_index);
308305

309-
float4x4 probeProjection = load_entitymatrix(probe.GetMatrixIndex());
310-
const int probeTexture = asint(probeProjection[3][0]);
311-
probeProjection[3] = float4(0, 0, 0, 1);
312-
TextureCube<half4> cubemap = bindless_cubemaps_half4[descriptor_index(probeTexture)];
306+
float3x4 probeProjection = load_entitymatrix(entity_index); // note: straight entity-matrix mapping ok
307+
TextureCube<half4> cubemap = bindless_cubemaps_half4[descriptor_index(probe.GetTextureIndex())];
313308

314309
const half3 clipSpacePos = mul(probeProjection, float4(surface.P, 1)).xyz;
315310
const half3 uvw = box_to_uv(clipSpacePos.xyz);
@@ -644,22 +639,21 @@ inline void TiledDecals(inout Surface surface, uint flatTileIndex, inout half4 s
644639
{
645640
// Retrieve global entity index from local bucket, then remove bit from local bucket:
646641
const uint bucket_bit_index = firstbitlow(bucket_bits);
647-
const uint entity_index = bucket * 32 + bucket_bit_index;
648642
bucket_bits ^= 1u << bucket_bit_index;
649643

644+
const uint entity_index = bucket * 32 + bucket_bit_index;
650645
ShaderEntity decal = load_entity(entity_index);
651646

652-
float4x4 decalProjection = load_entitymatrix(decal.GetMatrixIndex());
647+
float4x4 decalProjection = load_entitymatrix(entity_index); // note: straight entity-matrix mapping ok
653648
const int decalTexture = asint(decalProjection[3][0]);
654649
const int decalNormal = asint(decalProjection[3][1]);
655650
const int decalSurfacemap = asint(decalProjection[3][2]);
656651
const int decalDisplacementmap = asint(decalProjection[3][3]);
657-
decalProjection[3] = float4(0, 0, 0, 1);
658652

659653
// under here will be VGPR!
660654
if ((decal.layerMask & surface.layerMask) == 0)
661655
continue;
662-
const float3 clipSpacePos = mul(decalProjection, float4(surface.P, 1)).xyz;
656+
const float3 clipSpacePos = mul((float3x4)decalProjection, float4(surface.P, 1)).xyz;
663657
float3 uvw = box_to_uv(clipSpacePos.xyz);
664658
[branch]
665659
if (is_saturated(uvw))

WickedEngine/shaders/surfaceHF.hlsli

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,22 +795,21 @@ struct Surface
795795
{
796796
// Retrieve global entity index from local bucket, then remove bit from local bucket:
797797
const uint bucket_bit_index = firstbitlow(bucket_bits);
798-
const uint entity_index = bucket * 32 + bucket_bit_index;
799798
bucket_bits ^= 1u << bucket_bit_index;
800799

800+
const uint entity_index = bucket * 32 + bucket_bit_index;
801801
ShaderEntity decal = load_entity(entity_index);
802802

803-
float4x4 decalProjection = load_entitymatrix(decal.GetMatrixIndex());
803+
float4x4 decalProjection = load_entitymatrix(entity_index); // note: straight entity-matrix mapping ok
804804
const int decalTexture = asint(decalProjection[3][0]);
805805
const int decalNormal = asint(decalProjection[3][1]);
806806
const int decalSurfacemap = asint(decalProjection[3][2]);
807807
const int decalDisplacementmap = asint(decalProjection[3][3]);
808-
decalProjection[3] = float4(0, 0, 0, 1);
809808

810809
// under here will be VGPR!
811810
if ((decal.layerMask & layerMask) == 0)
812811
continue;
813-
const float3 clipSpacePos = mul(decalProjection, float4(P, 1)).xyz;
812+
const float3 clipSpacePos = mul((float3x4)decalProjection, float4(P, 1)).xyz;
814813
float3 uvw = box_to_uv(clipSpacePos.xyz);
815814
[branch]
816815
if (is_saturated(uvw))

WickedEngine/wiRenderer.cpp

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4549,7 +4549,6 @@ void UpdatePerFrameData(
45494549
const XMMATRIX viewMatrix = vis.camera->GetView();
45504550

45514551
uint32_t entityCounter = 0;
4552-
uint32_t matrixCounter = 0;
45534552

45544553
// Write decals into entity array:
45554554
decalarray_offset = entityCounter;
@@ -4561,11 +4560,6 @@ void UpdatePerFrameData(
45614560
entityCounter--;
45624561
break;
45634562
}
4564-
if (matrixCounter >= MATRIXARRAY_COUNT)
4565-
{
4566-
matrixCounter--;
4567-
break;
4568-
}
45694563
ShaderEntity shaderentity = {};
45704564
XMMATRIX shadermatrix;
45714565

@@ -4596,7 +4590,7 @@ void UpdatePerFrameData(
45964590
shaderentity.SetAngleScale(decal.normal_strength);
45974591
shaderentity.SetLength(decal.displacement_strength);
45984592

4599-
shaderentity.SetIndices(matrixCounter, 0);
4593+
shaderentity.SetIndices(entityCounter, 0);
46004594
shadermatrix = XMMatrixInverse(nullptr, XMLoadFloat4x4(&decal.world));
46014595

46024596
int texture = -1;
@@ -4629,8 +4623,7 @@ void UpdatePerFrameData(
46294623
XMStoreFloat3(&cullsphere.center, XMVector3Transform(XMLoadFloat3(&shaderentity.position), viewMatrix));
46304624
cullsphere.radius = decal.range;
46314625

4632-
XMStoreFloat4x4(matrixArray + matrixCounter, shadermatrix);
4633-
matrixCounter++;
4626+
XMStoreFloat4x4(matrixArray + entityCounter, shadermatrix); // note: straight entity-matrix mapping ok
46344627

46354628
std::memcpy(entityArray + entityCounter, &shaderentity, sizeof(ShaderEntity));
46364629
std::memcpy(entityCullingArray + entityCounter, &cullsphere, sizeof(ShaderSphere));
@@ -4648,11 +4641,6 @@ void UpdatePerFrameData(
46484641
entityCounter--;
46494642
break;
46504643
}
4651-
if (matrixCounter >= MATRIXARRAY_COUNT)
4652-
{
4653-
matrixCounter--;
4654-
break;
4655-
}
46564644
ShaderEntity shaderentity = {};
46574645
XMMATRIX shadermatrix;
46584646

@@ -4673,33 +4661,29 @@ void UpdatePerFrameData(
46734661
shaderentity.position = probe.position;
46744662
shaderentity.SetRange(probe.range);
46754663

4676-
shaderentity.SetIndices(matrixCounter, 0);
4677-
shadermatrix = XMLoadFloat4x4(&probe.inverseMatrix);
4678-
4679-
int texture = -1;
4664+
int texture_index = -1;
46804665
if (probe.texture.IsValid())
46814666
{
4682-
texture = device->GetDescriptorIndex(&probe.texture, SubresourceType::SRV, probe.subresource);
4667+
texture_index = device->GetDescriptorIndex(&probe.texture, SubresourceType::SRV, probe.subresource);
46834668
}
4669+
texture_index = std::max(0, texture_index);
46844670

4685-
shadermatrix.r[0] = XMVectorSetW(shadermatrix.r[0], *(float*)&texture);
4686-
shadermatrix.r[1] = XMVectorSetW(shadermatrix.r[1], 0);
4687-
shadermatrix.r[2] = XMVectorSetW(shadermatrix.r[2], 0);
4688-
shadermatrix.r[3] = XMVectorSetW(shadermatrix.r[3], 0);
4671+
shaderentity.SetIndices(entityCounter, (uint)texture_index);
4672+
shadermatrix = XMLoadFloat4x4(&probe.inverseMatrix);
46894673

46904674
ShaderSphere cullsphere = {};
46914675
XMStoreFloat3(&cullsphere.center, XMVector3Transform(XMLoadFloat3(&shaderentity.position), viewMatrix));
46924676
cullsphere.radius = probe.range;
46934677

4694-
XMStoreFloat4x4(matrixArray + matrixCounter, shadermatrix);
4695-
matrixCounter++;
4678+
XMStoreFloat4x4(matrixArray + entityCounter, shadermatrix); // note: straight entity-matrix mapping ok
46964679

46974680
std::memcpy(entityArray + entityCounter, &shaderentity, sizeof(ShaderEntity));
46984681
std::memcpy(entityCullingArray + entityCounter, &cullsphere, sizeof(ShaderSphere));
46994682
entityCounter++;
47004683
envprobearray_count++;
47014684
}
47024685

4686+
uint32_t matrixCounter = entityCounter; // so far entities and matrices had 1-1 mapping, this is not true below this part:
47034687
const XMFLOAT2 atlas_dim_rcp = XMFLOAT2(1.0f / float(shadowMapAtlas.desc.width), 1.0f / float(shadowMapAtlas.desc.height));
47044688

47054689
// Write directional lights into entity array:
@@ -4759,6 +4743,7 @@ void UpdatePerFrameData(
47594743
for (size_t cascade = 0; cascade < cascade_count; ++cascade)
47604744
{
47614745
XMStoreFloat4x4(&matrixArray[matrixCounter++], shcams[cascade].view_projection);
4746+
matrixCounter = std::min(matrixCounter, MATRIXARRAY_COUNT - 1);
47624747
}
47634748
}
47644749

@@ -4851,6 +4836,7 @@ void UpdatePerFrameData(
48514836
SHCAM shcam;
48524837
CreateSpotLightShadowCam(light, shcam);
48534838
XMStoreFloat4x4(&matrixArray[matrixCounter++], shcam.view_projection);
4839+
matrixCounter = std::min(matrixCounter, MATRIXARRAY_COUNT - 1);
48544840
}
48554841

48564842
if (light.IsCastingShadow())
@@ -5015,6 +5001,7 @@ void UpdatePerFrameData(
50155001
SHCAM shcam;
50165002
CreateSpotLightShadowCam(light, shcam);
50175003
XMStoreFloat4x4(&matrixArray[matrixCounter++], shcam.view_projection);
5004+
matrixCounter = std::min(matrixCounter, MATRIXARRAY_COUNT - 1);
50185005
}
50195006

50205007
if (light.IsCastingShadow())
@@ -5093,6 +5080,7 @@ void UpdatePerFrameData(
50935080
XMStoreFloat3(&cullsphere.center, XMVector3Transform(XMLoadFloat3(&shaderentity.position), viewMatrix));
50945081
cullsphere.radius = FLT_MAX;
50955082
matrixArray[matrixCounter++] = collider.plane.projection;
5083+
matrixCounter = std::min(matrixCounter, MATRIXARRAY_COUNT - 1);
50965084
break;
50975085
default:
50985086
assert(0);

WickedEngine/wiVersion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace wi::version
99
// minor features, major updates, breaking compatibility changes
1010
const int minor = 72;
1111
// minor bug fixes, alterations, refactors, updates
12-
const int revision = 89;
12+
const int revision = 90;
1313

1414
const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);
1515

0 commit comments

Comments
 (0)