From 6a3c14540d1d75bc7f83d1bde2c2bdb62b67eed1 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Thu, 17 Dec 2015 10:38:13 +0100 Subject: [PATCH] fixes --- include/Engine/Rendering/Renderer.h | 2 + resources/Schema/Entities/Test.xml | 13 ++++-- resources/Shaders/ForwardPlus.frag.glsl | 13 +++--- resources/Shaders/GridFrustum.comp.glsl | 58 ++++++++++++------------- resources/Shaders/cullLights.comp.glsl | 15 ++----- src/Engine/Rendering/Renderer.cpp | 13 +----- 6 files changed, 51 insertions(+), 63 deletions(-) diff --git a/include/Engine/Rendering/Renderer.h b/include/Engine/Rendering/Renderer.h index 63b41b32..fad41c5f 100644 --- a/include/Engine/Rendering/Renderer.h +++ b/include/Engine/Rendering/Renderer.h @@ -67,6 +67,7 @@ private: glm::vec3 Normal; float d; }; + struct Frustum { Plane Planes[4]; }; @@ -90,6 +91,7 @@ private: float Amount; glm::vec2 Padding; }; + LightGrid m_LightGrid[80*45]; int m_LightOffset = 0; diff --git a/resources/Schema/Entities/Test.xml b/resources/Schema/Entities/Test.xml index 78494ce1..f8be48a7 100644 --- a/resources/Schema/Entities/Test.xml +++ b/resources/Schema/Entities/Test.xml @@ -5,11 +5,18 @@ - - Models/DummyScene.obj - + + + + + + + Models/Core/UnitPlane.obj + + + diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index e3a3f05c..685a8d22 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -7,6 +7,7 @@ uniform vec4 Color; uniform sampler2D texture0; +#define TILE_SIZE 16 struct PointLight { vec4 Position; @@ -48,7 +49,7 @@ in VertexData{ out vec4 fragmentColor; -vec4 scene_ambient = vec4(0.6,0.6,0.6,1); +vec4 scene_ambient = vec4(0.0,0.0,0.0,1); struct LightResult { vec4 Diffuse; @@ -98,10 +99,10 @@ void main() LightResult totalLighting; totalLighting.Diffuse = scene_ambient; + int currentTile = int(floor(gl_FragCoord.x/TILE_SIZE) + (floor(gl_FragCoord.y/TILE_SIZE) * 80)); - - int start = int(LightGrids.Data[int(tilePos.x + tilePos.y*80)].Start); - int amount = int(LightGrids.Data[int(tilePos.x + tilePos.y*80)].Amount); + int start = int(LightGrids.Data[currentTile].Start); + int amount = int(LightGrids.Data[currentTile].Amount); //for(int i = 0; i < 3; i++) for(int i = start; i < start + amount; i++) { @@ -114,11 +115,11 @@ void main() } fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse + totalLighting.Specular) * texel * Color; - + fragmentColor += vec4(LightGrids.Data[currentTile].Amount/3.0, 0, 0, 1); //fragmentColor = texel * Input.DiffuseColor * Color; if(int(gl_FragCoord.x)%16 == 0 || int(gl_FragCoord.y)%16 == 0 ) { - fragmentColor = vec4(0.5, 0, 0, 0); + //fragmentColor = vec4(0.5, 0, 0, 0); } else { //fragmentColor += vec4(LightGrids.Data[int(tilePos.x + tilePos.y*80)].Amount/3.0, 0, 0, 1); diff --git a/resources/Shaders/GridFrustum.comp.glsl b/resources/Shaders/GridFrustum.comp.glsl index 523852d5..9e0f5a8e 100644 --- a/resources/Shaders/GridFrustum.comp.glsl +++ b/resources/Shaders/GridFrustum.comp.glsl @@ -16,7 +16,7 @@ struct Frustum { layout (std430, binding = 0) buffer FrustumBuffer { - Frustum Data[3600]; + Frustum Data[]; } Frustums; vec4 ConvertToView(vec4 ScreenCoords) @@ -43,36 +43,32 @@ Plane ComputePlane( vec3 p0, vec3 p1, vec3 p2 ) layout (local_size_x = 16, local_size_y = 16, local_size_z = 1) in; void main () { - if(gl_GlobalInvocationID.x * TILE_SIZE < ScreenDimensions.x && gl_GlobalInvocationID.y * TILE_SIZE < ScreenDimensions.y) { - //Top-Left = 0 | Top-Right = 1 - //Bottom-Left = 2 | Bottom-Right = 3 - vec4 ScreenCoords[4]; - ScreenCoords[0] = vec4(gl_GlobalInvocationID.x * TILE_SIZE, (gl_GlobalInvocationID.y + 1 ) * TILE_SIZE, -1.0, 1.0); // Z-axis might need to be 1 - ScreenCoords[1] = vec4((gl_GlobalInvocationID.x + 1) * TILE_SIZE, (gl_GlobalInvocationID.y + 1) * TILE_SIZE, -1.0, 1.0); - ScreenCoords[2] = vec4(gl_GlobalInvocationID.x * TILE_SIZE, (gl_GlobalInvocationID.y) * TILE_SIZE, -1.0, 1.0); - ScreenCoords[3] = vec4((gl_GlobalInvocationID.x + 1) * TILE_SIZE, (gl_GlobalInvocationID.y) * TILE_SIZE, -1.0, 1.0); + //Top-Left = 0 | Top-Right = 1 + //Bottom-Left = 2 | Bottom-Right = 3 + vec4 ScreenCoords[4]; + ScreenCoords[0] = vec4(gl_GlobalInvocationID.x * TILE_SIZE, (gl_GlobalInvocationID.y + 1 ) * TILE_SIZE, -1.0, 1.0); + ScreenCoords[1] = vec4((gl_GlobalInvocationID.x + 1) * TILE_SIZE, (gl_GlobalInvocationID.y + 1) * TILE_SIZE, -1.0, 1.0); + ScreenCoords[2] = vec4(gl_GlobalInvocationID.x * TILE_SIZE, (gl_GlobalInvocationID.y) * TILE_SIZE, -1.0, 1.0); + ScreenCoords[3] = vec4((gl_GlobalInvocationID.x + 1) * TILE_SIZE, (gl_GlobalInvocationID.y) * TILE_SIZE, -1.0, 1.0); - vec3 ViewVectors[4]; - for(int i = 0; i < 4; i++) { - ViewVectors[i] = vec3(ConvertToView(ScreenCoords[i])); - } - - vec3 EyePos = vec3(0,0,0); - - Frustum f; - f.Planes[0] = ComputePlane(EyePos, ViewVectors[2], ViewVectors[0]); - f.Planes[1] = ComputePlane(EyePos, ViewVectors[1], ViewVectors[3]); - f.Planes[2] = ComputePlane(EyePos, ViewVectors[0], ViewVectors[1]); - f.Planes[3] = ComputePlane(EyePos, ViewVectors[3], ViewVectors[2]); - - - - - if ( gl_GlobalInvocationID.x < ScreenDimensions.x / TILE_SIZE && gl_GlobalInvocationID.y < ScreenDimensions.y / TILE_SIZE ) { // innanför skärmen? - Frustums.Data[gl_GlobalInvocationID.x + gl_GlobalInvocationID.y*80] = f; - - } - - + vec3 ViewVectors[4]; + for(int i = 0; i < 4; i++) { + ViewVectors[i] = vec3(ConvertToView(ScreenCoords[i])); } + + vec3 EyePos = vec3(0,0,0); + + Frustum f; + f.Planes[0] = ComputePlane(EyePos, ViewVectors[2], ViewVectors[0]); + f.Planes[1] = ComputePlane(EyePos, ViewVectors[1], ViewVectors[3]); + f.Planes[2] = ComputePlane(EyePos, ViewVectors[0], ViewVectors[1]); + f.Planes[3] = ComputePlane(EyePos, ViewVectors[3], ViewVectors[2]); + + + + + if ( gl_GlobalInvocationID.x < ScreenDimensions.x / TILE_SIZE && gl_GlobalInvocationID.y < ScreenDimensions.y / TILE_SIZE ) { // inside the screen + Frustums.Data[gl_GlobalInvocationID.x + gl_GlobalInvocationID.y*80] = f; + + } } \ No newline at end of file diff --git a/resources/Shaders/cullLights.comp.glsl b/resources/Shaders/cullLights.comp.glsl index 012d72a7..ecb0e8e6 100644 --- a/resources/Shaders/cullLights.comp.glsl +++ b/resources/Shaders/cullLights.comp.glsl @@ -25,7 +25,7 @@ struct Frustum { layout (std430, binding = 0) buffer FrustumBuffer { - Frustum Data[3600]; + Frustum Data[]; } Frustums; struct PointLight { @@ -111,11 +111,9 @@ void main () if(gl_LocalInvocationIndex == 0) { GroupLightCount = 0; - GroupFrustum = Frustums.Data[GroupIndex]; } - memoryBarrierShared(); barrier(); for(int i = int(gl_LocalInvocationIndex); i < PointLights.List.length(); i += TILE_SIZE*TILE_SIZE) @@ -124,7 +122,7 @@ void main () //if pointlight //Pos i view antagligen - if(SphereInsideFrustrum(vec3(V * light.Position), light.Radius, GroupFrustum)) + if(SphereInsideFrustrum( vec3(V * light.Position), light.Radius, GroupFrustum)) { //TODO: Fix transparent and opaque list, and depth test. AppendLight( i ); @@ -137,20 +135,15 @@ void main () } - memoryBarrierShared(); barrier(); if(gl_LocalInvocationIndex == 0) { GroupLightIndexStartOffset = atomicAdd(LightOffset[0], GroupLightCount); - LightGrid g; - g.Start = GroupLightIndexStartOffset; - g.Amount = GroupLightCount; - g.Padding = vec2(1111, 1111); - LightGrids.Data[GroupIndex] = g; + LightGrids.Data[GroupIndex].Start = GroupLightIndexStartOffset; + LightGrids.Data[GroupIndex].Amount = GroupLightCount; } - memoryBarrierShared(); barrier(); for (uint i = gl_LocalInvocationIndex; i < GroupLightCount; i += TILE_SIZE * TILE_SIZE ) diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index b838bdbc..24e82cfd 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -265,7 +265,7 @@ void Renderer::CalculateFrustum() void Renderer::TEMPCreateLights() { for (int i = 0; i < NUM_LIGHTS; i++) { - m_PointLights[i].Position = glm::vec4(5.f * (i-1), -1.5f, 0.f, 1.f); + m_PointLights[i].Position = glm::vec4(5.f * (i-1), 1.f, 0.f, 1.f); m_PointLights[i].Color = glm::vec4(1.f, 0.5f, 0.f + i*0.1f, 1.f); m_PointLights[i].Radius = 2.f; } @@ -280,17 +280,6 @@ void Renderer::CullLights() glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(m_LightOffset), &m_LightOffset, GL_DYNAMIC_COPY); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightGridSSBO); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(m_LightGrid), &m_LightGrid, GL_DYNAMIC_COPY); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); - - glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightOffsetSSBO); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(m_LightOffset), &m_LightOffset, GL_DYNAMIC_COPY); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); - - glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightIndexSSBO); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(m_LightIndex), &m_LightIndex, GL_DYNAMIC_COPY); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); m_LightCullProgram->Bind(); glUniformMatrix4fv(glGetUniformLocation(m_CalculateFrustumProgram->GetHandle(), "V"), 1, false, glm::value_ptr(m_Camera->ViewMatrix()));