color matrix
[mesa.git] / src / mesa / state_tracker / st_atom_pixeltransfer.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /*
29 * Generate fragment programs to implement pixel transfer ops, such as
30 * scale/bias, colormatrix, colortable, convolution...
31 *
32 * Authors:
33 * Brian Paul
34 */
35
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"
43
44 #include "st_context.h"
45
46
47
48 struct state_key
49 {
50 GLuint scaleAndBias:1;
51 GLuint colorMatrix:1;
52
53 #if 0
54 GLfloat Maps[3][256][4];
55 int NumMaps;
56 GLint NumStages;
57 pipeline_stage Stages[STAGE_MAX];
58 GLboolean StagesUsed[STAGE_MAX];
59 GLfloat Scale1[4], Bias1[4];
60 GLfloat Scale2[4], Bias2[4];
61 #endif
62 };
63
64
65 static GLboolean
66 is_identity(const GLfloat m[16])
67 {
68 GLuint i;
69 for (i = 0; i < 16; i++) {
70 const int row = i % 4, col = i / 4;
71 const float val = (row == col);
72 if (m[i] != val)
73 return GL_FALSE;
74 }
75 return GL_TRUE;
76 }
77
78
79 static void
80 make_state_key(GLcontext *ctx, struct state_key *key)
81 {
82 /*GLuint i, j;*/
83
84 memset(key, 0, sizeof(*key));
85
86 if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 ||
87 ctx->Pixel.GreenBias != 0.0 || ctx->Pixel.GreenScale != 1.0 ||
88 ctx->Pixel.BlueBias != 0.0 || ctx->Pixel.BlueScale != 1.0 ||
89 ctx->Pixel.AlphaBias != 0.0 || ctx->Pixel.AlphaScale != 1.0) {
90 key->scaleAndBias = 1;
91 }
92
93 if (!is_identity(ctx->ColorMatrixStack.Top->m)) {
94 key->colorMatrix = 1;
95 }
96 }
97
98
99
100
101 #define MAX_INST 100
102
103 /**
104 * Returns a fragment program which implements the current pixel transfer ops.
105 */
106 static struct gl_fragment_program *
107 get_pixel_transfer_program(GLcontext *ctx, const struct state_key *key)
108 {
109 struct prog_instruction inst[MAX_INST];
110 struct gl_program_parameter_list *params;
111 struct gl_fragment_program *fp;
112 GLuint ic = 0;
113
114 fp = (struct gl_fragment_program *)
115 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
116 if (!fp)
117 return NULL;
118
119 params = _mesa_new_parameter_list();
120
121 /* TEX result.color, fragment.texcoord[0], texture[0], 2D; */
122 _mesa_init_instructions(inst + ic, 1);
123 inst[ic].Opcode = OPCODE_TEX;
124 inst[ic].DstReg.File = PROGRAM_OUTPUT;
125 inst[ic].DstReg.Index = FRAG_RESULT_COLR;
126 inst[ic].SrcReg[0].File = PROGRAM_INPUT;
127 inst[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
128 inst[ic].TexSrcUnit = 0;
129 inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
130 ic++;
131 fp->Base.InputsRead = (1 << FRAG_ATTRIB_TEX0);
132 fp->Base.OutputsWritten = (1 << FRAG_RESULT_COLR);
133
134 /* MAD result.color, result.color, scale, bias; */
135 if (key->scaleAndBias) {
136 static const gl_state_index scale_state[STATE_LENGTH] =
137 { STATE_INTERNAL, STATE_PT_SCALE, 0, 0, 0 };
138 static const gl_state_index bias_state[STATE_LENGTH] =
139 { STATE_INTERNAL, STATE_PT_BIAS, 0, 0, 0 };
140 GLfloat scale[4], bias[4];
141 GLint scale_p, bias_p;
142
143 scale[0] = ctx->Pixel.RedScale;
144 scale[1] = ctx->Pixel.GreenScale;
145 scale[2] = ctx->Pixel.BlueScale;
146 scale[3] = ctx->Pixel.AlphaScale;
147 bias[0] = ctx->Pixel.RedBias;
148 bias[1] = ctx->Pixel.GreenBias;
149 bias[2] = ctx->Pixel.BlueBias;
150 bias[3] = ctx->Pixel.AlphaBias;
151
152 scale_p = _mesa_add_state_reference(params, scale_state);
153 bias_p = _mesa_add_state_reference(params, bias_state);
154
155 _mesa_init_instructions(inst + ic, 1);
156 inst[ic].Opcode = OPCODE_MAD;
157 inst[ic].DstReg.File = PROGRAM_OUTPUT;
158 inst[ic].DstReg.Index = FRAG_RESULT_COLR;
159 inst[ic].SrcReg[0].File = PROGRAM_OUTPUT;
160 inst[ic].SrcReg[0].Index = FRAG_RESULT_COLR;
161 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
162 inst[ic].SrcReg[1].Index = scale_p;
163 inst[ic].SrcReg[2].File = PROGRAM_STATE_VAR;
164 inst[ic].SrcReg[2].Index = bias_p;
165 ic++;
166 }
167
168 if (key->colorMatrix) {
169 static const gl_state_index row0_state[STATE_LENGTH] =
170 { STATE_COLOR_MATRIX, 0, 0, 0, 0 };
171 static const gl_state_index row1_state[STATE_LENGTH] =
172 { STATE_COLOR_MATRIX, 0, 1, 1, 0 };
173 static const gl_state_index row2_state[STATE_LENGTH] =
174 { STATE_COLOR_MATRIX, 0, 2, 2, 0 };
175 static const gl_state_index row3_state[STATE_LENGTH] =
176 { STATE_COLOR_MATRIX, 0, 3, 3, 0 };
177
178 GLint row0_p = _mesa_add_state_reference(params, row0_state);
179 GLint row1_p = _mesa_add_state_reference(params, row1_state);
180 GLint row2_p = _mesa_add_state_reference(params, row2_state);
181 GLint row3_p = _mesa_add_state_reference(params, row3_state);
182
183 /* MOV temp0, result.color; */
184 _mesa_init_instructions(inst + ic, 1);
185 inst[ic].Opcode = OPCODE_MOV;
186 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
187 inst[ic].DstReg.Index = 0;
188 inst[ic].SrcReg[0].File = PROGRAM_OUTPUT;
189 inst[ic].SrcReg[0].Index = FRAG_RESULT_COLR;
190 ic++;
191
192 /* DP4 result.color.x, tmp0, matrow0; */
193 _mesa_init_instructions(inst + ic, 1);
194 inst[ic].Opcode = OPCODE_DP4;
195 inst[ic].DstReg.File = PROGRAM_OUTPUT;
196 inst[ic].DstReg.Index = FRAG_RESULT_COLR;
197 inst[ic].DstReg.WriteMask = WRITEMASK_X;
198 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
199 inst[ic].SrcReg[0].Index = 0;
200 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
201 inst[ic].SrcReg[1].Index = row0_p;
202 ic++;
203
204 /* DP4 result.color.y, tmp0, matrow1; */
205 _mesa_init_instructions(inst + ic, 1);
206 inst[ic].Opcode = OPCODE_DP4;
207 inst[ic].DstReg.File = PROGRAM_OUTPUT;
208 inst[ic].DstReg.Index = FRAG_RESULT_COLR;
209 inst[ic].DstReg.WriteMask = WRITEMASK_Y;
210 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
211 inst[ic].SrcReg[0].Index = 0;
212 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
213 inst[ic].SrcReg[1].Index = row1_p;
214 ic++;
215
216 /* DP4 result.color.z, tmp0, matrow2; */
217 _mesa_init_instructions(inst + ic, 1);
218 inst[ic].Opcode = OPCODE_DP4;
219 inst[ic].DstReg.File = PROGRAM_OUTPUT;
220 inst[ic].DstReg.Index = FRAG_RESULT_COLR;
221 inst[ic].DstReg.WriteMask = WRITEMASK_Z;
222 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
223 inst[ic].SrcReg[0].Index = 0;
224 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
225 inst[ic].SrcReg[1].Index = row2_p;
226 ic++;
227
228 /* DP4 result.color.w, tmp0, matrow3; */
229 _mesa_init_instructions(inst + ic, 1);
230 inst[ic].Opcode = OPCODE_DP4;
231 inst[ic].DstReg.File = PROGRAM_OUTPUT;
232 inst[ic].DstReg.Index = FRAG_RESULT_COLR;
233 inst[ic].DstReg.WriteMask = WRITEMASK_W;
234 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
235 inst[ic].SrcReg[0].Index = 0;
236 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
237 inst[ic].SrcReg[1].Index = row3_p;
238 ic++;
239 }
240
241 /* END; */
242 _mesa_init_instructions(inst + ic, 1);
243 inst[ic].Opcode = OPCODE_END;
244 ic++;
245
246 assert(ic <= MAX_INST);
247
248
249 fp->Base.Instructions = _mesa_alloc_instructions(ic);
250 if (!fp->Base.Instructions) {
251 _mesa_error(ctx, GL_OUT_OF_MEMORY,
252 "generating pixel transfer program");
253 return NULL;
254 }
255
256 _mesa_copy_instructions(fp->Base.Instructions, inst, ic);
257 fp->Base.NumInstructions = ic;
258 fp->Base.Parameters = params;
259
260 #if 0
261 printf("========= pixel transfer prog\n");
262 _mesa_print_program(&fp->Base);
263 _mesa_print_parameter_list(fp->Base.Parameters);
264 #endif
265
266 return fp;
267 }
268
269
270
271 static void
272 update_pixel_transfer(struct st_context *st)
273 {
274 struct state_key key;
275 struct gl_fragment_program *fp;
276
277 make_state_key(st->ctx, &key);
278
279 fp = (struct gl_fragment_program *)
280 _mesa_search_program_cache(st->pixel_transfer_cache, &key, sizeof(key));
281 if (!fp) {
282 fp = get_pixel_transfer_program(st->ctx, &key);
283 _mesa_program_cache_insert(st->ctx, st->pixel_transfer_cache,
284 &key, sizeof(key), &fp->Base);
285 }
286
287 st->pixel_transfer_program = fp;
288 }
289
290
291
292 const struct st_tracked_state st_update_pixel_transfer = {
293 .name = "st_update_pixel_transfer",
294 .dirty = {
295 .mesa = _NEW_PIXEL | _NEW_COLOR_MATRIX,
296 .st = 0,
297 },
298 .update = update_pixel_transfer
299 };