1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * Generate fragment programs to implement pixel transfer ops, such as
30 * scale/bias, colormatrix, colortable, convolution...
36 #include "main/imports.h"
37 #include "main/image.h"
38 #include "main/macros.h"
39 #include "shader/program.h"
40 #include "shader/prog_instruction.h"
41 #include "shader/prog_parameter.h"
42 #include "shader/prog_print.h"
44 #include "st_context.h"
45 #include "st_format.h"
46 #include "st_program.h"
47 #include "st_texture.h"
49 #include "pipe/p_screen.h"
50 #include "pipe/p_context.h"
51 #include "util/u_pack_color.h"
56 GLuint scaleAndBias
:1;
58 GLuint colorMatrixPostScaleBias
:1;
62 GLfloat Maps
[3][256][4];
65 pipeline_stage Stages
[STAGE_MAX
];
66 GLboolean StagesUsed
[STAGE_MAX
];
67 GLfloat Scale1
[4], Bias1
[4];
68 GLfloat Scale2
[4], Bias2
[4];
74 is_identity(const GLfloat m
[16])
77 for (i
= 0; i
< 16; i
++) {
78 const int row
= i
% 4, col
= i
/ 4;
79 const float val
= (GLfloat
)(row
== col
);
88 make_state_key(GLcontext
*ctx
, struct state_key
*key
)
90 static const GLfloat zero
[4] = { 0.0f
, 0.0f
, 0.0f
, 0.0f
};
91 static const GLfloat one
[4] = { 1.0f
, 1.0f
, 1.0f
, 1.0f
};
93 memset(key
, 0, sizeof(*key
));
95 if (ctx
->Pixel
.RedBias
!= 0.0 || ctx
->Pixel
.RedScale
!= 1.0 ||
96 ctx
->Pixel
.GreenBias
!= 0.0 || ctx
->Pixel
.GreenScale
!= 1.0 ||
97 ctx
->Pixel
.BlueBias
!= 0.0 || ctx
->Pixel
.BlueScale
!= 1.0 ||
98 ctx
->Pixel
.AlphaBias
!= 0.0 || ctx
->Pixel
.AlphaScale
!= 1.0) {
99 key
->scaleAndBias
= 1;
102 if (!is_identity(ctx
->ColorMatrixStack
.Top
->m
)) {
103 key
->colorMatrix
= 1;
106 if (!TEST_EQ_4V(ctx
->Pixel
.PostColorMatrixScale
, one
) ||
107 !TEST_EQ_4V(ctx
->Pixel
.PostColorMatrixBias
, zero
)) {
108 key
->colorMatrixPostScaleBias
= 1;
111 key
->pixelMaps
= ctx
->Pixel
.MapColorFlag
;
115 static struct pipe_texture
*
116 create_color_map_texture(GLcontext
*ctx
)
118 struct pipe_context
*pipe
= ctx
->st
->pipe
;
119 struct pipe_texture
*pt
;
120 enum pipe_format format
;
121 const uint texSize
= 256; /* simple, and usually perfect */
123 /* find an RGBA texture format */
124 format
= st_choose_format(pipe
, GL_RGBA
, PIPE_TEXTURE_2D
, PIPE_TEXTURE_USAGE_SAMPLER
);
126 /* create texture for color map/table */
127 pt
= st_texture_create(ctx
->st
, PIPE_TEXTURE_2D
, format
, 0,
128 texSize
, texSize
, 1, PIPE_TEXTURE_USAGE_SAMPLER
);
134 * Update the pixelmap texture with the contents of the R/G/B/A pixel maps.
137 load_color_map_texture(GLcontext
*ctx
, struct pipe_texture
*pt
)
139 struct pipe_context
*pipe
= ctx
->st
->pipe
;
140 struct pipe_screen
*screen
= pipe
->screen
;
141 struct pipe_transfer
*transfer
;
142 const GLuint rSize
= ctx
->PixelMaps
.RtoR
.Size
;
143 const GLuint gSize
= ctx
->PixelMaps
.GtoG
.Size
;
144 const GLuint bSize
= ctx
->PixelMaps
.BtoB
.Size
;
145 const GLuint aSize
= ctx
->PixelMaps
.AtoA
.Size
;
146 const uint texSize
= pt
->width
[0];
150 transfer
= screen
->get_tex_transfer(screen
, pt
, 0, 0, 0, PIPE_TRANSFER_WRITE
,
151 0, 0, texSize
, texSize
);
152 dest
= (uint
*) screen
->transfer_map(screen
, transfer
);
154 /* Pack four 1D maps into a 2D texture:
155 * R map is placed horizontally, indexed by S, in channel 0
156 * G map is placed vertically, indexed by T, in channel 1
157 * B map is placed horizontally, indexed by S, in channel 2
158 * A map is placed vertically, indexed by T, in channel 3
160 for (i
= 0; i
< texSize
; i
++) {
161 for (j
= 0; j
< texSize
; j
++) {
162 int k
= (i
* texSize
+ j
);
163 ubyte r
= ctx
->PixelMaps
.RtoR
.Map8
[j
* rSize
/ texSize
];
164 ubyte g
= ctx
->PixelMaps
.GtoG
.Map8
[i
* gSize
/ texSize
];
165 ubyte b
= ctx
->PixelMaps
.BtoB
.Map8
[j
* bSize
/ texSize
];
166 ubyte a
= ctx
->PixelMaps
.AtoA
.Map8
[i
* aSize
/ texSize
];
167 util_pack_color_ub(r
, g
, b
, a
, pt
->format
, dest
+ k
);
171 screen
->transfer_unmap(screen
, transfer
);
172 screen
->tex_transfer_destroy(transfer
);
180 * Returns a fragment program which implements the current pixel transfer ops.
182 static struct gl_fragment_program
*
183 get_pixel_transfer_program(GLcontext
*ctx
, const struct state_key
*key
)
185 struct st_context
*st
= ctx
->st
;
186 struct prog_instruction inst
[MAX_INST
];
187 struct gl_program_parameter_list
*params
;
188 struct gl_fragment_program
*fp
;
190 const GLuint colorTemp
= 0;
192 fp
= (struct gl_fragment_program
*)
193 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
197 params
= _mesa_new_parameter_list();
200 * Get initial pixel color from the texture.
201 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
203 _mesa_init_instructions(inst
+ ic
, 1);
204 inst
[ic
].Opcode
= OPCODE_TEX
;
205 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
206 inst
[ic
].DstReg
.Index
= colorTemp
;
207 inst
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
208 inst
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
209 inst
[ic
].TexSrcUnit
= 0;
210 inst
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
212 fp
->Base
.InputsRead
= (1 << FRAG_ATTRIB_TEX0
);
213 fp
->Base
.OutputsWritten
= (1 << FRAG_RESULT_COLOR
);
214 fp
->Base
.SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
216 if (key
->scaleAndBias
) {
217 static const gl_state_index scale_state
[STATE_LENGTH
] =
218 { STATE_INTERNAL
, STATE_PT_SCALE
, 0, 0, 0 };
219 static const gl_state_index bias_state
[STATE_LENGTH
] =
220 { STATE_INTERNAL
, STATE_PT_BIAS
, 0, 0, 0 };
221 GLfloat scale
[4], bias
[4];
222 GLint scale_p
, bias_p
;
224 scale
[0] = ctx
->Pixel
.RedScale
;
225 scale
[1] = ctx
->Pixel
.GreenScale
;
226 scale
[2] = ctx
->Pixel
.BlueScale
;
227 scale
[3] = ctx
->Pixel
.AlphaScale
;
228 bias
[0] = ctx
->Pixel
.RedBias
;
229 bias
[1] = ctx
->Pixel
.GreenBias
;
230 bias
[2] = ctx
->Pixel
.BlueBias
;
231 bias
[3] = ctx
->Pixel
.AlphaBias
;
233 scale_p
= _mesa_add_state_reference(params
, scale_state
);
234 bias_p
= _mesa_add_state_reference(params
, bias_state
);
236 /* MAD colorTemp, colorTemp, scale, bias; */
237 _mesa_init_instructions(inst
+ ic
, 1);
238 inst
[ic
].Opcode
= OPCODE_MAD
;
239 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
240 inst
[ic
].DstReg
.Index
= colorTemp
;
241 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
242 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
243 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
244 inst
[ic
].SrcReg
[1].Index
= scale_p
;
245 inst
[ic
].SrcReg
[2].File
= PROGRAM_STATE_VAR
;
246 inst
[ic
].SrcReg
[2].Index
= bias_p
;
250 if (key
->pixelMaps
) {
251 const GLuint temp
= 1;
253 /* create the colormap/texture now if not already done */
254 if (!st
->pixel_xfer
.pixelmap_texture
) {
255 st
->pixel_xfer
.pixelmap_texture
= create_color_map_texture(ctx
);
258 /* with a little effort, we can do four pixel map look-ups with
259 * two TEX instructions:
262 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
263 _mesa_init_instructions(inst
+ ic
, 1);
264 inst
[ic
].Opcode
= OPCODE_TEX
;
265 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
266 inst
[ic
].DstReg
.Index
= temp
;
267 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_XY
; /* write R,G */
268 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
269 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
270 inst
[ic
].TexSrcUnit
= 1;
271 inst
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
274 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
275 _mesa_init_instructions(inst
+ ic
, 1);
276 inst
[ic
].Opcode
= OPCODE_TEX
;
277 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
278 inst
[ic
].DstReg
.Index
= temp
;
279 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_ZW
; /* write B,A */
280 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
281 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
282 inst
[ic
].SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
,
283 SWIZZLE_Z
, SWIZZLE_W
);
284 inst
[ic
].TexSrcUnit
= 1;
285 inst
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
288 /* MOV colorTemp, temp; */
289 _mesa_init_instructions(inst
+ ic
, 1);
290 inst
[ic
].Opcode
= OPCODE_MOV
;
291 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
292 inst
[ic
].DstReg
.Index
= colorTemp
;
293 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
294 inst
[ic
].SrcReg
[0].Index
= temp
;
297 fp
->Base
.SamplersUsed
|= (1 << 1); /* sampler 1 is used */
300 if (key
->colorMatrix
) {
301 static const gl_state_index row0_state
[STATE_LENGTH
] =
302 { STATE_COLOR_MATRIX
, 0, 0, 0, 0 };
303 static const gl_state_index row1_state
[STATE_LENGTH
] =
304 { STATE_COLOR_MATRIX
, 0, 1, 1, 0 };
305 static const gl_state_index row2_state
[STATE_LENGTH
] =
306 { STATE_COLOR_MATRIX
, 0, 2, 2, 0 };
307 static const gl_state_index row3_state
[STATE_LENGTH
] =
308 { STATE_COLOR_MATRIX
, 0, 3, 3, 0 };
310 GLint row0_p
= _mesa_add_state_reference(params
, row0_state
);
311 GLint row1_p
= _mesa_add_state_reference(params
, row1_state
);
312 GLint row2_p
= _mesa_add_state_reference(params
, row2_state
);
313 GLint row3_p
= _mesa_add_state_reference(params
, row3_state
);
314 const GLuint temp
= 1;
316 /* DP4 temp.x, colorTemp, matrow0; */
317 _mesa_init_instructions(inst
+ ic
, 1);
318 inst
[ic
].Opcode
= OPCODE_DP4
;
319 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
320 inst
[ic
].DstReg
.Index
= temp
;
321 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_X
;
322 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
323 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
324 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
325 inst
[ic
].SrcReg
[1].Index
= row0_p
;
328 /* DP4 temp.y, colorTemp, matrow1; */
329 _mesa_init_instructions(inst
+ ic
, 1);
330 inst
[ic
].Opcode
= OPCODE_DP4
;
331 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
332 inst
[ic
].DstReg
.Index
= temp
;
333 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
334 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
335 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
336 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
337 inst
[ic
].SrcReg
[1].Index
= row1_p
;
340 /* DP4 temp.z, colorTemp, matrow2; */
341 _mesa_init_instructions(inst
+ ic
, 1);
342 inst
[ic
].Opcode
= OPCODE_DP4
;
343 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
344 inst
[ic
].DstReg
.Index
= temp
;
345 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
346 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
347 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
348 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
349 inst
[ic
].SrcReg
[1].Index
= row2_p
;
352 /* DP4 temp.w, colorTemp, matrow3; */
353 _mesa_init_instructions(inst
+ ic
, 1);
354 inst
[ic
].Opcode
= OPCODE_DP4
;
355 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
356 inst
[ic
].DstReg
.Index
= temp
;
357 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_W
;
358 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
359 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
360 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
361 inst
[ic
].SrcReg
[1].Index
= row3_p
;
364 /* MOV colorTemp, temp; */
365 _mesa_init_instructions(inst
+ ic
, 1);
366 inst
[ic
].Opcode
= OPCODE_MOV
;
367 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
368 inst
[ic
].DstReg
.Index
= colorTemp
;
369 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
370 inst
[ic
].SrcReg
[0].Index
= temp
;
374 if (key
->colorMatrixPostScaleBias
) {
375 static const gl_state_index scale_state
[STATE_LENGTH
] =
376 { STATE_INTERNAL
, STATE_PT_SCALE
, 0, 0, 0 };
377 static const gl_state_index bias_state
[STATE_LENGTH
] =
378 { STATE_INTERNAL
, STATE_PT_BIAS
, 0, 0, 0 };
379 GLint scale_param
, bias_param
;
381 scale_param
= _mesa_add_state_reference(params
, scale_state
);
382 bias_param
= _mesa_add_state_reference(params
, bias_state
);
384 _mesa_init_instructions(inst
+ ic
, 1);
385 inst
[ic
].Opcode
= OPCODE_MAD
;
386 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
387 inst
[ic
].DstReg
.Index
= colorTemp
;
388 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
389 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
390 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
391 inst
[ic
].SrcReg
[1].Index
= scale_param
;
392 inst
[ic
].SrcReg
[2].File
= PROGRAM_STATE_VAR
;
393 inst
[ic
].SrcReg
[2].Index
= bias_param
;
397 /* Modify last instruction's dst reg to write to result.color */
399 struct prog_instruction
*last
= &inst
[ic
- 1];
400 last
->DstReg
.File
= PROGRAM_OUTPUT
;
401 last
->DstReg
.Index
= FRAG_RESULT_COLOR
;
405 _mesa_init_instructions(inst
+ ic
, 1);
406 inst
[ic
].Opcode
= OPCODE_END
;
409 assert(ic
<= MAX_INST
);
412 fp
->Base
.Instructions
= _mesa_alloc_instructions(ic
);
413 if (!fp
->Base
.Instructions
) {
414 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
415 "generating pixel transfer program");
419 _mesa_copy_instructions(fp
->Base
.Instructions
, inst
, ic
);
420 fp
->Base
.NumInstructions
= ic
;
421 fp
->Base
.Parameters
= params
;
424 printf("========= pixel transfer prog\n");
425 _mesa_print_program(&fp
->Base
);
426 _mesa_print_parameter_list(fp
->Base
.Parameters
);
435 * Update st->pixel_xfer.program in response to new pixel-transfer state.
438 update_pixel_transfer(struct st_context
*st
)
440 GLcontext
*ctx
= st
->ctx
;
441 struct state_key key
;
442 struct gl_fragment_program
*fp
;
444 make_state_key(st
->ctx
, &key
);
446 fp
= (struct gl_fragment_program
*)
447 _mesa_search_program_cache(st
->pixel_xfer
.cache
, &key
, sizeof(key
));
449 fp
= get_pixel_transfer_program(st
->ctx
, &key
);
450 _mesa_program_cache_insert(st
->ctx
, st
->pixel_xfer
.cache
,
451 &key
, sizeof(key
), &fp
->Base
);
454 if (ctx
->Pixel
.MapColorFlag
) {
455 load_color_map_texture(ctx
, st
->pixel_xfer
.pixelmap_texture
);
457 st
->pixel_xfer
.pixelmap_enabled
= ctx
->Pixel
.MapColorFlag
;
459 st
->pixel_xfer
.program
= (struct st_fragment_program
*) fp
;
464 const struct st_tracked_state st_update_pixel_transfer
= {
465 "st_update_pixel_transfer", /* name */
467 _NEW_PIXEL
| _NEW_COLOR_MATRIX
, /* mesa */
470 update_pixel_transfer
/* update */