1Name 2 3 EXT_texture_swizzle 4 5Name Strings 6 7 GL_EXT_texture_swizzle 8 9Contact 10 11 Jeff Bolz, NVIDIA Corporation (jbolz 'at' nvidia.com) 12 13Contributors 14 15 Cass Everitt, Id Software 16 Brian Harris, Id Software 17 Pat Brown, NVIDIA 18 Eric Werness, NVIDIA 19 20Status 21 22 Shipping in October, 2008. 23 24Version 25 26 Last Modified Date: September 9, 2008 27 Author Revision: 1.0 28 29Number 30 31 356 32 33Dependencies 34 35 Written based on the wording of the OpenGL 2.1 specification. 36 37Overview 38 39 Classic OpenGL texture formats conflate texture storage and 40 interpretation, and assume that textures represent color. In 41 modern applications, a significant quantity of textures don't 42 represent color, but rather data like shadow maps, normal maps, 43 page tables, occlusion data, etc.. For the latter class of data, 44 calling the data "RGBA" is just a convenient mapping of what the 45 data is onto the current model, but isn't an accurate reflection 46 of the reality of the data. 47 48 The existing texture formats provide an almost orthogonal set of 49 data types, sizes, and number of components, but the mappings of 50 this storage into what the shader or fixed-function pipeline 51 fetches is very much non-orthogonal. Previous extensions have 52 added some of the most demanded missing formats, but the problem 53 has not been solved once and for all. 54 55 This extension provides a mechanism to swizzle the components 56 of a texture before they are applied according to the texture 57 environment in fixed-function or as they are returned to the 58 shader. 59 60IP Status 61 62 No known IP claims. 63 64New Tokens 65 66 Accepted by the <pname> parameters of TexParameteri, 67 TexParameterf, TexParameteriv, TexParameterfv, 68 GetTexParameterfv, and GetTexParameteriv: 69 70 TEXTURE_SWIZZLE_R_EXT 0x8E42 71 TEXTURE_SWIZZLE_G_EXT 0x8E43 72 TEXTURE_SWIZZLE_B_EXT 0x8E44 73 TEXTURE_SWIZZLE_A_EXT 0x8E45 74 75 Accepted by the <pname> parameters of TexParameteriv, 76 TexParameterfv, GetTexParameterfv, and GetTexParameteriv: 77 78 TEXTURE_SWIZZLE_RGBA_EXT 0x8E46 79 80 81Additions to Chapter 2 of the OpenGL 2.1 Specification (OpenGL Operation) 82 83 Modify Section 2.15.4 (Shader Execution), p. 84: 84 85 Texture Access 86 87 ...and converts it to a texture source color Cs according to table 88 3.20 (section 3.8.13), followed by application of the texture swizzle 89 as described in section 3.8.13. A four-component vector (Rs,Gs,Bs,As) 90 is returned to the vertex shader. 91 92Additions to Chapter 3 of the OpenGL 2.1 Specification (Rasterization) 93 94 Modify Section 3.8.4 (Texture Parameters), p. 168: 95 96 (modify table 3.18, p. 169) 97 Name Type Legal Values 98 ---- ---- ------------ 99 TEXTURE_SWIZZLE_R_EXT enum RED, GREEN, BLUE, ALPHA, ZERO, ONE 100 TEXTURE_SWIZZLE_G_EXT enum RED, GREEN, BLUE, ALPHA, ZERO, ONE 101 TEXTURE_SWIZZLE_B_EXT enum RED, GREEN, BLUE, ALPHA, ZERO, ONE 102 TEXTURE_SWIZZLE_A_EXT enum RED, GREEN, BLUE, ALPHA, ZERO, ONE 103 104 (append to section 3.8.4, p. 170) 105 106 TEXTURE_SWIZZLE_RGBA_EXT sets the same state as the R, G, B, A 107 enums in a single call. 108 109 (append to section 3.8.13, p. 184) 110 The values of the texture parameters TEXTURE_SWIZZLE_R_EXT, 111 TEXTURE_SWIZZLE_G_EXT, TEXTURE_SWIZZLE_B_EXT, and 112 TEXTURE_SWIZZLE_A_EXT, are applied after the swizzling described 113 in Table 3.20 (p. 186). The swizzle parameter 114 TEXTURE_SWIZZLE_R_EXT affects the first component of Cs as: 115 116 if (TEXTURE_SWIZZLE_R_EXT == RED) { 117 Cs'[0] = Cs[0]; 118 } else if (TEXTURE_SWIZZLE_R_EXT == GREEN) { 119 Cs'[0] = Cs[1]; 120 } else if (TEXTURE_SWIZZLE_R_EXT == BLUE) { 121 Cs'[0] = Cs[2]; 122 } else if (TEXTURE_SWIZZLE_R_EXT == ALPHA) { 123 Cs'[0] = As; 124 } else if (TEXTURE_SWIZZLE_R_EXT == ZERO) { 125 Cs'[0] = 0; 126 } else if (TEXTURE_SWIZZLE_R_EXT == ONE) { 127 Cs'[0] = 1; // float or int depending on texture component type 128 } 129 130 and similarly for Cs'[1], Cs'[2], and As'. 131 132 Modify Section 3.11.2 (Shader Execution), p. 197: 133 134 Texture Access 135 136 ...and converts it to a texture source color Cs according to table 137 3.20 (section 3.8.13), followed by application of the texture swizzle 138 as described in section 3.8.13. The GL returns a four-component 139 vector (Rs,Gs,Bs,As) to the fragment shader... 140 141Additions to the AGL/EGL/GLX/WGL Specifications 142 143 None 144 145Errors 146 147 The error INVALID_OPERATION is generated if TexParameteri, 148 TexParameterf, TexParameteriv, or TexParameterfv, parameter <pname> 149 is TEXTURE_SWIZZLE_R_EXT, TEXTURE_SWIZZLE_G_EXT, 150 TEXTURE_SWIZZLE_B_EXT, or TEXTURE_SWIZZLE_A_EXT, and <param> is not 151 RED, GREEN, BLUE, ALPHA, ZERO, or ONE. 152 153 The error INVALID_OPERATION is generated if TexParameteriv, or 154 TexParameterfv, parameter <pname> TEXTURE_SWIZZLE_RGBA_EXT, and 155 the four consecutive values pointed to by <param> are not all 156 RED, GREEN, BLUE, ALPHA, ZERO, or ONE. 157 158New State 159 160 Changes to table 6.16, p. 277 (Texture, state per texture object) 161 162 Initial 163 Get Value Type Get Command Value Description Sec. Attribute 164 --------- ---- ----------- ------- ----------- ---- --------- 165 TEXTURE_SWIZZLE_R_EXT Z GetTexParameter RED Red 3.8.4 texture 166 component swizzle 167 TEXTURE_SWIZZLE_G_EXT Z GetTexParameter GREEN Green 3.8.4 texture 168 component swizzle 169 TEXTURE_SWIZZLE_B_EXT Z GetTexParameter BLUE Blue 3.8.4 texture 170 component swizzle 171 TEXTURE_SWIZZLE_A_EXT Z GetTexParameter ALPHA Alpha 3.8.4 texture 172 component swizzle 173 174Issues 175 176 1) Why not add more new formats instead? 177 178 RESOLVED: Adding new formats is more implementation burden than 179 one might expect. New formats need to be added to the pixel path, 180 must be considered for FBO support, etc.. This extension avoids 181 those issues by solely affecting the result of a texture fetch. 182 183 2) What is the demand for this extension? 184 185 RESOLVED: There are several independent demands for this, 186 including: 187 - OpenGL 3.0 deprecated support for ALPHA, LUMINANCE, 188 LUMINANCE_ALPHA, and INTENSITY formats. This extension provides 189 a simple porting path for legacy applications that used these 190 formats. 191 192 - There have been specific requests for (1,1,1,a), or "white alpha" 193 formats that allow a "decal" texture to be used in the same shader 194 as an RGBA texture. This can be accomplished with an OpenGL 2.1 195 ALPHA texture by doing 196 197 TexParameteri(target, TEXTURE_SWIZZLE_R_EXT, ONE); 198 TexParameteri(target, TEXTURE_SWIZZLE_G_EXT, ONE); 199 TexParameteri(target, TEXTURE_SWIZZLE_B_EXT, ONE); 200 // TEXTURE_SWIZZLE_A_EXT is already ALPHA 201 or equivalently 202 GLint swiz[4] = {ONE, ONE, ONE, ALPHA}; 203 TexParameteriv(target, TEXTURE_SWIZZLE_RGBA_EXT, swiz); 204 205 or in OpenGL 3.0 "preview" contexts where ALPHA internal formats 206 are deprecated by using a RED texture: 207 TexParameteri(target, TEXTURE_SWIZZLE_R_EXT, ONE); 208 TexParameteri(target, TEXTURE_SWIZZLE_G_EXT, ONE); 209 TexParameteri(target, TEXTURE_SWIZZLE_B_EXT, ONE); 210 TexParameteri(target, TEXTURE_SWIZZLE_A_EXT, RED); 211 or equivalently 212 GLint swiz[4] = {ONE, ONE, ONE, RED}; 213 TexParameteriv(target, TEXTURE_SWIZZLE_RGBA_EXT, swiz); 214 215 - This functionality is available on Sony PlayStation 3 and can 216 simplify porting those applications to OpenGL. 217 218 3) What names should be used for the components, in both the "source" 219 (<param>) and "destination" (<pname>) enums? RGBA? XYZW? 0123? 220 221 RESOLVED: RGBA for both source and destination. 0123 could cause 222 confusion with ZERO and ONE. RGBA is a natural choice for source, 223 because the spec describes the values returned by textures as "Color" 224 and "Alpha." There's no particular precedent for destination to be 225 XYZW as this is still part of texture and not the shader, so RGBA 226 it is. 227 228 4) How does this interact with depth component textures? 229 230 RESOLVED: The swizzle is applied after the DEPTH_TEXTURE_MODE. This 231 naturally falls out of specifying the swizzle in terms of Table 3.20. 232 233 5) How does this interact with sRGB? 234 235 RESOLVED: The swizzle is applied after sRGB conversion. 236 237 6) How does this interact with NV_register_combiners, 238 NV_texture_shader, and other old "shading" extensions that 239 predate NV/ARB_fragment_program and define their own "interesting" 240 swizzles? 241 242 RESOLVED: Undefined. Core fixed-function shading (texture_env/combine) 243 are fully supported, but old proprietary "configurable shading" 244 extensions are not. 245 246 7) How does the swizzle interact with the fixed-function texture 247 environment referencing the base format? 248 249 RESOLVED: Note that, by virtue of being defined in terms of Table 3.20, 250 the swizzling occurs *before* the texture environment functions 251 (Table 3.21 and 3.22) are applied. So if you used the first example 252 under Issue (2) with fixed-function, the primary color would "pass 253 through" because the ALPHA base format environment function would 254 still apply. In order to effectively swizzle all four components, an 255 INTENSITY format would give the desired result with the same storage 256 requirements. 257 258Revision History 259 260 Revision 1, 2008/08/21 261 - Initial draft 262 Revision 2, 2009/01/28 263 - Add edits to fragment/vertex shader sections to clarify that the 264 swizzle is applied in both. 265