Skip to content

Commit

Permalink
hair particle velocity buffer fix at fadeout range
Browse files Browse the repository at this point in the history
  • Loading branch information
turanszkij committed Nov 8, 2023
1 parent e7aef9e commit dce2696
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ You can specify command line arguments (without any prefix) to switch between re
### Other software using Wicked Engine
- <a href="https://www.game-guru.com/max">Game Guru MAX</a>: Easy to use game creator software
- <a href="https://www.youtube.com/watch?v=0SxXmnSQ6Q4">Flytrap</a>: Demoscene production by qop
- <a href="https://youtu.be/mbmNU5QVM8A?si=9sDMS1LrMsz03f5r">doddering</a>: Demoscene production by qop
- Your project: add your project to this readme and open a pull request

<br/>
Expand Down
11 changes: 7 additions & 4 deletions WickedEngine/shaders/hairparticle_simulateCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ RWBuffer<float4> vertexBuffer_POS : register(u1);
RWBuffer<float4> vertexBuffer_UVS : register(u2);
RWBuffer<uint> culledIndexBuffer : register(u3);
RWStructuredBuffer<IndirectDrawArgsIndexedInstanced> indirectBuffer : register(u4);
RWStructuredBuffer<float3> vertexBuffer_POS_RT : register(u5);

[numthreads(THREADCOUNT_SIMULATEHAIR, 1, 1)]
void main(uint3 DTid : SV_DispatchThreadID, uint3 Gid : SV_GroupID, uint groupIndex : SV_GroupIndex)
Expand Down Expand Up @@ -268,13 +269,15 @@ void main(uint3 DTid : SV_DispatchThreadID, uint3 Gid : SV_GroupID, uint groupIn
const float3 wind = sample_wind(rootposition, segmentID + patchPos.y);

float3 position = rootposition + patchPos + wind;
if (distance_culled)
{
position = 0;
}

vertexBuffer_POS[v0 + vertexID] = float4(position, asfloat(pack_unitvector(normalize(normal + wind))));
vertexBuffer_UVS[v0 + vertexID] = uv.xyxy; // a second uv set could be used here

if (distance_culled)
{
position = 0; // We can only zero out for raytracing geometry to keep correct prevpos swapping motion vectors!
}
vertexBuffer_POS_RT[v0 + vertexID] = position;
}

// Frustum culling:
Expand Down
24 changes: 15 additions & 9 deletions WickedEngine/wiHairParticle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,16 @@ namespace wi
vb_uvs.size = sizeof(MeshComponent::Vertex_UVS) * 4 * particleCount;
ib_culled.size = GetFormatStride(ib_format) * 6 * particleCount;
indirect_view.size = sizeof(IndirectDrawArgsIndexedInstanced);
vb_pos_raytracing.size = sizeof(float3) * 4 * particleCount;

bd.size =
AlignTo(AlignTo(indirect_view.size, alignment), sizeof(IndirectDrawArgsIndexedInstanced)) + // additional structured buffer alignment
AlignTo(AlignTo(simulation_view.size, alignment), sizeof(PatchSimulationData)) + // additional structured buffer alignment
AlignTo(vb_pos[0].size, alignment) +
AlignTo(vb_pos[1].size, alignment) +
AlignTo(vb_uvs.size, alignment) +
AlignTo(ib_culled.size, alignment)
AlignTo(ib_culled.size, alignment) +
AlignTo(AlignTo(vb_pos_raytracing.size, alignment), sizeof(float3))
;
device->CreateBuffer(&bd, nullptr, &generalBuffer);
device->SetName(&generalBuffer, "HairParticleSystem::generalBuffer");
Expand Down Expand Up @@ -172,6 +174,14 @@ namespace wi
ib_culled.descriptor_uav = device->GetDescriptorIndex(&generalBuffer, SubresourceType::UAV, ib_culled.subresource_uav);
buffer_offset += ib_culled.size;

constexpr uint32_t vb_pos_raytracing_stride = sizeof(float3);
buffer_offset = AlignTo(buffer_offset, alignment);
buffer_offset = AlignTo(buffer_offset, sizeof(float3));
vb_pos_raytracing.offset = buffer_offset;
vb_pos_raytracing.subresource_uav = device->CreateSubresource(&generalBuffer, SubresourceType::UAV, vb_pos_raytracing.offset, vb_pos_raytracing.size, nullptr, &vb_pos_raytracing_stride);
vb_pos_raytracing.descriptor_uav = device->GetDescriptorIndex(&generalBuffer, SubresourceType::UAV, vb_pos_raytracing.subresource_uav);
buffer_offset += vb_pos_raytracing.size;

primitiveBuffer = wi::renderer::GetIndexBufferForQuads(particleCount);
}

Expand Down Expand Up @@ -232,10 +242,10 @@ namespace wi
geometry.triangles.index_format = GetIndexBufferFormat(primitiveBuffer.desc.format);
geometry.triangles.index_count = GetParticleCount() * 6;
geometry.triangles.index_offset = 0;
geometry.triangles.vertex_count = (uint32_t)(vb_pos[0].size / sizeof(MeshComponent::Vertex_POS));
geometry.triangles.vertex_count = (uint32_t)(vb_pos_raytracing.size / sizeof(float3));
geometry.triangles.vertex_format = Format::R32G32B32_FLOAT;
geometry.triangles.vertex_stride = sizeof(MeshComponent::Vertex_POS);
geometry.triangles.vertex_byte_offset = vb_pos[0].offset;
geometry.triangles.vertex_stride = sizeof(float3);
geometry.triangles.vertex_byte_offset = vb_pos_raytracing.offset;

bool success = device->CreateRaytracingAccelerationStructure(&desc, &BLAS);
assert(success);
Expand Down Expand Up @@ -272,11 +282,6 @@ namespace wi
}

std::swap(vb_pos[0], vb_pos[1]);

if (BLAS.IsValid() && !BLAS.desc.bottom_level.geometries.empty())
{
BLAS.desc.bottom_level.geometries.back().triangles.vertex_byte_offset = vb_pos[0].offset;
}
}
void HairParticleSystem::UpdateGPU(
const UpdateGPUItem* items,
Expand Down Expand Up @@ -371,6 +376,7 @@ namespace wi
device->BindUAV(&hair.generalBuffer, 2, cmd, hair.vb_uvs.subresource_uav);
device->BindUAV(&hair.generalBuffer, 3, cmd, hair.ib_culled.subresource_uav);
device->BindUAV(&hair.generalBuffer, 4, cmd, hair.indirect_view.subresource_uav);
device->BindUAV(&hair.generalBuffer, 5, cmd, hair.vb_pos_raytracing.subresource_uav);

if (hair.indexBuffer.IsValid())
{
Expand Down
1 change: 1 addition & 0 deletions WickedEngine/wiHairParticle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace wi
wi::graphics::GPUBuffer generalBuffer;
wi::scene::MeshComponent::BufferView simulation_view;
wi::scene::MeshComponent::BufferView vb_pos[2];
wi::scene::MeshComponent::BufferView vb_pos_raytracing;
wi::scene::MeshComponent::BufferView vb_uvs;
wi::scene::MeshComponent::BufferView ib_culled;
wi::scene::MeshComponent::BufferView indirect_view;
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 = 339;
const int revision = 340;

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

Expand Down

0 comments on commit dce2696

Please sign in to comment.