Skip to content

Commit b9480f8

Browse files
committed
Fix depth texture reads in sampler2D
1 parent 489279b commit b9480f8

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

demosys/deferred/renderer.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import moderngl as mgl
2-
32
from pyrr import matrix44
3+
44
from demosys.opengl import FBO
55
from demosys.opengl import Texture2D, DepthTexture
6+
from demosys.opengl import samplers
67
from demosys import resources
78
from demosys import geometry
89
from demosys import context
@@ -28,11 +29,15 @@ def position(self, pos):
2829
class DeferredRenderer:
2930

3031
def __init__(self, width, height, gbuffer=None, lightbuffer=None):
32+
self.ctx = context.ctx()
33+
3134
self.width = width
3235
self.height = height
3336
self.size = (width, height)
34-
print(self.size)
35-
self.ctx = context.ctx()
37+
self.depth_sampler = samplers.create(
38+
texture_compare_mode=False,
39+
min_filter=mgl.LINEAR, mag_filter=mgl.LINEAR
40+
)
3641

3742
# FBOs
3843
self.gbuffer = gbuffer
@@ -112,13 +117,16 @@ def render_lights(self, camera_matrix, projection):
112117
self.point_light_shader.uniform("m_light", m_light.astype('f4').tobytes())
113118
self.gbuffer.color_buffers[1].use(location=0)
114119
self.point_light_shader.uniform("g_normal", 0)
120+
self.depth_sampler.use(location=1)
115121
self.gbuffer.depth_buffer.use(location=1)
116122
self.point_light_shader.uniform("g_depth", 1)
117123
self.point_light_shader.uniform("screensize", (self.width, self.height))
118124
self.point_light_shader.uniform("proj_const", projection.projection_constants)
119125
self.point_light_shader.uniform("radius", light_size)
120126
self.unit_cube.draw(self.point_light_shader)
121127

128+
self.depth_sampler.release()
129+
122130
self.ctx.disable(mgl.BLEND)
123131
self.ctx.disable(mgl.CULL_FACE)
124132

demosys/opengl/samplers.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
"""
22
Simple cached factory for creating samplers
33
"""
4+
import OpenGL
45
from OpenGL import GL
56
from OpenGL.GL.EXT import texture_filter_anisotropic as tfa
67

8+
OpenGL.ERROR_CHECKING = False
9+
710

811
def create(mipmap=None, anisotropy=None,
912
min_filter=None, mag_filter=None,
10-
wrap_s=None, wrap_t=None, wrap_r=None):
13+
wrap_s=None, wrap_t=None, wrap_r=None,
14+
texture_compare_mode=None,
15+
texture_compare_func=None):
1116
"""Create sampler or get from cache"""
1217
return Sampler(
1318
mipmap=mipmap,
@@ -17,47 +22,63 @@ def create(mipmap=None, anisotropy=None,
1722
wrap_s=wrap_s,
1823
wrap_t=wrap_t,
1924
wrap_r=wrap_r,
25+
texture_compare_mode=texture_compare_mode,
26+
texture_compare_func=texture_compare_func,
2027
)
2128

2229

2330
class Sampler:
2431
"""Represents an immutable sampler we pre-set states"""
2532
def __init__(self, mipmap=None, anisotropy=None,
2633
min_filter=None, mag_filter=None,
27-
wrap_s=None, wrap_t=None, wrap_r=None):
34+
wrap_s=None, wrap_t=None, wrap_r=None,
35+
texture_compare_mode=None, texture_compare_func=None):
2836
"""Set sampler states"""
29-
self.sid = None
37+
self._id = None
38+
3039
self.mipmap = mipmap
3140
self.anisotropy = anisotropy
3241
self.min_filter = min_filter
3342
self.mag_filter = mag_filter
3443
self.wrap_s = wrap_s
3544
self.wrap_t = wrap_t
3645
self.wrap_r = wrap_r
46+
self.texture_compare_mode = texture_compare_mode
47+
self.texture_compare_func = texture_compare_func
48+
3749
self.states()
3850

3951
def use(self, location=0):
40-
GL.glBindSampler(location, self.sid)
52+
GL.glBindSampler(location, self._id)
53+
54+
def release(self, location=0):
55+
GL.glBindSampler(location, 0)
4156

4257
def states(self):
43-
self.sid = GL.glGenSamplers(1)
58+
self._id = GL.glGenSamplers(1)
4459

