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