Skip to content

Commit

Permalink
general improvements (#1049)
Browse files Browse the repository at this point in the history
- Entity is now 64-bit uint
- terrain chunk hashing improvement
- structure memory alignment improvements
- light shafts improvements
  • Loading branch information
turanszkij authored Jan 31, 2025
1 parent ed5b4a0 commit 12f6abd
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 301 deletions.
2 changes: 1 addition & 1 deletion Editor/GeneralWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ void GeneralWindow::Create(EditorComponent* _editor)
}

theme.shadow_highlight = !focusModeCheckBox.GetCheck();
theme.shadow_highlight_spread = 0.6f;
theme.shadow_highlight_spread = 0.4f;
theme.shadow_highlight_color = theme_color_focus;
theme.shadow_highlight_color.x *= 1.4f;
theme.shadow_highlight_color.y *= 1.4f;
Expand Down
15 changes: 7 additions & 8 deletions WickedEngine/shaders/ShaderInterop_Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,15 +653,18 @@ struct alignas(16) ShaderTransform

struct alignas(16) ShaderMeshInstance
{
uint uid;
uint64_t uid;
uint flags; // high 8 bits: user stencilRef
uint layerMask;
uint meshletOffset; // offset in the global meshlet buffer for first subset (for LOD0)

uint geometryOffset; // offset of all geometries for currently active LOD
uint geometryCount; // number of all geometries in currently active LOD
uint meshletOffset; // offset in the global meshlet buffer for first subset (for LOD0)
uint geometryOffset; // offset of all geometries for currently active LOD
uint geometryCount; // number of all geometries in currently active LOD
uint baseGeometryOffset; // offset of all geometries of the instance (if no LODs, then it is equal to geometryOffset)

uint2 rimHighlight; // packed half4
uint baseGeometryCount; // number of all geometries of the instance (if no LODs, then it is equal to geometryCount)
float fadeDistance;

uint2 color; // packed half4
uint2 emissive; // packed half4
Expand All @@ -671,10 +674,6 @@ struct alignas(16) ShaderMeshInstance
int lightmap;
uint alphaTest_size; // packed half2

uint2 rimHighlight; // packed half4
float fadeDistance;
float padding;

float3 center;
float radius;

Expand Down
5 changes: 2 additions & 3 deletions WickedEngine/shaders/ShaderInterop_SurfelGI.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ struct Surfel
// This per-surfel structure will store all additional persistent data per surfel that isn't needed at GI lookup
struct SurfelData
{
uint64_t uid;
uint2 primitiveID;
uint bary;
uint uid;

uint bary;
uint raydata; // 24bit rayOffset, 8bit rayCount
uint properties; // 8bit life frames, 8bit recycle frames, 1bit backface normal
float max_inconsistency;
int padding1;

inline uint GetRayOffset() { return raydata & 0xFFFFFF; }
inline uint GetRayCount() { return (raydata >> 24u) & 0xFF; }
Expand Down
32 changes: 13 additions & 19 deletions WickedEngine/shaders/lightShaftsCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,34 @@

PUSHCONSTANT(postprocess, PostProcess);

Texture2D<float4> input : register(t0);
Texture2D<half4> input : register(t0);

RWTexture2D<float4> output : register(u0);
RWTexture2D<half4> output : register(u0);

static const uint NUM_SAMPLES = 32;
static const uint UNROLL_GRANULARITY = 8;
static const uint NUM_SAMPLES = 64;

[numthreads(POSTPROCESS_BLOCKSIZE, POSTPROCESS_BLOCKSIZE, 1)]
void main(uint3 DTid : SV_DispatchThreadID)
{
float2 uv = (DTid.xy + 0.5f) * postprocess.resolution_rcp;
float2 uv = (DTid.xy + 0.5) * postprocess.resolution_rcp;

float3 color = input.SampleLevel(sampler_linear_clamp, uv, 0).rgb;
half3 color = input.SampleLevel(sampler_linear_clamp, uv, 0).rgb;

float2 lightPos = postprocess.params1.xy;
float2 deltaTexCoord = uv - lightPos;
deltaTexCoord *= postprocess.params0.x / NUM_SAMPLES;
float illuminationDecay = 1.0f;
half illuminationDecay = 1.0;

[loop] // loop big part (balance register pressure)
for (uint i = 0; i < NUM_SAMPLES / UNROLL_GRANULARITY; i++)
for (uint i = 0; i < NUM_SAMPLES; i++)
{
[unroll] // unroll small parts (balance register pressure)
for (uint j = 0; j < UNROLL_GRANULARITY; ++j)
{
uv.xy -= deltaTexCoord;
float3 sam = input.SampleLevel(sampler_linear_clamp, uv.xy, 0).rgb;
sam *= illuminationDecay * postprocess.params0.y;
color.rgb += sam;
illuminationDecay *= postprocess.params0.z;
}
uv.xy -= deltaTexCoord;
half3 sam = input.SampleLevel(sampler_linear_clamp, uv.xy, 0).rgb;
sam *= illuminationDecay * postprocess.params0.y;
color.rgb += sam;
illuminationDecay *= postprocess.params0.z;
}

color *= postprocess.params0.w;

output[DTid.xy] = float4(color, 1);
output[DTid.xy] = half4(color, 1);
}
18 changes: 12 additions & 6 deletions WickedEngine/wiECS.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ namespace wi::ecs
// The Entity is a global unique persistent identifier within the entity-component system
// It can be stored and used for the duration of the application
// The entity can be a different value on a different run of the application, if it was serialized
// It must be only serialized with the SerializeEntity() function. It will ensure that entities still match with their components correctly after serialization
using Entity = uint32_t;
inline constexpr Entity INVALID_ENTITY = 0;
// It must be only serialized with the SerializeEntity() function if persistence is needed across different program runs,
// this will ensure that entities still match with their components correctly after serialization
using Entity = uint64_t;
inline static constexpr Entity INVALID_ENTITY = 0;
// Runtime can create a new entity with this
inline Entity CreateEntity()
{
Expand Down Expand Up @@ -142,9 +143,14 @@ namespace wi::ecs
// reservedCount : how much components can be held initially before growing the container
ComponentManager(size_t reservedCount = 0)
{
components.reserve(reservedCount);
entities.reserve(reservedCount);
lookup.reserve(reservedCount);
Reserve(reservedCount);
}

inline void Reserve(size_t count)
{
components.reserve(count);
entities.reserve(count);
lookup.reserve(count);
}

// Clear the whole container
Expand Down
6 changes: 3 additions & 3 deletions WickedEngine/wiHairParticle.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ namespace wi
wi::graphics::CommandList cmd
);

mutable bool gpu_initialized = false;
void InitializeGPUDataIfNeeded(wi::graphics::CommandList cmd);

void Draw(
Expand All @@ -69,6 +68,8 @@ namespace wi
wi::graphics::CommandList cmd
) const;

wi::ecs::Entity meshID = wi::ecs::INVALID_ENTITY;

enum FLAGS
{
EMPTY = 0,
Expand All @@ -78,8 +79,6 @@ namespace wi
};
uint32_t _flags = EMPTY;

wi::ecs::Entity meshID = wi::ecs::INVALID_ENTITY;

uint32_t strandCount = 0;
uint32_t segmentCount = 1;
uint32_t randomSeed = 1;
Expand All @@ -106,6 +105,7 @@ namespace wi
mutable bool regenerate_frame = true;
wi::graphics::Format position_format = wi::graphics::Format::R16G16B16A16_UNORM;
mutable bool must_rebuild_blas = true;
mutable bool gpu_initialized = false;

void Serialize(wi::Archive& archive, wi::ecs::EntitySerializer& seri);

Expand Down
17 changes: 14 additions & 3 deletions WickedEngine/wiRenderPath3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace wi
rtShadow = {};
rtSun[0] = {};
rtSun[1] = {};
rtSun[2] = {};
rtSun_resolved = {};
rtGUIBlurredBackground[0] = {};
rtGUIBlurredBackground[1] = {};
Expand Down Expand Up @@ -1914,6 +1915,8 @@ namespace wi

device->EventBegin("Light Shafts", cmd);

const Texture* texture_fullres = nullptr;

// Render sun stencil cutout:
{
if (getMSAASampleCount() > 1)
Expand All @@ -1931,6 +1934,7 @@ namespace wi
RenderPassImage::Resolve(&rtSun_resolved),
};
device->RenderPassBegin(rp, arraysize(rp), cmd);
texture_fullres = &rtSun_resolved;
}
else
{
Expand All @@ -1946,6 +1950,7 @@ namespace wi
RenderPassImage::RenderTarget(&rtSun[0], RenderPassImage::LoadOp::CLEAR),
};
device->RenderPassBegin(rp, arraysize(rp), cmd);
texture_fullres = &rtSun[0];
}

