Skip to content

Commit

Permalink
reduced buffer resources for light culling
Browse files Browse the repository at this point in the history
  • Loading branch information
turanszkij committed Jan 16, 2024
1 parent 0aa9eea commit 40af47f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 43 deletions.
11 changes: 6 additions & 5 deletions WickedEngine/shaders/ShaderInterop_Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,8 @@ struct ShaderCamera
uint4 scissor; // scissor in physical coordinates (left,top,right,bottom) range: [0, internal_resolution]
float4 scissor_uv; // scissor in screen UV coordinates (left,top,right,bottom) range: [0, 1]

uint3 entity_culling_tilecount;
uint2 entity_culling_tilecount;
uint entity_culling_tile_bucket_count_flat; // tilecount.x * tilecount.y * SHADER_ENTITY_TILE_BUCKET_COUNT (the total number of uint buckets for the whole screen)
uint sample_count;

uint2 visibility_tilecount;
Expand All @@ -1037,9 +1038,9 @@ struct ShaderCamera
int texture_velocity_index;
int texture_normal_index;
int texture_roughness_index;
int buffer_entitytiles_opaque_index;
int buffer_entitytiles_index;

int buffer_entitytiles_transparent_index;
int padding;
int texture_reflection_index;
int texture_reflection_depth_index;
int texture_refraction_index;
Expand Down Expand Up @@ -1093,6 +1094,7 @@ struct ShaderCamera
scissor = {};
scissor_uv = {};
entity_culling_tilecount = {};
entity_culling_tile_bucket_count_flat = 0;
sample_count = {};
visibility_tilecount = {};
visibility_tilecount_flat = {};
Expand All @@ -1105,8 +1107,7 @@ struct ShaderCamera
texture_velocity_index = -1;
texture_normal_index = -1;
texture_roughness_index = -1;
buffer_entitytiles_opaque_index = -1;
buffer_entitytiles_transparent_index = -1;
buffer_entitytiles_index = -1;
texture_reflection_index = -1;
texture_refraction_index = -1;
texture_waterriples_index = -1;
Expand Down
4 changes: 2 additions & 2 deletions WickedEngine/shaders/globals.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ inline ShaderMaterial load_material(uint materialIndex)
uint load_entitytile(uint tileIndex)
{
#ifdef TRANSPARENT
return bindless_structured_uint[GetCamera().buffer_entitytiles_transparent_index][tileIndex];
return bindless_structured_uint[GetCamera().buffer_entitytiles_index][GetCamera().entity_culling_tile_bucket_count_flat + tileIndex];
#else
return bindless_structured_uint[GetCamera().buffer_entitytiles_opaque_index][tileIndex];
return bindless_structured_uint[GetCamera().buffer_entitytiles_index][tileIndex];
#endif // TRANSPARENT
}
inline ShaderEntity load_entity(uint entityIndex)
Expand Down
7 changes: 3 additions & 4 deletions WickedEngine/shaders/lightCullingCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

StructuredBuffer<Frustum> in_Frustums : register(t0);

RWStructuredBuffer<uint> EntityTiles_Transparent : register(u0);
RWStructuredBuffer<uint> EntityTiles_Opaque : register(u1);
RWStructuredBuffer<uint> entityTiles : register(u0);

