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