190 likes | 340 Views
Offset and Dependent Texture Shaders in OpenGL. Cass Everitt NVIDIA Corporation cass@nvidia.com. Overview. What are texture shaders? Offset texture shaders Dependent texture shaders New texture formats GL_DSDT_NV (internal and external) GL_DSDT_MAG_NV (internal and external)
E N D
Offset and Dependent Texture Shaders in OpenGL Cass Everitt NVIDIA Corporation cass@nvidia.com
Overview • What are texture shaders? • Offset texture shaders • Dependent texture shaders • New texture formats • GL_DSDT_NV (internal and external) • GL_DSDT_MAG_NV (internal and external) • GL_DSDT_MAG_INTENSITY_NV (internal only) • GL_DSDT_MAG_VIB_NV (external only) • Shader stage operations • GL_OFFSET_TEXTURE_2D_NV • GL_OFFSET_TEXTURE_2D_SCALE_NV • GL_DEPENDENT_AR_TEXTURE_2D_NV • GL_DEPENDENT_GB_TEXTURE_2D_NV
ARB_multitexture only ARB_multitexture with NV_texture_shader unit 0fetch stage 0math & fetch (s,t,r,q)0 (s,t,r,q)0 GL_TEXTURE0_ARB GL_TEXTURE0_ARB unit 1fetch stage 1math & fetch (s,t,r,q)1 (s,t,r,q)1 GL_TEXTURE1_ARB GL_TEXTURE1_ARB iterated texture coordinates… iterated texture coordinates… to the combiners… to the combiners… unit 2fetch stage 2math & fetch (s,t,r,q)2 (s,t,r,q)2 GL_TEXTURE2_ARB GL_TEXTURE2_ARB What are Texture Shaders? • Texture Shaders (when enabled) replace the standard OpenGL texture fetch mechanism • With texture shaders, results from previous stages can affect the lookup of a subsequent stage
What are Offset Texture Shaders? • Offset texture shaders transform the results of a previous texture lookup and use it to bias the (s,t) of its stage for a non-projective 2D lookup stage 0 TEXTURE_2D GL_DSDT_NVinternal format Offset texture 2Dmatrix (s0,t0) on to texture environment or register combiners stage 1 OFFSET_TEXTURE_2D_NV nonprojective_lookup_2D( s1+ ds’, t1 + dt’ ) RGBA result (s1,t1)
What are Dependent Texture Shaders? • Dependent texture shaders use the results of a previous texture lookup as the (s,t) for a non-projective 2D lookup stage 0 TEXTURE_2D GL_RGBA8internal format (s0,t0) on to texture environment or register combiners RGBA stage 1 DEPENDENT_AR_-TEXTURE_2D_NV nonprojective_lookup_2D( A, R ) Texture coordinates unused … RGBA result
New Texture Formats • GL_DSDT_NV • This format encodes an offset vector in texture space • ds and dt are mapped to the range [-1,1] offset vector dt ds t s
New Texture Formats (2) • GL_DSDT_MAG_NV • For the offset portion, this format behaves identically to GL_DSDT_NV • The RGB fetched in the 2D texture lookup is modulated by a factor computed using the MAG component • when using GL_OFFSET_TEXTURE_2D_SCALED_NV shader
New Texture Formats (3) • GL_DSDT_MAG_INTENSITY_NV (internal) and GL_DSDT_MAG_VIB_NV (external) • The DSDT and MAG portions behave as in the previous texture formats • The external VIB term is converted to INTENSITY for internal format • This format is important because for color purposes, it behaves as if its internal format is INTENSITY, and thus (unlike GL_DSDT_NV and GL_DSDT_MAG_NV) produces valid RGBA results • This format can be used with NV_texture_shader disabled
GL_OFFSET_TEXTURE_2D_NV • (ds,dt) is accessed from a previous texture lookup • The internal texture format from the previous stage selected must be one of • GL_DSDT_NV, • GL_DSDT_MAG_NV, or • GL_DSDT_MAG_INTENSITY_NV • Previous stage is not limited to GL_TEXTURE_2D shader • 1D, 2D, 3D, and cube map all valid choices • Extra indirection if previous stage is itself an offset or dependent shader!
GL_OFFSET_TEXTURE_2D_NV (2) • Per-stage 2x2 matrix (O) transforms the [ds,dt]t before biasing the incoming (s,t) • Offset texture 2D matrix should also include scaling since ds and dt are low precision DSDT from previous stage dt Offset texture 2Dmatrix dt’ ds ds’ t t s s
GL_TEXTURE_OFFSET_2D_NV (3) • Once [ds’, dt’]t has been calculated, the fetch is simply a non-projective 2D lookup of the biased 2D texture coordinates • nonprojective_lookup_2D(s + ds’, t + dt’) • The offset texture matrix is set by • glTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_2D_MATRIX_NV, matrixptr);
GL_OFFSET_TEXTURE_2D_-SCALED_NV • Functions identically to GL_OFFSET_TEXTURE_2D_NV except that the previous texture provides [ds, dt]t and mag • The mag component is used to calculate a scaling term that modulates the red, green, and blue terms (but not alpha) of the resulting 2D texture lookup • scale = offsetTextureBias + offsetTextureScale * mag
GL_OFFSET_TEXTURE_2D_-SCALED_NV (2) • Bias and scale of the mag component are set per-stage via glTexEnv() calls • glTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_2D_BIAS_NV, mag_bias); • glTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_2D_SCALE_NV, mag_scale);
GL_OFFSET_TEXTURE_2D_-SCALED_NV (3) • Internal format of the previous stage must be GL_DSDT_MAG_NV or GL_DSDT_MAG_INTENSITY_NV • The stage’s internal texture format must be an unsigned color format • No SIGNED_RGB or SIGNED_RGBA • No DSDT • No HILO
GL_DEPENDENT_AR_TEXTURE_-2D_NV • The previous texture shader’s alpha and red colors are used as the (s,t) for non-projective 2D texture lookup stage 0 TEXTURE_2D GL_RGBA8internal format (s0,t0) on to texture environment or register combiners RGBA stage 1 DEPENDENT_AR_-TEXTURE_2D_NV nonprojective_lookup_2D( A, R ) Texture coordinates unused … RGBA result
GL_DEPENDENT_GB_TEXTURE_-2D_NV • The previous texture shader’s green and blue colors are used as the (s,t) for non-projective 2D texture lookup stage 0 TEXTURE_2D GL_RGBA8internal format (s0,t0) on to texture environment or register combiners RGBA stage 1 DEPENDENT_GB_-TEXTURE_2D_NV nonprojective_lookup_2D( G, B ) Texture coordinates unused … RGBA result
Considerations • Be careful – precision is limited • Use the full range for DSDT, scale with the 2x2 matrix • Don’t access high frequency textures with dependent shaders • Offset texture shaders can be used to perform the equivalent of DX6 EMBM
Considerations (2) • Exploit multiple offset stages – for example • stage 0: GL_TEXTURE_2D • apply a noise DSDT texture • stage 1: GL_OFFSET_TEXTURE_2D_NV • stage 0 as the previous input • same noise texture, scale tex coords by 2 • stage 2: GL_OFFSET_TEXTURE_2D_NV • stage 1 as the previous input • same noise texture, scale tex coords by 4 • stage 2: GL_OFFSET_TEXTURE_2D_NV • stage 2 as the previous input • RGBA texture