Skip to content

Commit

Permalink
Improve reflection filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
feiy committed Jul 13, 2023
1 parent 1c2ae2e commit caada79
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 5 deletions.
2 changes: 2 additions & 0 deletions ThreeEngine/ThreeEngine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
<ClCompile Include="renderers\routines\Picking.cpp" />
<ClCompile Include="renderers\routines\RasterizeAtlas.cpp" />
<ClCompile Include="renderers\routines\ReflectionCopy.cpp" />
<ClCompile Include="renderers\routines\ReflectionMipmaps.cpp" />
<ClCompile Include="renderers\routines\SceneToVolume.cpp" />
<ClCompile Include="renderers\routines\SimpleFogRayMarching.cpp" />
<ClCompile Include="renderers\routines\SimpleFogRayMarchingEnv.cpp" />
Expand Down Expand Up @@ -375,6 +376,7 @@
<ClInclude Include="renderers\routines\Picking.h" />
<ClInclude Include="renderers\routines\RasterizeAtlas.h" />
<ClInclude Include="renderers\routines\ReflectionCopy.h" />
<ClInclude Include="renderers\routines\ReflectionMipmaps.h" />
<ClInclude Include="renderers\routines\SceneToVolume.h" />
<ClInclude Include="renderers\routines\SimpleFogRayMarching.h" />
<ClInclude Include="renderers\routines\SimpleFogRayMarchingEnv.h" />
Expand Down
6 changes: 6 additions & 0 deletions ThreeEngine/ThreeEngine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@
<ClCompile Include="renderers\routines\ReflectionCopy.cpp">
<Filter>Source Files\renderers\routines</Filter>
</ClCompile>
<ClCompile Include="renderers\routines\ReflectionMipmaps.cpp">
<Filter>Source Files\renderers\routines</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="scenes\Scene.h">
Expand Down Expand Up @@ -938,6 +941,9 @@
<ClInclude Include="renderers\routines\ReflectionCopy.h">
<Filter>Header Files\renderers\routines</Filter>
</ClInclude>
<ClInclude Include="renderers\routines\ReflectionMipmaps.h">
<Filter>Header Files\renderers\routines</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
17 changes: 14 additions & 3 deletions ThreeEngine/renderers/GLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3211,9 +3211,20 @@ void GLRenderer::render(Scene& scene, Camera& camera, GLRenderTarget& target)
ReflectionCopier = std::unique_ptr<ReflectionCopy>(new ReflectionCopy);
}
ReflectionCopier->copy(reflector->m_tex_mipmapped->tex_id, reflector->m_target.m_tex_video->tex_id, target.m_width, target.m_height, &reflector->m_camera);
glBindTexture(GL_TEXTURE_2D, reflector->m_tex_mipmapped->tex_id);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);

if (ReflectionMipmapper == nullptr)
{
ReflectionMipmapper = std::unique_ptr<ReflectionMipmaps>(new ReflectionMipmaps);
}

int width = target.m_width;
int height = target.m_height;
for (int i = 0; i < 7; i++)
{
if (width > 1) width /= 2;
if (height > 1) height /= 2;
ReflectionMipmapper->downsample(reflector->m_tex_mipmapped->tex_id, i, width, height);
}

}

Expand Down
2 changes: 2 additions & 0 deletions ThreeEngine/renderers/GLRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "renderers/routines/DepthDownsample.h"
#include "renderers/routines/ReflectionCopy.h"
#include "renderers/routines/ReflectionMipmaps.h"

#include "renderers/routines/SceneToVolume.h"

Expand Down Expand Up @@ -198,6 +199,7 @@ class GLRenderer

std::unique_ptr<DepthDownsample> DepthDownsampler;
std::unique_ptr<ReflectionCopy> ReflectionCopier;
std::unique_ptr<ReflectionMipmaps> ReflectionMipmapper;

std::unique_ptr<SceneToVolume> SceneVolumeConvert;
void scene_to_volume_primitive(const SceneToVolume::RenderParams& params);
Expand Down
67 changes: 67 additions & 0 deletions ThreeEngine/renderers/routines/ReflectionMipmaps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <string>
#include <GL/glew.h>
#include "ReflectionMipmaps.h"


