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_texture.h"
47 #include "st_inlines.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_resource
*
116 create_color_map_texture(GLcontext
*ctx
)
118 struct st_context
*st
= st_context(ctx
);
119 struct pipe_context
*pipe
= st
->pipe
;
120 struct pipe_resource
*pt
;
121 enum pipe_format format
;
122 const uint texSize
= 256; /* simple, and usually perfect */
124 /* find an RGBA texture format */
125 format
= st_choose_format(pipe
->screen
, GL_RGBA
,
126 PIPE_TEXTURE_2D
, PIPE_BIND_SAMPLER_VIEW
);
128 /* create texture for color map/table */
129 pt
= st_texture_create(st
, PIPE_TEXTURE_2D
, format
, 0,
130 texSize
, texSize
, 1, PIPE_BIND_SAMPLER_VIEW
);
136 * Update the pixelmap texture with the contents of the R/G/B/A pixel maps.
139 load_color_map_texture(GLcontext
*ctx
, struct pipe_resource
*pt
)
141 struct st_context
*st
= st_context(ctx
);
142 struct pipe_context
*pipe
= st
->pipe
;
143 struct pipe_transfer
*transfer
;
144 const GLuint rSize
= ctx
->PixelMaps
.RtoR
.Size
;
145 const GLuint gSize
= ctx
->PixelMaps
.GtoG
.Size
;
146 const GLuint bSize
= ctx
->PixelMaps
.BtoB
.Size
;
147 const GLuint aSize
= ctx
->PixelMaps
.AtoA
.Size
;
148 const uint texSize
= pt
->width0
;
152 transfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
153 pt
, 0, 0, 0, PIPE_TRANSFER_WRITE
,
154 0, 0, texSize
, texSize
);
155 dest
= (uint
*) pipe_transfer_map(pipe
, transfer
);
157 /* Pack four 1D maps into a 2D texture:
158 * R map is placed horizontally, indexed by S, in channel 0
159 * G map is placed vertically, indexed by T, in channel 1
160 * B map is placed horizontally, indexed by S, in channel 2
161 * A map is placed vertically, indexed by T, in channel 3
163 for (i
= 0; i
< texSize
; i
++) {
164 for (j
= 0; j
< texSize
; j
++) {
166 int k
= (i
* texSize
+ j
);
167 ubyte r
= ctx
->PixelMaps
.RtoR
.Map8
[j
* rSize
/ texSize
];
168 ubyte g
= ctx
->PixelMaps
.GtoG
.Map8
[i
* gSize
/ texSize
];
169 ubyte b
= ctx
->PixelMaps
.BtoB
.Map8
[j
* bSize
/ texSize
];
170 ubyte a
= ctx
->PixelMaps
.AtoA
.Map8
[i
* aSize
/ texSize
];
171 util_pack_color_ub(r
, g
, b
, a
, pt
->format
, &uc
);
176 pipe_transfer_unmap(pipe
, transfer
);
177 pipe
->transfer_destroy(pipe
, transfer
);
185 * Returns a fragment program which implements the current pixel transfer ops.
187 static struct gl_fragment_program
*
188 get_pixel_transfer_program(GLcontext
*ctx
, const struct state_key
*key
)
190 struct st_context
*st
= st_context(ctx
);
191 struct prog_instruction inst
[MAX_INST
];
192 struct gl_program_parameter_list
*params
;
193 struct gl_fragment_program
*fp
;
195 const GLuint colorTemp
= 0;
197 fp
= (struct gl_fragment_program
*)
198 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
202 params
= _mesa_new_parameter_list();
205 * Get initial pixel color from the texture.
206 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
208 _mesa_init_instructions(inst
+ ic
, 1);
209 inst
[ic
].Opcode
= OPCODE_TEX
;
210 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
211 inst
[ic
].DstReg
.Index
= colorTemp
;
212 inst
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
213 inst
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
214 inst
[ic
].TexSrcUnit
= 0;
215 inst
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
217 fp
->Base
.InputsRead
= (1 << FRAG_ATTRIB_TEX0
);
218 fp
->Base
.OutputsWritten
= (1 << FRAG_RESULT_COLOR
);
219 fp
->Base
.SamplersUsed
= 0x1; /* sampler 0 (bit 0) is used */
221 if (key
->scaleAndBias
) {
222 static const gl_state_index scale_state
[STATE_LENGTH
] =
223 { STATE_INTERNAL
, STATE_PT_SCALE
, 0, 0, 0 };
224 static const gl_state_index bias_state
[STATE_LENGTH
] =
225 { STATE_INTERNAL
, STATE_PT_BIAS
, 0, 0, 0 };
226 GLfloat scale
[4], bias
[4];
227 GLint scale_p
, bias_p
;
229 scale
[0] = ctx
->Pixel
.RedScale
;
230 scale
[1] = ctx
->Pixel
.GreenScale
;
231 scale
[2] = ctx
->Pixel
.BlueScale
;
232 scale
[3] = ctx
->Pixel
.AlphaScale
;
233 bias
[0] = ctx
->Pixel
.RedBias
;
234 bias
[1] = ctx
->Pixel
.GreenBias
;
235 bias
[2] = ctx
->Pixel
.BlueBias
;
236 bias
[3] = ctx
->Pixel
.AlphaBias
;
238 scale_p
= _mesa_add_state_reference(params
, scale_state
);
239 bias_p
= _mesa_add_state_reference(params
, bias_state
);
241 /* MAD colorTemp, colorTemp, scale, bias; */
242 _mesa_init_instructions(inst
+ ic
, 1);
243 inst
[ic
].Opcode
= OPCODE_MAD
;
244 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
245 inst
[ic
].DstReg
.Index
= colorTemp
;
246 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
247 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
248 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
249 inst
[ic
].SrcReg
[1].Index
= scale_p
;
250 inst
[ic
].SrcReg
[2].File
= PROGRAM_STATE_VAR
;
251 inst
[ic
].SrcReg
[2].Index
= bias_p
;
255 if (key
->pixelMaps
) {
256 const GLuint temp
= 1;
258 /* create the colormap/texture now if not already done */
259 if (!st
->pixel_xfer
.pixelmap_texture
) {
260 st
->pixel_xfer
.pixelmap_texture
= create_color_map_texture(ctx
);
261 st
->pixel_xfer
.pixelmap_sampler_view
=
262 st_create_texture_sampler_view(st
->pipe
,
263 st
->pixel_xfer
.pixelmap_texture
);
266 /* with a little effort, we can do four pixel map look-ups with
267 * two TEX instructions:
270 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
271 _mesa_init_instructions(inst
+ ic
, 1);
272 inst
[ic
].Opcode
= OPCODE_TEX
;
273 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
274 inst
[ic
].DstReg
.Index
= temp
;
275 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_XY
; /* write R,G */
276 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
277 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
278 inst
[ic
].TexSrcUnit
= 1;
279 inst
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
282 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
283 _mesa_init_instructions(inst
+ ic
, 1);
284 inst
[ic
].Opcode
= OPCODE_TEX
;
285 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
286 inst
[ic
].DstReg
.Index
= temp
;
287 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_ZW
; /* write B,A */
288 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
289 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
290 inst
[ic
].SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(SWIZZLE_Z
, SWIZZLE_W
,
291 SWIZZLE_Z
, SWIZZLE_W
);
292 inst
[ic
].TexSrcUnit
= 1;
293 inst
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
296 /* MOV colorTemp, temp; */
297 _mesa_init_instructions(inst
+ ic
, 1);
298 inst
[ic
].Opcode
= OPCODE_MOV
;
299 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
300 inst
[ic
].DstReg
.Index
= colorTemp
;
301 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
302 inst
[ic
].SrcReg
[0].Index
= temp
;
305 fp
->Base
.SamplersUsed
|= (1 << 1); /* sampler 1 is used */
308 if (key
->colorMatrix
) {
309 static const gl_state_index row0_state
[STATE_LENGTH
] =
310 { STATE_COLOR_MATRIX
, 0, 0, 0, 0 };
311 static const gl_state_index row1_state
[STATE_LENGTH
] =
312 { STATE_COLOR_MATRIX
, 0, 1, 1, 0 };
313 static const gl_state_index row2_state
[STATE_LENGTH
] =
314 { STATE_COLOR_MATRIX
, 0, 2, 2, 0 };
315 static const gl_state_index row3_state
[STATE_LENGTH
] =
316 { STATE_COLOR_MATRIX
, 0, 3, 3, 0 };
318 GLint row0_p
= _mesa_add_state_reference(params
, row0_state
);
319 GLint row1_p
= _mesa_add_state_reference(params
, row1_state
);
320 GLint row2_p
= _mesa_add_state_reference(params
, row2_state
);
321 GLint row3_p
= _mesa_add_state_reference(params
, row3_state
);
322 const GLuint temp
= 1;
324 /* DP4 temp.x, colorTemp, matrow0; */
325 _mesa_init_instructions(inst
+ ic
, 1);
326 inst
[ic
].Opcode
= OPCODE_DP4
;
327 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
328 inst
[ic
].DstReg
.Index
= temp
;
329 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_X
;
330 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
331 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
332 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
333 inst
[ic
].SrcReg
[1].Index
= row0_p
;
336 /* DP4 temp.y, colorTemp, matrow1; */
337 _mesa_init_instructions(inst
+ ic
, 1);
338 inst
[ic
].Opcode
= OPCODE_DP4
;
339 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
340 inst
[ic
].DstReg
.Index
= temp
;
341 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
342 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
343 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
344 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
345 inst
[ic
].SrcReg
[1].Index
= row1_p
;
348 /* DP4 temp.z, colorTemp, matrow2; */
349 _mesa_init_instructions(inst
+ ic
, 1);
350 inst
[ic
].Opcode
= OPCODE_DP4
;
351 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
352 inst
[ic
].DstReg
.Index
= temp
;
353 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
354 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
355 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
356 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
357 inst
[ic
].SrcReg
[1].Index
= row2_p
;
360 /* DP4 temp.w, colorTemp, matrow3; */
361 _mesa_init_instructions(inst
+ ic
, 1);
362 inst
[ic
].Opcode
= OPCODE_DP4
;
363 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
364 inst
[ic
].DstReg
.Index
= temp
;
365 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_W
;
366 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
367 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
368 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
369 inst
[ic
].SrcReg
[1].Index
= row3_p
;
372 /* MOV colorTemp, temp; */
373 _mesa_init_instructions(inst
+ ic
, 1);
374 inst
[ic
].Opcode
= OPCODE_MOV
;
375 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
376 inst
[ic
].DstReg
.Index
= colorTemp
;
377 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
378 inst
[ic
].SrcReg
[0].Index
= temp
;
382 if (key
->colorMatrixPostScaleBias
) {
383 static const gl_state_index scale_state
[STATE_LENGTH
] =
384 { STATE_INTERNAL
, STATE_PT_SCALE
, 0, 0, 0 };
385 static const gl_state_index bias_state
[STATE_LENGTH
] =
386 { STATE_INTERNAL
, STATE_PT_BIAS
, 0, 0, 0 };
387 GLint scale_param
, bias_param
;
389 scale_param
= _mesa_add_state_reference(params
, scale_state
);
390 bias_param
= _mesa_add_state_reference(params
, bias_state
);
392 _mesa_init_instructions(inst
+ ic
, 1);
393 inst
[ic
].Opcode
= OPCODE_MAD
;
394 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
395 inst
[ic
].DstReg
.Index
= colorTemp
;
396 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
397 inst
[ic
].SrcReg
[0].Index
= colorTemp
;
398 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
399 inst
[ic
].SrcReg
[1].Index
= scale_param
;
400 inst
[ic
].SrcReg
[2].File
= PROGRAM_STATE_VAR
;
401 inst
[ic
].SrcReg
[2].Index
= bias_param
;
405 /* Modify last instruction's dst reg to write to result.color */
407 struct prog_instruction
*last
= &inst
[ic
- 1];
408 last
->DstReg
.File
= PROGRAM_OUTPUT
;
409 last
->DstReg
.Index
= FRAG_RESULT_COLOR
;
413 _mesa_init_instructions(inst
+ ic
, 1);
414 inst
[ic
].Opcode
= OPCODE_END
;
417 assert(ic
<= MAX_INST
);
420 fp
->Base
.Instructions
= _mesa_alloc_instructions(ic
);
421 if (!fp
->Base
.Instructions
) {
422 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
423 "generating pixel transfer program");
427 _mesa_copy_instructions(fp
->Base
.Instructions
, inst
, ic
);
428 fp
->Base
.NumInstructions
= ic
;
429 fp
->Base
.Parameters
= params
;
432 printf("========= pixel transfer prog\n");
433 _mesa_print_program(&fp
->Base
);
434 _mesa_print_parameter_list(fp
->Base
.Parameters
);
443 * Update st->pixel_xfer.program in response to new pixel-transfer state.
446 update_pixel_transfer(struct st_context
*st
)
448 GLcontext
*ctx
= st
->ctx
;
449 struct state_key key
;
450 struct gl_fragment_program
*fp
;
452 make_state_key(st
->ctx
, &key
);
454 fp
= (struct gl_fragment_program
*)
455 _mesa_search_program_cache(st
->pixel_xfer
.cache
, &key
, sizeof(key
));
457 fp
= get_pixel_transfer_program(st
->ctx
, &key
);
458 _mesa_program_cache_insert(st
->ctx
, st
->pixel_xfer
.cache
,
459 &key
, sizeof(key
), &fp
->Base
);
462 if (ctx
->Pixel
.MapColorFlag
) {
463 load_color_map_texture(ctx
, st
->pixel_xfer
.pixelmap_texture
);
465 st
->pixel_xfer
.pixelmap_enabled
= ctx
->Pixel
.MapColorFlag
;
467 st
->pixel_xfer
.program
= (struct st_fragment_program
*) fp
;
472 const struct st_tracked_state st_update_pixel_transfer
= {
473 "st_update_pixel_transfer", /* name */
475 _NEW_PIXEL
| _NEW_COLOR_MATRIX
, /* mesa */
478 update_pixel_transfer
/* update */