Skip to content

Commit

Permalink
Work out issues with integer vs float
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturk committed Dec 6, 2024
1 parent 4c79a4b commit a472dff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
34 changes: 28 additions & 6 deletions yt_idv/opengl_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,21 @@
4: (GL.GL_UNSIGNED_BYTE, GL.GL_RGBA8, GL.GL_RGBA),
},
"uint32": {
1: (GL.GL_UNSIGNED_INT, GL.GL_R32UI, GL.GL_RED),
1: (GL.GL_UNSIGNED_INT, GL.GL_R32UI, GL.GL_RED_INTEGER),
2: (GL.GL_UNSIGNED_INT, GL.GL_RG32UI, GL.GL_RG),
3: (GL.GL_UNSIGNED_INT, GL.GL_RGB32UI, GL.GL_RGB),
4: (GL.GL_UNSIGNED_INT, GL.GL_RGBA32UI, GL.GL_RGBA),
},
"int32": {
1: (GL.GL_INT, GL.GL_R32I, GL.GL_RED_INTEGER),
2: (GL.GL_INT, GL.GL_RG32I, GL.GL_RG),
3: (GL.GL_INT, GL.GL_RGB32I, GL.GL_RGB),
4: (GL.GL_INT, GL.GL_RGBA32I, GL.GL_RGBA),
},
}

SKIP_MIPMAP = [GL.GL_R32UI, GL.GL_R16UI, GL.GL_R8UI, GL.GL_R32I, GL.GL_R16I, GL.GL_R8I]


def coerce_uniform_type(val, gl_type):
# gl_type here must be in const_types
Expand Down Expand Up @@ -193,12 +201,23 @@ def bind_as_image(self, target=0, override_mode=None):
_ = GL.glActiveTexture(TEX_TARGETS[target])
mode = override_mode or self.image_mode
_ = GL.glBindImageTexture(
0, self.texture_name, 0, False, 0, mode, self.channels
target, self.texture_name, 0, False, 0, mode, self.channels
)
yield
_ = GL.glActiveTexture(TEX_TARGETS[target])
GL.glBindImageTexture(0, 0, 0, False, 0, mode, self.channels)

def read_as_image(self):
if len(self.data.shape) == 2:
channels = self.data.shape[-1]
else:
channels = 1
gl_type, _, type2 = TEX_CHANNELS[self.data.dtype.name][channels]
with self.bind():
data = np.zeros(self.data.shape, dtype=self.data.dtype)
GL.glGetTexImage(self.dim_enum, 0, type2, gl_type, data)
return data


class Texture1D(Texture):
boundary_x = TextureBoundary()
Expand All @@ -217,7 +236,7 @@ def _set_data(self, change):
gl_type, type1, type2 = TEX_CHANNELS[data.dtype.name][channels]
GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1)
if not isinstance(change["old"], np.ndarray):
GL.glTexStorage1D(GL.GL_TEXTURE_1D, 6, type1, dx)
GL.glTexStorage1D(GL.GL_TEXTURE_1D, 1, type1, dx)
GL.glTexSubImage1D(GL.GL_TEXTURE_1D, 0, 0, dx, type2, gl_type, data)
GL.glTexParameterf(GL.GL_TEXTURE_1D, GL.GL_TEXTURE_WRAP_S, self.boundary_x)
GL.glTexParameteri(
Expand All @@ -226,7 +245,8 @@ def _set_data(self, change):
GL.glTexParameteri(
GL.GL_TEXTURE_1D, GL.GL_TEXTURE_MAG_FILTER, self.mag_filter
)
GL.glGenerateMipmap(GL.GL_TEXTURE_1D)
if type1 not in SKIP_MIPMAP:
GL.glGenerateMipmap(GL.GL_TEXTURE_1D)


class ColormapTexture(Texture1D):
Expand Down Expand Up @@ -286,7 +306,8 @@ def _set_data(self, change):
GL.glTexParameteri(
GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, self.mag_filter
)
GL.glGenerateMipmap(GL.GL_TEXTURE_2D)
if type1 not in SKIP_MIPMAP:
GL.glGenerateMipmap(GL.GL_TEXTURE_2D)


class TransferFunctionTexture(Texture2D):
Expand Down Expand Up @@ -347,7 +368,8 @@ def _set_data(self, change):
GL.glTexParameteri(
GL.GL_TEXTURE_3D, GL.GL_TEXTURE_MAG_FILTER, self.mag_filter
)
GL.glGenerateMipmap(GL.GL_TEXTURE_3D)
if type1 not in SKIP_MIPMAP:
GL.glGenerateMipmap(GL.GL_TEXTURE_3D)


class VertexAttribute(traitlets.HasTraits):
Expand Down
18 changes: 12 additions & 6 deletions yt_idv/scene_annotations/block_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BlockHistogram(SceneAnnotation):

@traitlets.default("output_data")
def _default_output_data(self):
return Texture1D(data=np.zeros(self.bins, dtype="u4"), channels="r32i")
return Texture1D(data=np.zeros(self.bins, dtype="u4"), channels="r32ui")

def _set_uniforms(self, scene, shader_program):
pass
Expand All @@ -38,18 +38,24 @@ def compute(self, scene, program):
# We need a place to dump our stuff.
self.visible = False
self.output_data.clear()
for _tex_ind, tex, bitmap_tex in self.data.viewpoint_iter(scene.camera):
# We now need to bind our textures. We don't care about positions.
with tex.bind(target=0):
with bitmap_tex.bind(target=1):
with self.output_data.bind_as_image(2):
total = 0
with self.output_data.bind_as_image(2):
for _tex_ind, tex, bitmap_tex in self.data.viewpoint_iter(scene.camera):
# We now need to bind our textures. We don't care about positions.
total += bitmap_tex.data.size
with tex.bind(target=0):
with bitmap_tex.bind(target=1):
GL.glUseProgram(program.program)
self._set_compute_uniforms(scene, program)
# This will need to be carefully chosen based on our
# architecture, I guess. That aspect of running compute
# shaders, CUDA, etc, is one of my absolute least favorite
# parts.
GL.glDispatchCompute(self.bins, 1, 1)
# arr = self.output_data.read_as_image()
# print(
# f"histogram: {arr=} => {arr.sum()=} with {total=} for a ratio of {arr.sum()/total}"
# )

def draw(self, scene, program):
# This will probably need to have somewhere to draw the darn thing. So
Expand Down

0 comments on commit a472dff

Please sign in to comment.