Merge remote branch 'origin/master' into nv50-compiler
[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 "program/program.h"
40 #include "program/prog_cache.h"
41 #include "program/prog_instruction.h"
42 #include "program/prog_parameter.h"
43 #include "program/prog_print.h"
44
45 #include "st_context.h"
46 #include "st_format.h"
47 #include "st_texture.h"
48
49 #include "pipe/p_screen.h"
50 #include "pipe/p_context.h"
51 #include "util/u_inlines.h"
52 #include "util/u_pack_color.h"
53
54
55 struct state_key
56 {
57 GLuint scaleAndBias:1;
58 GLuint colorMatrix:1;
59 GLuint colorMatrixPostScaleBias:1;
60 GLuint pixelMaps:1;
61
62 #if 0
63 GLfloat Maps[3][256][4];
64 int NumMaps;
65 GLint NumStages;
66 pipeline_stage Stages[STAGE_MAX];
67 GLboolean StagesUsed[STAGE_MAX];
68 GLfloat Scale1[4], Bias1[4];
69 GLfloat Scale2[4], Bias2[4];
70 #endif
71 };
72
73
74 static GLboolean
75 is_identity(const GLfloat m[16])
76 {
77 GLuint i;
78 for (i = 0; i < 16; i++) {
79 const int row = i % 4, col = i / 4;
80 const float val = (GLfloat)(row == col);
81 if (m[i] != val)
82 return GL_FALSE;
83 }
84 return GL_TRUE;
85 }
86
87
88 static void
89 make_state_key(GLcontext *ctx, struct state_key *key)
90 {
91 static const GLfloat zero[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
92 static const GLfloat one[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
93
94 memset(key, 0, sizeof(*key));
95
96 if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 ||
97 ctx->Pixel.GreenBias != 0.0 || ctx->Pixel.GreenScale != 1.0 ||
98 ctx->Pixel.BlueBias != 0.0 || ctx->Pixel.BlueScale != 1.0 ||
99 ctx->Pixel.AlphaBias != 0.0 || ctx->Pixel.AlphaScale != 1.0) {
100 key->scaleAndBias = 1;
101 }
102
103 if (!is_identity(ctx->ColorMatrixStack.Top->m)) {
104 key->colorMatrix = 1;
105 }
106
107 if (!TEST_EQ_4V(ctx->Pixel.PostColorMatrixScale, one) ||
108 !TEST_EQ_4V(ctx->Pixel.PostColorMatrixBias, zero)) {
109 key->colorMatrixPostScaleBias = 1;
110 }
111
112 key->pixelMaps = ctx->Pixel.MapColorFlag;
113 }
114
115
116 static struct pipe_resource *
117 create_color_map_texture(GLcontext *ctx)
118 {
119 struct st_context *st = st_context(ctx);
120 struct pipe_context *pipe = st->pipe;
121 struct pipe_resource *pt;
122 enum pipe_format format;
123 const uint texSize = 256; /* simple, and usually perfect */
124
125 /* find an RGBA texture format */
126 format = st_choose_format(pipe->screen, GL_RGBA,
127 PIPE_TEXTURE_2D, 0, PIPE_BIND_SAMPLER_VIEW);
128
129 /* create texture for color map/table */
130 pt = st_texture_create(st, PIPE_TEXTURE_2D, format, 0,
131 texSize, texSize, 1, PIPE_BIND_SAMPLER_VIEW);
132 return pt;
133 }
134
135
136 /**
137 * Update the pixelmap texture with the contents of the R/G/B/A pixel maps.
138 */
139 static void
140 load_color_map_texture(GLcontext *ctx, struct pipe_resource *pt)
141 {
142 struct st_context *st = st_context(ctx);
143 struct pipe_context *pipe = st->pipe;
144 struct pipe_transfer *transfer;
145 const GLuint rSize = ctx->PixelMaps.RtoR.Size;
146 const GLuint gSize = ctx->PixelMaps.GtoG.Size;
147 const GLuint bSize = ctx->PixelMaps.BtoB.Size;
148 const GLuint aSize = ctx->PixelMaps.AtoA.Size;
149 const uint texSize = pt->width0;
150 uint *dest;
151 uint i, j;
152
153 transfer = pipe_get_transfer(st_context(ctx)->pipe,
154 pt, 0, 0, 0, PIPE_TRANSFER_WRITE,
155 0, 0, texSize, texSize);
156 dest = (uint *) pipe_transfer_map(pipe, transfer);
157
158 /* Pack four 1D maps into a 2D texture:
159 * R map is placed horizontally, indexed by S, in channel 0
160 * G map is placed vertically, indexed by T, in channel 1
161 * B map is placed horizontally, indexed by S, in channel 2
162 * A map is placed vertically, indexed by T, in channel 3
163 */
164 for (i = 0; i < texSize; i++) {
165 for (j = 0; j < texSize; j++) {
166 union util_color uc;
167 int k = (i * texSize + j);
168 ubyte r = ctx->PixelMaps.RtoR.Map8[j * rSize / texSize];
169 ubyte g = ctx->PixelMaps.GtoG.Map8[i * gSize / texSize];
170 ubyte b = ctx->PixelMaps.BtoB.Map8[j * bSize / texSize];
171 ubyte a = ctx->PixelMaps.AtoA.Map8[i * aSize / texSize];
172 util_pack_color_ub(r, g, b, a, pt->format, &uc);
173 *(dest + k) = uc.ui;
174 }
175 }
176
177 pipe_transfer_unmap(pipe, transfer);
178 pipe->transfer_destroy(pipe, transfer);
179 }
180
181
182
183 #define MAX_INST 100
184
185 /**
186 * Returns a fragment program which implements the current pixel transfer ops.
187 */
188 static struct gl_fragment_program *
189 get_pixel_transfer_program(GLcontext *ctx, const struct state_key *key)
190 {
191 struct st_context *st = st_context(ctx);
192 struct prog_instruction inst[MAX_INST];
193 struct gl_program_parameter_list *params;
194 struct gl_fragment_program *fp;
195 GLuint ic = 0;
196 const GLuint colorTemp = 0;
197
198 fp = (struct gl_fragment_program *)
199 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
200 if (!fp)
201 return NULL;
202
203 params = _mesa_new_parameter_list();
204
205 /*
206 * Get initial pixel color from the texture.
207 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
208 */
209 _mesa_init_instructions(inst + ic, 1);
210 inst[ic].Opcode = OPCODE_TEX;
211 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
212 inst[ic].DstReg.Index = colorTemp;
213 inst[ic].SrcReg[0].File = PROGRAM_INPUT;
214 inst[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
215 inst[ic].TexSrcUnit = 0;
216 inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
217 ic++;
218 fp->Base.InputsRead = (1 << FRAG_ATTRIB_TEX0);
219 fp->Base.OutputsWritten = (1 << FRAG_RESULT_COLOR);
220 fp->Base.SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
221
222 if (key->scaleAndBias) {
223 static const gl_state_index scale_state[STATE_LENGTH] =
224 { STATE_INTERNAL, STATE_PT_SCALE, 0, 0, 0 };
225 static const gl_state_index bias_state[STATE_LENGTH] =
226 { STATE_INTERNAL, STATE_PT_BIAS, 0, 0, 0 };
227 GLfloat scale[4], bias[4];
228 GLint scale_p, bias_p;
229
230 scale[0] = ctx->Pixel.RedScale;
231 scale[1] = ctx->Pixel.GreenScale;
232 scale[2] = ctx->Pixel.BlueScale;
233 scale[3] = ctx->Pixel.AlphaScale;
234 bias[0] = ctx->Pixel.RedBias;
235 bias[1] = ctx->Pixel.GreenBias;
236 bias[2] = ctx->Pixel.BlueBias;
237 bias[3] = ctx->Pixel.AlphaBias;
238
239 scale_p = _mesa_add_state_reference(params, scale_state);
240 bias_p = _mesa_add_state_reference(params, bias_state);
241
242 /* MAD colorTemp, colorTemp, scale, bias; */
243 _mesa_init_instructions(inst + ic, 1);
244 inst[ic].Opcode = OPCODE_MAD;
245 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
246 inst[ic].DstReg.Index = colorTemp;
247 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
248 inst[ic].SrcReg[0].Index = colorTemp;
249 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
250 inst[ic].SrcReg[1].Index = scale_p;
251 inst[ic].SrcReg[2].File = PROGRAM_STATE_VAR;
252 inst[ic].SrcReg[2].Index = bias_p;
253 ic++;
254 }
255
256 if (key->pixelMaps) {
257 const GLuint temp = 1;
258
259 /* create the colormap/texture now if not already done */
260 if (!st->pixel_xfer.pixelmap_texture) {
261 st->pixel_xfer.pixelmap_texture = create_color_map_texture(ctx);
262 st->pixel_xfer.pixelmap_sampler_view =
263 st_create_texture_sampler_view(st->pipe,
264 st->pixel_xfer.pixelmap_texture);
265 }
266
267 /* with a little effort, we can do four pixel map look-ups with
268 * two TEX instructions:
269 */
270
271 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
272 _mesa_init_instructions(inst + ic, 1);
273 inst[ic].Opcode = OPCODE_TEX;
274 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
275 inst[ic].DstReg.Index = temp;
276 inst[ic].DstReg.WriteMask = WRITEMASK_XY; /* write R,G */
277 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
278 inst[ic].SrcReg[0].Index = colorTemp;
279 inst[ic].TexSrcUnit = 1;
280 inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
281 ic++;
282
283 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
284 _mesa_init_instructions(inst + ic, 1);
285 inst[ic].Opcode = OPCODE_TEX;
286 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
287 inst[ic].DstReg.Index = temp;
288 inst[ic].DstReg.WriteMask = WRITEMASK_ZW; /* write B,A */
289 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
290 inst[ic].SrcReg[0].Index = colorTemp;
291 inst[ic].SrcReg[0].Swizzle = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W,
292 SWIZZLE_Z, SWIZZLE_W);
293 inst[ic].TexSrcUnit = 1;
294 inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
295 ic++;
296
297 /* MOV colorTemp, temp; */
298 _mesa_init_instructions(inst + ic, 1);
299 inst[ic].Opcode = OPCODE_MOV;
300 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
301 inst[ic].DstReg.Index = colorTemp;
302 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
303 inst[ic].SrcReg[0].Index = temp;
304 ic++;
305
306 fp->Base.SamplersUsed |= (1 << 1); /* sampler 1 is used */
307 }
308
309 if (key->colorMatrix) {
310 static const gl_state_index row0_state[STATE_LENGTH] =
311 { STATE_COLOR_MATRIX, 0, 0, 0, 0 };
312 static const gl_state_index row1_state[STATE_LENGTH] =
313 { STATE_COLOR_MATRIX, 0, 1, 1, 0 };
314 static const gl_state_index row2_state[STATE_LENGTH] =
315 { STATE_COLOR_MATRIX, 0, 2, 2, 0 };
316 static const gl_state_index row3_state[STATE_LENGTH] =
317 { STATE_COLOR_MATRIX, 0, 3, 3, 0 };
318
319 GLint row0_p = _mesa_add_state_reference(params, row0_state);
320 GLint row1_p = _mesa_add_state_reference(params, row1_state);
321 GLint row2_p = _mesa_add_state_reference(params, row2_state);
322 GLint row3_p = _mesa_add_state_reference(params, row3_state);
323 const GLuint temp = 1;
324
325 /* DP4 temp.x, colorTemp, matrow0; */
326 _mesa_init_instructions(inst + ic, 1);
327 inst[ic].Opcode = OPCODE_DP4;
328 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
329 inst[ic].DstReg.Index = temp;
330 inst[ic].DstReg.WriteMask = WRITEMASK_X;
331 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
332 inst[ic].SrcReg[0].Index = colorTemp;
333 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
334 inst[ic].SrcReg[1].Index = row0_p;
335 ic++;
336
337 /* DP4 temp.y, colorTemp, matrow1; */
338 _mesa_init_instructions(inst + ic, 1);
339 inst[ic].Opcode = OPCODE_DP4;
340 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
341 inst[ic].DstReg.Index = temp;
342 inst[ic].DstReg.WriteMask = WRITEMASK_Y;
343 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
344 inst[ic].SrcReg[0].Index = colorTemp;
345 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
346 inst[ic].SrcReg[1].Index = row1_p;
347 ic++;
348
349 /* DP4 temp.z, colorTemp, matrow2; */
350 _mesa_init_instructions(inst + ic, 1);
351 inst[ic].Opcode = OPCODE_DP4;
352 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
353 inst[ic].DstReg.Index = temp;
354 inst[ic].DstReg.WriteMask = WRITEMASK_Z;
355 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
356 inst[ic].SrcReg[0].Index = colorTemp;
357 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
358 inst[ic].SrcReg[1].Index = row2_p;
359 ic++;
360
361 /* DP4 temp.w, colorTemp, matrow3; */
362 _mesa_init_instructions(inst + ic, 1);
363 inst[ic].Opcode = OPCODE_DP4;
364 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
365 inst[ic].DstReg.Index = temp;
366 inst[ic].DstReg.WriteMask = WRITEMASK_W;
367 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
368 inst[ic].SrcReg[0].Index = colorTemp;
369 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
370 inst[ic].SrcReg[1].Index = row3_p;
371 ic++;
372
373 /* MOV colorTemp, temp; */
374 _mesa_init_instructions(inst + ic, 1);
375 inst[ic].Opcode = OPCODE_MOV;
376 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
377 inst[ic].DstReg.Index = colorTemp;
378 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
379 inst[ic].SrcReg[0].Index = temp;
380 ic++;
381 }
382
383 if (key->colorMatrixPostScaleBias) {
384 static const gl_state_index scale_state[STATE_LENGTH] =
385 { STATE_INTERNAL, STATE_PT_SCALE, 0, 0, 0 };
386 static const gl_state_index bias_state[STATE_LENGTH] =
387 { STATE_INTERNAL, STATE_PT_BIAS, 0, 0, 0 };
388 GLint scale_param, bias_param;
389
390 scale_param = _mesa_add_state_reference(params, scale_state);
391 bias_param = _mesa_add_state_reference(params, bias_state);
392
393 _mesa_init_instructions(inst + ic, 1);
394 inst[ic].Opcode = OPCODE_MAD;
395 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
396 inst[ic].DstReg.Index = colorTemp;
397 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
398 inst[ic].SrcReg[0].Index = colorTemp;
399 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
400 inst[ic].SrcReg[1].Index = scale_param;
401 inst[ic].SrcReg[2].File = PROGRAM_STATE_VAR;
402 inst[ic].SrcReg[2].Index = bias_param;
403 ic++;
404 }
405
406 /* Modify last instruction's dst reg to write to result.color */
407 {
408 struct prog_instruction *last = &inst[ic - 1];
409 last->DstReg.File = PROGRAM_OUTPUT;
410 last->DstReg.Index = FRAG_RESULT_COLOR;
411 }
412
413 /* END; */
414 _mesa_init_instructions(inst + ic, 1);
415 inst[ic].Opcode = OPCODE_END;
416 ic++;
417
418 assert(ic <= MAX_INST);
419
420
421 fp->Base.Instructions = _mesa_alloc_instructions(ic);
422 if (!fp->Base.Instructions) {
423 _mesa_error(ctx, GL_OUT_OF_MEMORY,
424 "generating pixel transfer program");
425 return NULL;
426 }
427
428 _mesa_copy_instructions(fp->Base.Instructions, inst, ic);
429 fp->Base.NumInstructions = ic;
430 fp->Base.Parameters = params;
431
432 #if 0
433 printf("========= pixel transfer prog\n");
434 _mesa_print_program(&fp->Base);
435 _mesa_print_parameter_list(fp->Base.Parameters);
436 #endif
437
438 return fp;
439 }
440
441
442
443 /**
444 * Update st->pixel_xfer.program in response to new pixel-transfer state.
445 */
446 static void
447 update_pixel_transfer(struct st_context *st)
448 {
449 GLcontext *ctx = st->ctx;
450 struct state_key key;
451 struct gl_fragment_program *fp;
452
453 make_state_key(st->ctx, &key);
454
455 fp = (struct gl_fragment_program *)
456 _mesa_search_program_cache(st->pixel_xfer.cache, &key, sizeof(key));
457 if (!fp) {
458 fp = get_pixel_transfer_program(st->ctx, &key);
459 _mesa_program_cache_insert(st->ctx, st->pixel_xfer.cache,
460 &key, sizeof(key), &fp->Base);
461 }
462
463 if (ctx->Pixel.MapColorFlag) {
464 load_color_map_texture(ctx, st->pixel_xfer.pixelmap_texture);
465 }
466 st->pixel_xfer.pixelmap_enabled = ctx->Pixel.MapColorFlag;
467
468 st->pixel_xfer.program = (struct st_fragment_program *) fp;
469 }
470
471
472
473 const struct st_tracked_state st_update_pixel_transfer = {
474 "st_update_pixel_transfer", /* name */
475 { /* dirty */
476 _NEW_PIXEL | _NEW_COLOR_MATRIX, /* mesa */
477 0, /* st */
478 },
479 update_pixel_transfer /* update */
480 };