From 14b5ae08fd2fd24d9235ffaf94a29f5a193ae2ee Mon Sep 17 00:00:00 2001 From: turanszkij Date: Tue, 15 Nov 2016 18:07:52 +0100 Subject: [PATCH] particle system GPU culling refactor --- WickedEngine/cullingShaderHF.hlsli | 15 +++++++++++++ WickedEngine/grassGS.hlsl | 3 ++- WickedEngine/grassHF_GS.hlsli | 34 ------------------------------ WickedEngine/pointspriteGS.hlsl | 12 +++++++++-- WickedEngine/qgrassGS.hlsl | 3 ++- 5 files changed, 29 insertions(+), 38 deletions(-) diff --git a/WickedEngine/cullingShaderHF.hlsli b/WickedEngine/cullingShaderHF.hlsli index 15ad7b5e6..8862acfed 100644 --- a/WickedEngine/cullingShaderHF.hlsli +++ b/WickedEngine/cullingShaderHF.hlsli @@ -154,4 +154,19 @@ bool ConeInsideFrustum(Cone cone, Frustum frustum, float zNear, float zFar) return result; } + + +static const float4 frustumCorners[4] = { + float4(-1, -1, 1, 1), + float4(1, -1, 1, 1), + float4(-1, 1, 1, 1), + float4(1, 1, 1, 1) +}; +static const Frustum frustum = { + ComputePlane(float3(0,0,0), frustumCorners[2].xyz, frustumCorners[0].xyz), + ComputePlane(float3(0,0,0), frustumCorners[1].xyz, frustumCorners[3].xyz), + ComputePlane(float3(0,0,0), frustumCorners[0].xyz, frustumCorners[1].xyz), + ComputePlane(float3(0,0,0), frustumCorners[3].xyz, frustumCorners[2].xyz) +}; + #endif // _CULLING_SHADER_HF_ diff --git a/WickedEngine/grassGS.hlsl b/WickedEngine/grassGS.hlsl index 7a57549df..2a79d33c1 100644 --- a/WickedEngine/grassGS.hlsl +++ b/WickedEngine/grassGS.hlsl @@ -9,7 +9,8 @@ void main( float grassLength = input[0].nor.w; float3 root = mul(float4(input[0].pos.xyz, 1), xWorld).xyz; - if (!IsVisible(root, grassLength)) + Sphere sphere = { mul(float4(root,1), g_xCamera_View).xyz, grassLength }; + if (!SphereInsideFrustum(sphere, frustum, 1, LOD2)) { return; } diff --git a/WickedEngine/grassHF_GS.hlsli b/WickedEngine/grassHF_GS.hlsli index ff9a247ce..c402d7fbc 100644 --- a/WickedEngine/grassHF_GS.hlsli +++ b/WickedEngine/grassHF_GS.hlsli @@ -41,40 +41,6 @@ struct QGS_OUT float4 pos2D : SCREENPOSITION; }; -inline bool IsVisible(in float3 root, in float length) -{ - // culling: - // View space eye position is always at the origin. - const float3 eyePos = float3(0, 0, 0); - - // Compute 4 points on the far clipping plane to use as the - // frustum vertices. - float4 corners[4]; - // Top left point - corners[0] = float4(-1, -1, 1, 1); - // Top right point - corners[1] = float4(1, -1, 1, 1); - // Bottom left point - corners[2] = float4(-1, 1, 1, 1); - // Bottom right point - corners[3] = float4(1, 1, 1, 1); - - // Now build the frustum planes from the view space points - Frustum frustum; - - // Left plane - frustum.planes[0] = ComputePlane(eyePos, corners[2].xyz, corners[0].xyz); - // Right plane - frustum.planes[1] = ComputePlane(eyePos, corners[1].xyz, corners[3].xyz); - // Top plane - frustum.planes[2] = ComputePlane(eyePos, corners[0].xyz, corners[1].xyz); - // Bottom plane - frustum.planes[3] = ComputePlane(eyePos, corners[3].xyz, corners[2].xyz); - - Sphere sphere = { mul(float4(root,1), g_xCamera_View).xyz, length }; - return SphereInsideFrustum(sphere, frustum, 1, LOD2); -} - static const float3 MOD[] = { float3(-0.010735935, 0.01647018, 0.0062425877), float3(-0.06533369, 0.3647007, -0.13746321), diff --git a/WickedEngine/pointspriteGS.hlsl b/WickedEngine/pointspriteGS.hlsl index 66a8d12b2..1239f18b8 100644 --- a/WickedEngine/pointspriteGS.hlsl +++ b/WickedEngine/pointspriteGS.hlsl @@ -1,4 +1,5 @@ #include "globals.hlsli" +#include "cullingShaderHF.hlsli" CBUFFER(EmittedParticleCB, CBSLOT_OTHER_EMITTEDPARTICLE) { @@ -25,8 +26,16 @@ struct VertextoPixel }; [maxvertexcount(4)] -void main(point GS_INPUT p[1], inout TriangleStream triStream) +void main(point GS_INPUT p[1], inout TriangleStream triStream, uint vid : SV_PrimitiveID) { + float quadLength = p[0].inSizOpMir.x*0.5f; + + Sphere sphere = { mul(float4(p[0].pos.xyz,1), g_xCamera_View).xyz, quadLength }; + if (!SphereInsideFrustum(sphere, frustum, 1, g_xFrame_MainCamera_ZFarP)) + { + return; + } + VertextoPixel p1 = (VertextoPixel)0; float2x2 rot = float2x2( cos(p[0].rot),-sin(p[0].rot), @@ -49,7 +58,6 @@ void main(point GS_INPUT p[1], inout TriangleStream triStream) p[0].vel = mul(p[0].vel, (float3x3)g_xCamera_View); - float quadLength = p[0].inSizOpMir.x*0.5f; //p1.col = p[0].color; p1.opaAddDarkSiz = float4(saturate(p[0].inSizOpMir.y),xAdd.x,xAdd.y,p[0].inSizOpMir.x); diff --git a/WickedEngine/qgrassGS.hlsl b/WickedEngine/qgrassGS.hlsl index 48551a49b..94a68fa49 100644 --- a/WickedEngine/qgrassGS.hlsl +++ b/WickedEngine/qgrassGS.hlsl @@ -9,7 +9,8 @@ void main( float grassLength = input[0].nor.w; float3 root = mul(float4(input[0].pos.xyz,1), xWorld).xyz; - if (!IsVisible(root, grassLength)) + Sphere sphere = { mul(float4(root,1), g_xCamera_View).xyz, grassLength }; + if (!SphereInsideFrustum(sphere, frustum, 1, LOD2)) { return; }