Try to add compute shaders

This commit is contained in:
2023-04-11 14:55:57 +01:00
parent 69a76dbf93
commit d79216555b
21 changed files with 457 additions and 246 deletions
+27 -5
View File
@@ -44,6 +44,7 @@ createLightMap cfig = case shadrendertype of
InstancingShads -> instanceLightMap cfig
NoShadows -> const . const . const renderLightingNoShadows
NoLighting -> const . const . const . const . const . const renderFlatLighting
ComputeShader -> renderComputeShadows
_ -> renderShadows shadrendertype
where
shadrendertype = cfig ^. graphics_shadow_rendering
@@ -103,6 +104,28 @@ renderLightingNoShadows positiontexture normaltexture lightPoints pdata = do
glDisable GL_CULL_FACE
glDisable GL_STENCIL_TEST
renderComputeShadows ::
GLsizei ->
Int ->
Int ->
TO ->
TO ->
[(Point3, Float, Point3)] ->
RenderData ->
IO ()
renderComputeShadows _ _ _ toPos tanormals lightPoints rd = do
withArray (lightsToArray lightPoints) $ \ptr ->
glNamedBufferSubData (rd ^. lightsUBO) 0 620 ptr
glBindTextureUnit 0 (toPos ^. unTO)
glBindTextureUnit 1 (tanormals ^. unTO)
glBindImageTexture 5 (rd ^. fboLighting . _2 . unTO) 0 GL_FALSE 0 GL_WRITE_ONLY GL_RGBA8
glUseProgram (rd ^. computeShadowShader)
glDispatchCompute 800 835 1
-- the following should be later, just before the texture is accessed
--glMemoryBarrier GL_SHADER_IMAGE_ACCESS_BARRIER_BIT
glMemoryBarrier GL_TEXTURE_FETCH_BARRIER_BIT
return ()
renderFlatLighting :: RenderData -> IO ()
renderFlatLighting pdata = do
glBindFramebuffer GL_FRAMEBUFFER (_unFBO (fst (_fboLighting pdata)))
@@ -145,12 +168,14 @@ renderShadows shadrendertype nWalls nSils nCaps positiontexture normaltexture li
ptr
-- for each of the lights:
-- 1. stencil out the shadows from this light's point of view
-- 2. calculate lighting based on each fragment's position
-- to consider: adding normals/a "material" for each fragment
-- 2. calculate lighting based on each fragment's position and normal
glBlendFunc GL_ZERO GL_ONE_MINUS_SRC_COLOR
glEnable GL_STENCIL_TEST
glStencilOpSeparate GL_FRONT GL_KEEP GL_KEEP GL_INCR_WRAP
glStencilOpSeparate GL_BACK GL_KEEP GL_KEEP GL_DECR_WRAP
-- bind world position texture-- these will be used by the lighting shader
glBindTextureUnit 0 (positiontexture ^. unTO)
glBindTextureUnit 1 (normaltexture ^. unTO)
flip VFSM.mapM_ (VFSM.fromList lightPoints) $ \(V3 x y z, rad, V3 r g b) -> do
glDepthFunc GL_LESS
-- setup stencil
@@ -200,10 +225,7 @@ renderShadows shadrendertype nWalls nSils nCaps positiontexture normaltexture li
_ -> return ()
--draw lightmap itself
glDepthFunc GL_ALWAYS
-- bind world position texture
glDisable GL_CULL_FACE
glBindTextureUnit 0 (positiontexture ^. unTO)
glBindTextureUnit 1 (normaltexture ^. unTO)
glColorMask GL_TRUE GL_TRUE GL_TRUE GL_TRUE
glStencilFunc GL_EQUAL 0 255
glUseProgram (ltextShad ^. shaderUINT) --Just (_shadProg ltextShad)