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