Viewport vp;
Expand Down Expand Up @@ -1977,10 +1982,13 @@ namespace wi
1.0f, 1.0f, 0.1f, 1.0f,
camera->GetProjection(), camera->GetView(), XMMatrixIdentity());
{
// Downsample to low res first:
wi::renderer::Postprocess_Downsample4x(*texture_fullres, rtSun[2], cmd);

XMFLOAT2 sun;
XMStoreFloat2(&sun, sunPos);
wi::renderer::Postprocess_LightShafts(
getMSAASampleCount() > 1 ? rtSun_resolved : rtSun[0],
rtSun[2],
rtSun[1],
cmd,
sun,
Expand Down Expand Up @@ -2919,10 +2927,12 @@ namespace wi

desc.bind_flags = BindFlag::SHADER_RESOURCE | BindFlag::UNORDERED_ACCESS;
desc.sample_count = 1;
desc.width = internalResolution.x / 2;
desc.height = internalResolution.y / 2;
desc.width = internalResolution.x / 4;
desc.height = internalResolution.y / 4;
device->CreateTexture(&desc, nullptr, &rtSun[1]);
device->SetName(&rtSun[1], "rtSun[1]");
device->CreateTexture(&desc, nullptr, &rtSun[2]);
device->SetName(&rtSun[2], "rtSun[2]");

if (getMSAASampleCount() > 1)
{
Expand All @@ -2937,6 +2947,7 @@ namespace wi
{
rtSun[0] = {};
rtSun[1] = {};
rtSun[2] = {};
rtSun_resolved = {};
}
}
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiRenderPath3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ namespace wi
wi::graphics::Texture rtBloom_tmp; // temporary for bloom downsampling
wi::graphics::Texture rtAO; // full res AO
wi::graphics::Texture rtShadow; // raytraced shadows mask
wi::graphics::Texture rtSun[2]; // 0: sun render target used for lightshafts (can be MSAA), 1: radial blurred lightshafts
wi::graphics::Texture rtSun[3]; // 0: sun render target used for lightshafts (can be MSAA), 1: radial blurred lightshafts
wi::graphics::Texture rtSun_resolved; // sun render target, but the resolved version if MSAA is enabled
wi::graphics::Texture rtGUIBlurredBackground[3]; // downsampled, gaussian blurred scene for GUI
wi::graphics::Texture rtShadingRate; // UINT8 shading rate per tile
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4417,7 +4417,7 @@ namespace wi::scene
const float dist = std::sqrt(distsq);
const float dist_to_sphere = dist - radius;
object.lod = uint32_t(dist_to_sphere * object.lod_distance_multiplier);
object.lod = std::min(object.lod, mesh.GetLODCount() - 1);
object.lod = std::min(object.lod, uint16_t(mesh.GetLODCount() - 1));
}
}

Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiScene_BindLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4796,7 +4796,7 @@ int MeshComponent_BindLua::SetMeshSubsetMaterialID(lua_State* L)
if (argc >= 2)
{
size_t subsetindex = (uint32_t)wi::lua::SGetLongLong(L, 1);
Entity entity = (uint32_t)wi::lua::SGetLongLong(L, 2);
Entity entity = (Entity)wi::lua::SGetLongLong(L, 2);

const uint32_t lod_count = component->GetLODCount();
for (uint32_t lod = 0; lod < lod_count; ++lod)
Expand Down
Loading

0 comments on commit 12f6abd

Please sign in to comment.