Skip to content
Snippets Groups Projects
Commit 8ca951c8 authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Playing with a simple ARB v/f priogram parser, so I can dynamically bind

texture/param/attribute/varying locations. All the things are broken.
parent 40029533
No related branches found
No related tags found
No related merge requests found
...@@ -66,29 +66,26 @@ TEMP useNegCmap; ...@@ -66,29 +66,26 @@ TEMP useNegCmap;
TEMP negVoxValue; TEMP negVoxValue;
TEMP negClipValue; TEMP negClipValue;
PARAM imageShape = program.local[4]; PARAM imageShape = {{ param.imageShape }};
PARAM clipping = program.local[5]; PARAM clipping = {{ param.clipping }};
PARAM negCmap = program.local[6]; PARAM negCmap = {{ param.negCmap }};
# This matrix scales the voxel value to # This matrix scales the voxel value to
# lie in a range which is appropriate to # lie in a range which is appropriate to
# the current display range # the current display range
PARAM voxValXform[4] = { program.local[0], PARAM voxValXform[4] = {{ param.voxValXform }};
program.local[1],
program.local[2],
program.local[3] };
# retrieve the voxel coordinates, # retrieve the voxel coordinates,
# bail if they are are out of bounds # bail if they are are out of bounds
MOV voxCoord, fragment.texcoord[1]; MOV voxCoord, {{ varying.voxCoord }};
#pragma include test_in_bounds.prog #pragma include test_in_bounds.prog
# look up image voxel value # look up image voxel value
# and clipping value from 3D # and clipping value from 3D
# image/clipping textures # image/clipping textures
TEX voxValue.x, fragment.texcoord[0], texture[0], 3D; TEX voxValue.x, {{ varying.texCoord }}, {{ texture.imageTexture }}, 3D;
TEX clipValue.x, fragment.texcoord[0], texture[3], 3D; TEX clipValue.x, {{ varying.texCoord }}, {{ texture.clipTexture }}, 3D;
# If the image texture is the clip # If the image texture is the clip
# texture, overwrite the clip value # texture, overwrite the clip value
...@@ -165,8 +162,8 @@ MAD voxValue, voxValue, voxValXform[0].x, voxValXform[3].x; ...@@ -165,8 +162,8 @@ MAD voxValue, voxValue, voxValXform[0].x, voxValXform[3].x;
# look up the appropriate colour # look up the appropriate colour
# in the 1D colour map texture # in the 1D colour map texture
TEX posColour, voxValue.x, texture[1], 1D; TEX posColour, voxValue.x, {{ texture.colourTexture }}, 1D;
TEX negColour, voxValue.x, texture[2], 1D; TEX negColour, voxValue.x, {{ texture.negColourTexture }}, 1D;
# useNegCmap is negative if the # useNegCmap is negative if the
# negative colour map should be # negative colour map should be
......
...@@ -6,17 +6,18 @@ ...@@ -6,17 +6,18 @@
# passes the corresponding voxel and texture coordinates through to the # passes the corresponding voxel and texture coordinates through to the
# fragment program. # fragment program.
# #
# Inputs: # Input parameters:
# vertex.texcoord[0] - Texture coordinates # imageShape - image shape
# program.local[0] - image shape #
# # Input attributes:
# texCoord - Texture coordinates
# #
# Outputs: # Outputs:
# result.texcoord[0] - Texture coordinates # result.texcoord[0] - Texture coordinates
# result.texcoord[1] - Voxel coordinates # result.texcoord[1] - Voxel coordinates
# #
PARAM imageShape = program.local[0]; PARAM imageShape = {{ param.imageShape }};
TEMP voxCoord; TEMP voxCoord;
...@@ -26,12 +27,12 @@ DP4 result.position.y, state.matrix.mvp.row[1], vertex.position; ...@@ -26,12 +27,12 @@ DP4 result.position.y, state.matrix.mvp.row[1], vertex.position;
DP4 result.position.z, state.matrix.mvp.row[2], vertex.position; DP4 result.position.z, state.matrix.mvp.row[2], vertex.position;
DP4 result.position.w, state.matrix.mvp.row[3], vertex.position; DP4 result.position.w, state.matrix.mvp.row[3], vertex.position;
MOV result.texcoord[0], vertex.texcoord[0]; # Transform the texture coordinates
# into voxel coordinates
# Transform the texture coordinates into voxel coordinates MOV voxCoord, {{ attr.texCoord }};
MOV voxCoord, vertex.texcoord[0];
MUL voxCoord, voxCoord, imageShape; MUL voxCoord, voxCoord, imageShape;
MOV result.texcoord[1], voxCoord; MOV {{ varying.texCoord }}, {{ attr.texCoord }};
MOV {{ varying.voxCoord }}, voxCoord;
END END
#!/usr/bin/env python
#
# __init__.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import glsl.parse as glslparse
import glsl.program as glslprogram
import arbp.parse as arbpparse
import arbp.program as arbpprogram
GLSLShader = glslprogram.GLSLShader
ARBShaer = arbpprogram.ARBShader
parseGLSL = glslparse .parseGLSL
parseARBP = arbpparse .parseARBP
#!/usr/bin/env python
#
# parse.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import jinja2
#
# {{ params.paramName }} -> program.local[X]
#
# Or, if 'paramName' has length > 1
#
# {{ params.paramName }} -> { program.local[x], program.local[x + 1], ... }
# {{ attrs.attName }} -> vertex.texcoord[N]
#
# {{ textures.texName }} -> texture[N]
# Maybe this too? In vertex program:
# {{ varying.outputName }} -> result.texcoord[N]
#
# In fragment program:
# {{ varying.outputName }} -> fragment.texcoord[N]
def parseARBP(source,
vert,
paramMap=None,
textureMap=None,
attrMap=None,
varyingMap=None):
if paramMap is None: paramMap = {}
if textureMap is None: textureMap = {}
if attrMap is None: attrMap = {}
if varyingMap is None: varyingMap = {}
params = {}
textures = {}
attrs = {}
varyings = {}
for name, num in paramMap .items(): params[ name] = _param( num)
for name, num in textureMap.items(): textures[name] = _texture(num)
for name, num in attrMap .items(): attrs[ name] = _attr( num)
for name, num in varyingMap.items(): varyings[name] = _varying(num, vert)
template = jinja2.Template(source)
parsedSrc = template.render(param=params,
texture=textures,
attr=attrs,
varying=varyings)
return parsedSrc
def _param(number):
try: number, length = number
except: number, length = number, 1
if length == 1:
return 'program.local[{}]'.format(number)
else:
bits = ['program.local[{}]'.format(n) for n in range(number,
number + length)]
return '{{ {} }}'.format(','.join(bits))
def _texture(number):
return 'texture[{}]'.format(number)
def _attr(number):
return 'vertex.texcoord[{}]'.format(number)
def _varying(number, vert):
if vert: return 'result.texcoord[{}]' .format(number)
else: return 'fragment.texcoord[{}]'.format(number)
#!/usr/bin/env python
#
# program.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import logging
log = logging.getLogger(__name__)
class ARBProgram(object):
def __init__(self,
vertSrc,
fragSrc,
paramMap=None,
textureMap=None,
vertAttMap=None):
if textureMap is None: textureMap = {}
if paramMap is None: paramMap = {}
if vertAttMap is None: vertAttMap = {}
File moved
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# #
# Author: Paul McCarthy <pauldmccarthy@gmail.com> # Author: Paul McCarthy <pauldmccarthy@gmail.com>
# #
"""This module provides he :class:`ShaderProgram` class, which encapsulates """This module provides he :class:`GLSLShader` class, which encapsulates
a GLSL shader program comprising a vertex shader and a fragment shader. a GLSL shader program comprising a vertex shader and a fragment shader.
""" """
...@@ -35,14 +35,14 @@ corresponding GL types and sizes. ...@@ -35,14 +35,14 @@ corresponding GL types and sizes.
""" """
class ShaderProgram(object): class GLSLShader(object):
"""The ``ShaderProgram`` class encapsulates information and logic about """The ``GLSLShader`` class encapsulates information and logic about
a GLSL 1.20 shader program, comprising a vertex shader and a fragment a GLSL 1.20 shader program, comprising a vertex shader and a fragment
shader. It provides methods to set shader attribute and uniform values, shader. It provides methods to set shader attribute and uniform values,
to configure attributes, and to load/unload the program. Furthermore, to configure attributes, and to load/unload the program. Furthermore,
the ``ShaderProgram`` makes sure that all uniform and attribut variables the ``GLSLShader`` makes sure that all uniform and attribut variables
are converted to the appropriate type. The following methods are available are converted to the appropriate type. The following methods are available
on a ``ShaderProgram``: on a ``GLSLShader``:
.. autosummary:: .. autosummary::
...@@ -58,13 +58,13 @@ class ShaderProgram(object): ...@@ -58,13 +58,13 @@ class ShaderProgram(object):
setIndices setIndices
Typical usage of a ``ShaderProgram`` will look something like the Typical usage of a ``GLSLShader`` will look something like the
following:: following::
vertSrc = 'vertex shader source' vertSrc = 'vertex shader source'
fragSrc = 'vertex shader source' fragSrc = 'vertex shader source'
program = ShaderProgram(vertSrc, fragSrc) program = GLSLShader(vertSrc, fragSrc)
# Load the program # Load the program
program.load() program.load()
...@@ -97,7 +97,7 @@ class ShaderProgram(object): ...@@ -97,7 +97,7 @@ class ShaderProgram(object):
def __init__(self, vertSrc, fragSrc, indexed=False): def __init__(self, vertSrc, fragSrc, indexed=False):
"""Create a ``ShaderProgram``. """Create a ``GLSLShader``.
:arg vertSrc: String containing vertex shader source code. :arg vertSrc: String containing vertex shader source code.
...@@ -166,7 +166,7 @@ class ShaderProgram(object): ...@@ -166,7 +166,7 @@ class ShaderProgram(object):
def load(self): def load(self):
"""Loads this ``ShaderProgram`` into the GL state. """Loads this ``GLSLShader`` into the GL state.
""" """
gl.glUseProgram(self.program) gl.glUseProgram(self.program)
...@@ -224,7 +224,7 @@ class ShaderProgram(object): ...@@ -224,7 +224,7 @@ class ShaderProgram(object):
def delete(self): def delete(self):
"""Deletes all GL resources managed by this ``ShaderProgram`. """ """Deletes all GL resources managed by this ``GLSLShader`. """
gl.glDeleteProgram(self.program) gl.glDeleteProgram(self.program)
for buf in self.buffers.values(): for buf in self.buffers.values():
...@@ -283,7 +283,7 @@ class ShaderProgram(object): ...@@ -283,7 +283,7 @@ class ShaderProgram(object):
def setIndices(self, indices): def setIndices(self, indices):
"""If an index array is to be used by this ``ShaderProgram`` (see the """If an index array is to be used by this ``GLSLShader`` (see the
``indexed`` argument to :meth:`__init__`), the index array may be set ``indexed`` argument to :meth:`__init__`), the index array may be set
via this method. via this method.
""" """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment