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_program.h"
51 GLuint scaleAndBias
:1;
55 GLfloat Maps
[3][256][4];
58 pipeline_stage Stages
[STAGE_MAX
];
59 GLboolean StagesUsed
[STAGE_MAX
];
60 GLfloat Scale1
[4], Bias1
[4];
61 GLfloat Scale2
[4], Bias2
[4];
67 is_identity(const GLfloat m
[16])
70 for (i
= 0; i
< 16; i
++) {
71 const int row
= i
% 4, col
= i
/ 4;
72 const float val
= (row
== col
);
81 make_state_key(GLcontext
*ctx
, struct state_key
*key
)
83 memset(key
, 0, sizeof(*key
));
85 if (ctx
->Pixel
.RedBias
!= 0.0 || ctx
->Pixel
.RedScale
!= 1.0 ||
86 ctx
->Pixel
.GreenBias
!= 0.0 || ctx
->Pixel
.GreenScale
!= 1.0 ||
87 ctx
->Pixel
.BlueBias
!= 0.0 || ctx
->Pixel
.BlueScale
!= 1.0 ||
88 ctx
->Pixel
.AlphaBias
!= 0.0 || ctx
->Pixel
.AlphaScale
!= 1.0) {
89 key
->scaleAndBias
= 1;
92 if (!is_identity(ctx
->ColorMatrixStack
.Top
->m
)) {
103 * Returns a fragment program which implements the current pixel transfer ops.
105 static struct gl_fragment_program
*
106 get_pixel_transfer_program(GLcontext
*ctx
, const struct state_key
*key
)
108 struct prog_instruction inst
[MAX_INST
];
109 struct gl_program_parameter_list
*params
;
110 struct gl_fragment_program
*fp
;
113 fp
= (struct gl_fragment_program
*)
114 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
118 params
= _mesa_new_parameter_list();
120 /* TEX result.color, fragment.texcoord[0], texture[0], 2D; */
121 _mesa_init_instructions(inst
+ ic
, 1);
122 inst
[ic
].Opcode
= OPCODE_TEX
;
123 inst
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
124 inst
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
125 inst
[ic
].SrcReg
[0].File
= PROGRAM_INPUT
;
126 inst
[ic
].SrcReg
[0].Index
= FRAG_ATTRIB_TEX0
;
127 inst
[ic
].TexSrcUnit
= 0;
128 inst
[ic
].TexSrcTarget
= TEXTURE_2D_INDEX
;
130 fp
->Base
.InputsRead
= (1 << FRAG_ATTRIB_TEX0
);
131 fp
->Base
.OutputsWritten
= (1 << FRAG_RESULT_COLR
);
133 /* MAD result.color, result.color, scale, bias; */
134 if (key
->scaleAndBias
) {
135 static const gl_state_index scale_state
[STATE_LENGTH
] =
136 { STATE_INTERNAL
, STATE_PT_SCALE
, 0, 0, 0 };
137 static const gl_state_index bias_state
[STATE_LENGTH
] =
138 { STATE_INTERNAL
, STATE_PT_BIAS
, 0, 0, 0 };
139 GLfloat scale
[4], bias
[4];
140 GLint scale_p
, bias_p
;
142 scale
[0] = ctx
->Pixel
.RedScale
;
143 scale
[1] = ctx
->Pixel
.GreenScale
;
144 scale
[2] = ctx
->Pixel
.BlueScale
;
145 scale
[3] = ctx
->Pixel
.AlphaScale
;
146 bias
[0] = ctx
->Pixel
.RedBias
;
147 bias
[1] = ctx
->Pixel
.GreenBias
;
148 bias
[2] = ctx
->Pixel
.BlueBias
;
149 bias
[3] = ctx
->Pixel
.AlphaBias
;
151 scale_p
= _mesa_add_state_reference(params
, scale_state
);
152 bias_p
= _mesa_add_state_reference(params
, bias_state
);
154 _mesa_init_instructions(inst
+ ic
, 1);
155 inst
[ic
].Opcode
= OPCODE_MAD
;
156 inst
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
157 inst
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
158 inst
[ic
].SrcReg
[0].File
= PROGRAM_OUTPUT
;
159 inst
[ic
].SrcReg
[0].Index
= FRAG_RESULT_COLR
;
160 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
161 inst
[ic
].SrcReg
[1].Index
= scale_p
;
162 inst
[ic
].SrcReg
[2].File
= PROGRAM_STATE_VAR
;
163 inst
[ic
].SrcReg
[2].Index
= bias_p
;
167 if (key
->colorMatrix
) {
168 static const gl_state_index row0_state
[STATE_LENGTH
] =
169 { STATE_COLOR_MATRIX
, 0, 0, 0, 0 };
170 static const gl_state_index row1_state
[STATE_LENGTH
] =
171 { STATE_COLOR_MATRIX
, 0, 1, 1, 0 };
172 static const gl_state_index row2_state
[STATE_LENGTH
] =
173 { STATE_COLOR_MATRIX
, 0, 2, 2, 0 };
174 static const gl_state_index row3_state
[STATE_LENGTH
] =
175 { STATE_COLOR_MATRIX
, 0, 3, 3, 0 };
177 GLint row0_p
= _mesa_add_state_reference(params
, row0_state
);
178 GLint row1_p
= _mesa_add_state_reference(params
, row1_state
);
179 GLint row2_p
= _mesa_add_state_reference(params
, row2_state
);
180 GLint row3_p
= _mesa_add_state_reference(params
, row3_state
);
182 /* MOV temp0, result.color; */
183 _mesa_init_instructions(inst
+ ic
, 1);
184 inst
[ic
].Opcode
= OPCODE_MOV
;
185 inst
[ic
].DstReg
.File
= PROGRAM_TEMPORARY
;
186 inst
[ic
].DstReg
.Index
= 0;
187 inst
[ic
].SrcReg
[0].File
= PROGRAM_OUTPUT
;
188 inst
[ic
].SrcReg
[0].Index
= FRAG_RESULT_COLR
;
191 /* XXX reimplement in terms of MUL/MAD (see t_vp_build.c) */
193 /* DP4 result.color.x, tmp0, matrow0; */
194 _mesa_init_instructions(inst
+ ic
, 1);
195 inst
[ic
].Opcode
= OPCODE_DP4
;
196 inst
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
197 inst
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
198 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_X
;
199 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
200 inst
[ic
].SrcReg
[0].Index
= 0;
201 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
202 inst
[ic
].SrcReg
[1].Index
= row0_p
;
205 /* DP4 result.color.y, tmp0, matrow1; */
206 _mesa_init_instructions(inst
+ ic
, 1);
207 inst
[ic
].Opcode
= OPCODE_DP4
;
208 inst
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
209 inst
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
210 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_Y
;
211 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
212 inst
[ic
].SrcReg
[0].Index
= 0;
213 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
214 inst
[ic
].SrcReg
[1].Index
= row1_p
;
217 /* DP4 result.color.z, tmp0, matrow2; */
218 _mesa_init_instructions(inst
+ ic
, 1);
219 inst
[ic
].Opcode
= OPCODE_DP4
;
220 inst
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
221 inst
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
222 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_Z
;
223 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
224 inst
[ic
].SrcReg
[0].Index
= 0;
225 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
226 inst
[ic
].SrcReg
[1].Index
= row2_p
;
229 /* DP4 result.color.w, tmp0, matrow3; */
230 _mesa_init_instructions(inst
+ ic
, 1);
231 inst
[ic
].Opcode
= OPCODE_DP4
;
232 inst
[ic
].DstReg
.File
= PROGRAM_OUTPUT
;
233 inst
[ic
].DstReg
.Index
= FRAG_RESULT_COLR
;
234 inst
[ic
].DstReg
.WriteMask
= WRITEMASK_W
;
235 inst
[ic
].SrcReg
[0].File
= PROGRAM_TEMPORARY
;
236 inst
[ic
].SrcReg
[0].Index
= 0;
237 inst
[ic
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
238 inst
[ic
].SrcReg
[1].Index
= row3_p
;
243 _mesa_init_instructions(inst
+ ic
, 1);
244 inst
[ic
].Opcode
= OPCODE_END
;
247 assert(ic
<= MAX_INST
);
250 fp
->Base
.Instructions
= _mesa_alloc_instructions(ic
);
251 if (!fp
->Base
.Instructions
) {
252 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
253 "generating pixel transfer program");
257 _mesa_copy_instructions(fp
->Base
.Instructions
, inst
, ic
);
258 fp
->Base
.NumInstructions
= ic
;
259 fp
->Base
.Parameters
= params
;
262 printf("========= pixel transfer prog\n");
263 _mesa_print_program(&fp
->Base
);
264 _mesa_print_parameter_list(fp
->Base
.Parameters
);
273 * Update st->pixel_xfer.program in response to new pixel-transfer state.
276 update_pixel_transfer(struct st_context
*st
)
278 struct state_key key
;
279 struct gl_fragment_program
*fp
;
281 make_state_key(st
->ctx
, &key
);
283 fp
= (struct gl_fragment_program
*)
284 _mesa_search_program_cache(st
->pixel_xfer
.cache
, &key
, sizeof(key
));
286 fp
= get_pixel_transfer_program(st
->ctx
, &key
);
287 _mesa_program_cache_insert(st
->ctx
, st
->pixel_xfer
.cache
,
288 &key
, sizeof(key
), &fp
->Base
);
291 st
->pixel_xfer
.program
= (struct st_fragment_program
*) fp
;
296 const struct st_tracked_state st_update_pixel_transfer
= {
297 .name
= "st_update_pixel_transfer",
299 .mesa
= _NEW_PIXEL
| _NEW_COLOR_MATRIX
,
302 .update
= update_pixel_transfer