4560
if self.wrap_s is not None:
46-
GL.glSamplerParameteri(self.sid, GL.GL_TEXTURE_WRAP_S, self.wrap_s)
61+
GL.glSamplerParameteri(self._id, GL.GL_TEXTURE_WRAP_S, self.wrap_s)
4762
if self.wrap_t is not None:
48-
GL.glSamplerParameteri(self.sid, GL.GL_TEXTURE_WRAP_T, self.wrap_t)
63+
GL.glSamplerParameteri(self._id, GL.GL_TEXTURE_WRAP_T, self.wrap_t)
4964
if self.wrap_r is not None:
50-
GL.glSamplerParameteri(self.sid, GL.GL_TEXTURE_WRAP_R, self.wrap_r)
65+
GL.glSamplerParameteri(self._id, GL.GL_TEXTURE_WRAP_R, self.wrap_r)
66+
67+
if self.min_filter is not None:
68+
GL.glSamplerParameteri(self._id, GL.GL_TEXTURE_MIN_FILTER, self.min_filter)
5169

5270
if self.mag_filter is not None:
53-
GL.glSamplerParameteri(self.sid, GL.GL_TEXTURE_MAG_FILTER, self.mag_filter)
71+
GL.glSamplerParameteri(self._id, GL.GL_TEXTURE_MAG_FILTER, self.mag_filter)
5472

5573
if self.mipmap:
56-
GL.glSamplerParameteri(self.sid, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR)
57-
elif self.mipmap is not None:
58-
GL.glSamplerParameteri(self.sid, GL.GL_TEXTURE_MIN_FILTER, self.min_filter)
74+
GL.glSamplerParameteri(self._id, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR)
5975

6076
if self.anisotropy is not None and self.anisotropy > 0:
6177
max_ani = GL.glGetFloatv(tfa.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT)
6278
self.anisotropy = min(max_ani, self.anisotropy)
63-
GL.glSamplerParameterf(self.sid, tfa.GL_TEXTURE_MAX_ANISOTROPY_EXT, self.anisotropy)
79+
GL.glSamplerParameterf(self._id, tfa.GL_TEXTURE_MAX_ANISOTROPY_EXT, self.anisotropy)
80+
81+
if self.texture_compare_mode is False:
82+
GL.glSamplerParameteri(self._id, GL.GL_TEXTURE_COMPARE_MODE, GL.GL_NONE)
83+
elif self.texture_compare_mode is True:
84+
GL.glSamplerParameteri(self._id, GL.GL_TEXTURE_COMPARE_MODE, GL.GL_COMPARE_REF_TO_TEXTURE)

demosys/opengl/texture.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from PIL import Image
44

55
from demosys import context
6+
from demosys.opengl import samplers
67

78

89
class BaseTexture:
@@ -200,6 +201,7 @@ class DepthTexture(BaseTexture):
200201
# Class attributes for drawing the texture
201202
quad = None
202203
shader = None
204+
sampler = None
203205

204206
def __init__(self, size, data=None, samples=0, alignment=8):
205207
"""
@@ -212,6 +214,7 @@ def __init__(self, size, data=None, samples=0, alignment=8):
212214
"""
213215
super().__init__()
214216
self._texture = self.ctx.depth_texture(size, data=data, samples=samples, alignment=alignment)
217+
self._texture.filter = mgl.LINEAR, mgl.LINEAR
215218
_init_depth_texture_draw()
216219

217220
def draw(self, near, far, pos=(0.0, 0.0), scale=(1.0, 1.0)):
@@ -226,10 +229,12 @@ def draw(self, near, far, pos=(0.0, 0.0), scale=(1.0, 1.0)):
226229
self.shader.uniform("scale", (scale[0], scale[1]))
227230
self.shader.uniform("near", near)
228231
self.shader.uniform("far", far)
232+
self.sampler.use(location=0)
229233
self._texture.use(location=0)
230234
self.shader.uniform("texture0", 0)
231235

232236
self.quad.draw(self.shader)
237+
self.sampler.release()
233238

234239

235240
def _init_texture2d_draw():
@@ -303,7 +308,7 @@ def _init_depth_texture_draw():
303308
"uniform float near;"
304309
"uniform float far;"
305310
"void main() {",
306-
" float z = texture(texture0, uv).x;"
311+
" float z = texture(texture0, uv).r;"
307312
" float d = (2.0 * near) / (far + near - z * (far - near));"
308313
" out_color = vec4(d);",
309314
"}",
@@ -313,4 +318,9 @@ def _init_depth_texture_draw():
313318
program.set_source("\n".join(src))
314319
program.prepare()
315320

321+
DepthTexture.sampler = samplers.create(
322+
min_filter=mgl.LINEAR,
323+
mag_filter=mgl.LINEAR,
324+
texture_compare_mode=False,
325+
)
316326
DepthTexture.shader = program

0 commit comments

Comments
 (0)