static std::string g_comp =
R"(#version 430
layout(location = 0) uniform sampler2D tex_src;
layout (location = 1) uniform float lod;
layout(binding = 0, rgba16f) uniform image2D tex_dst;
layout(local_size_x = 8, local_size_y = 8) in;
void main()
{
ivec2 size = imageSize(tex_dst);
ivec2 id = ivec3(gl_GlobalInvocationID).xy;
if (id.x>=size.x || id.y>=size.y) return;
vec2 pixel_size = vec2(1.0,1.0)/vec2(size);
vec2 uv = (vec2(id)+0.5)/vec2(size);
vec3 color = vec3(0.0);
color += 0.0625 * textureLod(tex_src, uv + vec2(-pixel_size.x, -pixel_size.y), lod).xyz;
color += 0.125 * textureLod(tex_src, uv + vec2(0.0, -pixel_size.y), lod).xyz;
color += 0.0625 * textureLod(tex_src, uv + vec2(pixel_size.x, -pixel_size.y), lod).xyz;
color += 0.125 * textureLod(tex_src, uv + vec2(-pixel_size.x, 0.0), lod).xyz;
color += 0.25 * textureLod(tex_src, uv, lod).xyz;
color += 0.125 * textureLod(tex_src, uv + vec2(pixel_size.x, 0.0), lod).xyz;
color += 0.0625 * textureLod(tex_src, uv + vec2(-pixel_size.x, pixel_size.y), lod).xyz;
color += 0.125 * textureLod(tex_src, uv + vec2(0.0, pixel_size.y), lod).xyz;
color += 0.0625 * textureLod(tex_src, uv + vec2(pixel_size.x, pixel_size.y), lod).xyz;
imageStore(tex_dst, id, vec4(color,1.0));
}
)";


ReflectionMipmaps::ReflectionMipmaps()
{
GLShader comp_shader(GL_COMPUTE_SHADER, g_comp.c_str());
m_prog = (std::unique_ptr<GLProgram>)(new GLProgram(comp_shader));
}

void ReflectionMipmaps::downsample(unsigned tex, int level, int width, int height)
{
glUseProgram(m_prog->m_id);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(0, 0);
glUniform1f(1, (float)level);

glBindImageTexture(0, tex, level+1, GL_TRUE, 0, GL_WRITE_ONLY, GL_RGBA16F);

glDispatchCompute((width + 7) / 8, (height + 7) / 8, 1);

glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);

}


19 changes: 19 additions & 0 deletions ThreeEngine/renderers/routines/ReflectionMipmaps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <memory>
#include "renderers/GLUtils.h"

class ReflectionMipmaps
{
public:
ReflectionMipmaps();

void downsample(unsigned tex, int level, int width, int height);

private:
std::unique_ptr<GLProgram> m_prog;

};



18 changes: 16 additions & 2 deletions webgpu/client/engine/renderers/routines/ReflectionMipmaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,25 @@ fn main(@builtin(global_invocation_id) id : vec3<u32>)
return;
}
let pixel_size = vec2(1.0, 1.0)/vec2f(size);
let uv = (vec2f(id.xy) + 0.5)/vec2f(size);
let color = textureSampleLevel(uTexSrc, uSampler, uv, 0);
var color = vec3(0.0);
color += 0.0625 * textureSampleLevel(uTexSrc, uSampler, uv + vec2(-pixel_size.x, -pixel_size.y), 0).xyz;
color += 0.125 * textureSampleLevel(uTexSrc, uSampler, uv + vec2(0.0, -pixel_size.y), 0).xyz;
color += 0.0625 * textureSampleLevel(uTexSrc, uSampler, uv + vec2(pixel_size.x, -pixel_size.y), 0).xyz;
color += 0.125 * textureSampleLevel(uTexSrc, uSampler, uv + vec2(-pixel_size.x, 0.0), 0).xyz;
color += 0.25 * textureSampleLevel(uTexSrc, uSampler, uv, 0).xyz;
color += 0.125 * textureSampleLevel(uTexSrc, uSampler, uv + vec2(pixel_size.x, 0.0), 0).xyz;
color += 0.0625 * textureSampleLevel(uTexSrc, uSampler, uv + vec2(-pixel_size.x, pixel_size.y), 0).xyz;
color += 0.125 * textureSampleLevel(uTexSrc, uSampler, uv + vec2(0.0, pixel_size.y), 0).xyz;
color += 0.0625 * textureSampleLevel(uTexSrc, uSampler, uv + vec2(pixel_size.x, pixel_size.y), 0).xyz;
let coord = vec2i(id.xy);
textureStore(uTexDst, coord, color);
textureStore(uTexDst, coord, vec4(color, 1.0));
}
`;

Expand Down

0 comments on commit caada79

Please sign in to comment.