// Group shared variables.
groupshared uint uMinDepth;
Expand Down Expand Up @@ -328,8 +327,8 @@ void main(uint3 Gid : SV_GroupID, uint3 DTid : SV_DispatchThreadID, uint3 GTid :
// Each thread will export one bucket from LDS to global memory:
for (i = groupIndex; i < SHADER_ENTITY_TILE_BUCKET_COUNT; i += TILED_CULLING_THREADSIZE * TILED_CULLING_THREADSIZE)
{
EntityTiles_Opaque[tileBucketsAddress + i] = tile_opaque[i];
EntityTiles_Transparent[tileBucketsAddress + i] = tile_transparent[i];
entityTiles[tileBucketsAddress + i] = tile_opaque[i];
entityTiles[GetCamera().entity_culling_tile_bucket_count_flat + tileBucketsAddress + i] = tile_transparent[i];
}

#ifdef DEBUG_TILEDLIGHTCULLING
Expand Down
10 changes: 4 additions & 6 deletions WickedEngine/wiRenderPath3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,7 @@ namespace wi
camera->texture_velocity_index = device->GetDescriptorIndex(&rtVelocity, SubresourceType::SRV);
camera->texture_normal_index = device->GetDescriptorIndex(&visibilityResources.texture_normals, SubresourceType::SRV);
camera->texture_roughness_index = device->GetDescriptorIndex(&visibilityResources.texture_roughness, SubresourceType::SRV);
camera->buffer_entitytiles_opaque_index = device->GetDescriptorIndex(&tiledLightResources.entityTiles_Opaque, SubresourceType::SRV);
camera->buffer_entitytiles_transparent_index = device->GetDescriptorIndex(&tiledLightResources.entityTiles_Transparent, SubresourceType::SRV);
camera->buffer_entitytiles_index = device->GetDescriptorIndex(&tiledLightResources.entityTiles, SubresourceType::SRV);
camera->texture_reflection_index = device->GetDescriptorIndex(&rtReflection, SubresourceType::SRV);
camera->texture_reflection_depth_index = device->GetDescriptorIndex(&depthBuffer_Reflection, SubresourceType::SRV);
camera->texture_refraction_index = device->GetDescriptorIndex(&rtSceneCopy, SubresourceType::SRV);
Expand Down Expand Up @@ -670,8 +669,7 @@ namespace wi
camera_reflection.texture_velocity_index = -1;
camera_reflection.texture_normal_index = -1;
camera_reflection.texture_roughness_index = -1;
camera_reflection.buffer_entitytiles_opaque_index = device->GetDescriptorIndex(&tiledLightResources_planarReflection.entityTiles_Opaque, SubresourceType::SRV);
camera_reflection.buffer_entitytiles_transparent_index = device->GetDescriptorIndex(&tiledLightResources_planarReflection.entityTiles_Transparent, SubresourceType::SRV);
camera_reflection.buffer_entitytiles_index = device->GetDescriptorIndex(&tiledLightResources_planarReflection.entityTiles, SubresourceType::SRV);
camera_reflection.texture_reflection_index = -1;
camera_reflection.texture_reflection_depth_index = -1;
camera_reflection.texture_refraction_index = -1;
Expand Down Expand Up @@ -990,7 +988,7 @@ namespace wi
{
wi::renderer::Postprocess_ScreenSpaceShadow(
screenspaceshadowResources,
tiledLightResources.entityTiles_Opaque,
tiledLightResources.entityTiles,
rtLinearDepth,
rtShadow,
cmd,
Expand All @@ -1004,7 +1002,7 @@ namespace wi
wi::renderer::Postprocess_RTShadow(
rtshadowResources,
*scene,
tiledLightResources.entityTiles_Opaque,
tiledLightResources.entityTiles,
rtLinearDepth,
rtShadow,
cmd
Expand Down
25 changes: 9 additions & 16 deletions WickedEngine/wiRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8834,15 +8834,12 @@ void CreateTiledLightResources(TiledLightResources& res, XMUINT2 resolution)
{
GPUBufferDesc bd;
bd.stride = sizeof(uint);
bd.size = res.tileCount.x * res.tileCount.y * bd.stride * SHADER_ENTITY_TILE_BUCKET_COUNT;
bd.size = res.tileCount.x * res.tileCount.y * bd.stride * SHADER_ENTITY_TILE_BUCKET_COUNT * 2; // *2: opaque and transparent arrays
bd.usage = Usage::DEFAULT;
bd.bind_flags = BindFlag::UNORDERED_ACCESS | BindFlag::SHADER_RESOURCE;
bd.misc_flags = ResourceMiscFlag::BUFFER_STRUCTURED;
device->CreateBuffer(&bd, nullptr, &res.entityTiles_Opaque);
device->CreateBuffer(&bd, nullptr, &res.entityTiles_Transparent);

device->SetName(&res.entityTiles_Opaque, "entityTiles_Opaque");
device->SetName(&res.entityTiles_Transparent, "entityTiles_Transparent");
device->CreateBuffer(&bd, nullptr, &res.entityTiles);
device->SetName(&res.entityTiles, "entityTiles");
}
}
void ComputeTiledLightCulling(
Expand All @@ -8858,8 +8855,7 @@ void ComputeTiledLightCulling(
{
GPUBarrier barriers[] = {
GPUBarrier::Buffer(&res.tileFrustums, ResourceState::SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS),
GPUBarrier::Buffer(&res.entityTiles_Transparent, ResourceState::SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS),
GPUBarrier::Buffer(&res.entityTiles_Opaque, ResourceState::SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS),
GPUBarrier::Buffer(&res.entityTiles, ResourceState::SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS),
};
device->Barrier(barriers, arraysize(barriers), cmd);
}
Expand All @@ -8872,13 +8868,11 @@ void ComputeTiledLightCulling(
{
device->EventBegin("Tiled Entity Clear Only", cmd);
device->ClearUAV(&res.tileFrustums, 0, cmd);
device->ClearUAV(&res.entityTiles_Transparent, 0, cmd);
device->ClearUAV(&res.entityTiles_Opaque, 0, cmd);
device->ClearUAV(&res.entityTiles, 0, cmd);
{
GPUBarrier barriers[] = {
GPUBarrier::Buffer(&res.tileFrustums, ResourceState::UNORDERED_ACCESS, ResourceState::SHADER_RESOURCE),
GPUBarrier::Buffer(&res.entityTiles_Transparent, ResourceState::UNORDERED_ACCESS, ResourceState::SHADER_RESOURCE),
GPUBarrier::Buffer(&res.entityTiles_Opaque, ResourceState::UNORDERED_ACCESS, ResourceState::SHADER_RESOURCE),
GPUBarrier::Buffer(&res.entityTiles, ResourceState::UNORDERED_ACCESS, ResourceState::SHADER_RESOURCE),
};
device->Barrier(barriers, arraysize(barriers), cmd);
}
Expand Down Expand Up @@ -8933,8 +8927,7 @@ void ComputeTiledLightCulling(
}

const GPUResource* uavs[] = {
&res.entityTiles_Transparent,
&res.entityTiles_Opaque,
&res.entityTiles,
};
device->BindUAVs(uavs, 0, arraysize(uavs), cmd);

Expand Down Expand Up @@ -9822,6 +9815,7 @@ void BindCameraCB(
shadercam.scissor_uv.w = (shadercam.scissor.w - 0.5f) * shadercam.internal_resolution_rcp.y;

shadercam.entity_culling_tilecount = GetEntityCullingTileCount(shadercam.internal_resolution);
shadercam.entity_culling_tile_bucket_count_flat = shadercam.entity_culling_tilecount.x * shadercam.entity_culling_tilecount.y * SHADER_ENTITY_TILE_BUCKET_COUNT;
shadercam.sample_count = camera.sample_count;
shadercam.visibility_tilecount = GetVisibilityTileCount(shadercam.internal_resolution);
shadercam.visibility_tilecount_flat = shadercam.visibility_tilecount.x * shadercam.visibility_tilecount.y;
Expand All @@ -9832,8 +9826,7 @@ void BindCameraCB(
shadercam.texture_velocity_index = camera.texture_velocity_index;
shadercam.texture_normal_index = camera.texture_normal_index;
shadercam.texture_roughness_index = camera.texture_roughness_index;
shadercam.buffer_entitytiles_opaque_index = camera.buffer_entitytiles_opaque_index;
shadercam.buffer_entitytiles_transparent_index = camera.buffer_entitytiles_transparent_index;
shadercam.buffer_entitytiles_index = camera.buffer_entitytiles_index;
shadercam.texture_reflection_index = camera.texture_reflection_index;
shadercam.texture_reflection_depth_index = camera.texture_reflection_depth_index;
shadercam.texture_refraction_index = camera.texture_refraction_index;
Expand Down
12 changes: 5 additions & 7 deletions WickedEngine/wiRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ namespace wi::renderer
{
return (userStencilRef << 4) | static_cast<uint8_t>(engineStencilRef);
}
constexpr XMUINT3 GetEntityCullingTileCount(XMUINT2 internalResolution)
constexpr XMUINT2 GetEntityCullingTileCount(XMUINT2 internalResolution)
{
return XMUINT3(
return XMUINT2(
(internalResolution.x + TILED_CULLING_BLOCKSIZE - 1) / TILED_CULLING_BLOCKSIZE,
(internalResolution.y + TILED_CULLING_BLOCKSIZE - 1) / TILED_CULLING_BLOCKSIZE,
1
(internalResolution.y + TILED_CULLING_BLOCKSIZE - 1) / TILED_CULLING_BLOCKSIZE
);
}
constexpr XMUINT2 GetVisibilityTileCount(XMUINT2 internalResolution)
Expand Down Expand Up @@ -296,10 +295,9 @@ namespace wi::renderer

struct TiledLightResources
{
XMUINT3 tileCount = {};
XMUINT2 tileCount = {};
wi::graphics::GPUBuffer tileFrustums; // entity culling frustums
wi::graphics::GPUBuffer entityTiles_Opaque; // culled entity indices (for opaque pass)
wi::graphics::GPUBuffer entityTiles_Transparent; // culled entity indices (for transparent pass)
wi::graphics::GPUBuffer entityTiles; // culled entity indices
};
void CreateTiledLightResources(TiledLightResources& res, XMUINT2 resolution);
// Compute light grid tiles
Expand Down
3 changes: 1 addition & 2 deletions WickedEngine/wiScene_Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,7 @@ namespace wi::scene
int texture_rtshadow_index = -1;
int texture_rtdiffuse_index = -1;
int texture_surfelgi_index = -1;
int buffer_entitytiles_opaque_index = -1;
int buffer_entitytiles_transparent_index = -1;
int buffer_entitytiles_index = -1;
int texture_vxgi_diffuse_index = -1;
int texture_vxgi_specular_index = -1;

Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace wi::version
// minor features, major updates, breaking compatibility changes
const int minor = 71;
// minor bug fixes, alterations, refactors, updates
const int revision = 358;
const int revision = 359;

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

Expand Down

0 comments on commit 40af47f

Please sign in to comment.