gallium: support for array textures and related changes
[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, 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 pixelMaps:1;
59
60 #if 0
61 GLfloat Maps[3][256][4];
62 int NumMaps;
63 GLint NumStages;
64 pipeline_stage Stages[STAGE_MAX];
65 GLboolean StagesUsed[STAGE_MAX];
66 GLfloat Scale1[4], Bias1[4];
67 GLfloat Scale2[4], Bias2[4];
68 #endif
69 };
70
71 static void
72 make_state_key(struct gl_context *ctx, struct state_key *key)
73 {
74 memset(key, 0, sizeof(*key));
75
76 if (ctx->Pixel.RedBias != 0.0 || ctx->Pixel.RedScale != 1.0 ||
77 ctx->Pixel.GreenBias != 0.0 || ctx->Pixel.GreenScale != 1.0 ||
78 ctx->Pixel.BlueBias != 0.0 || ctx->Pixel.BlueScale != 1.0 ||
79 ctx->Pixel.AlphaBias != 0.0 || ctx->Pixel.AlphaScale != 1.0) {
80 key->scaleAndBias = 1;
81 }
82
83 key->pixelMaps = ctx->Pixel.MapColorFlag;
84 }
85
86
87 static struct pipe_resource *
88 create_color_map_texture(struct gl_context *ctx)
89 {
90 struct st_context *st = st_context(ctx);
91 struct pipe_context *pipe = st->pipe;
92 struct pipe_resource *pt;
93 enum pipe_format format;
94 const uint texSize = 256; /* simple, and usually perfect */
95
96 /* find an RGBA texture format */
97 format = st_choose_format(pipe->screen, GL_RGBA,
98 PIPE_TEXTURE_2D, 0, PIPE_BIND_SAMPLER_VIEW);
99
100 /* create texture for color map/table */
101 pt = st_texture_create(st, PIPE_TEXTURE_2D, format, 0,
102 texSize, texSize, 1, PIPE_BIND_SAMPLER_VIEW);
103 return pt;
104 }
105
106
107 /**
108 * Update the pixelmap texture with the contents of the R/G/B/A pixel maps.
109 */
110 static void
111 load_color_map_texture(struct gl_context *ctx, struct pipe_resource *pt)
112 {
113 struct st_context *st = st_context(ctx);
114 struct pipe_context *pipe = st->pipe;
115 struct pipe_transfer *transfer;
116 const GLuint rSize = ctx->PixelMaps.RtoR.Size;
117 const GLuint gSize = ctx->PixelMaps.GtoG.Size;
118 const GLuint bSize = ctx->PixelMaps.BtoB.Size;
119 const GLuint aSize = ctx->PixelMaps.AtoA.Size;
120 const uint texSize = pt->width0;
121 uint *dest;
122 uint i, j;
123
124 transfer = pipe_get_transfer(st_context(ctx)->pipe,
125 pt, 0, 0, PIPE_TRANSFER_WRITE,
126 0, 0, texSize, texSize);
127 dest = (uint *) pipe_transfer_map(pipe, transfer);
128
129 /* Pack four 1D maps into a 2D texture:
130 * R map is placed horizontally, indexed by S, in channel 0
131 * G map is placed vertically, indexed by T, in channel 1
132 * B map is placed horizontally, indexed by S, in channel 2
133 * A map is placed vertically, indexed by T, in channel 3
134 */
135 for (i = 0; i < texSize; i++) {
136 for (j = 0; j < texSize; j++) {
137 union util_color uc;
138 int k = (i * texSize + j);
139 ubyte r = ctx->PixelMaps.RtoR.Map8[j * rSize / texSize];
140 ubyte g = ctx->PixelMaps.GtoG.Map8[i * gSize / texSize];
141 ubyte b = ctx->PixelMaps.BtoB.Map8[j * bSize / texSize];
142 ubyte a = ctx->PixelMaps.AtoA.Map8[i * aSize / texSize];
143 util_pack_color_ub(r, g, b, a, pt->format, &uc);
144 *(dest + k) = uc.ui;
145 }
146 }
147
148 pipe_transfer_unmap(pipe, transfer);
149 pipe->transfer_destroy(pipe, transfer);
150 }
151
152
153
154 #define MAX_INST 100
155
156 /**
157 * Returns a fragment program which implements the current pixel transfer ops.
158 */
159 static struct gl_fragment_program *
160 get_pixel_transfer_program(struct gl_context *ctx, const struct state_key *key)
161 {
162 struct st_context *st = st_context(ctx);
163 struct prog_instruction inst[MAX_INST];
164 struct gl_program_parameter_list *params;
165 struct gl_fragment_program *fp;
166 GLuint ic = 0;
167 const GLuint colorTemp = 0;
168
169 fp = (struct gl_fragment_program *)
170 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
171 if (!fp)
172 return NULL;
173
174 params = _mesa_new_parameter_list();
175
176 /*
177 * Get initial pixel color from the texture.
178 * TEX colorTemp, fragment.texcoord[0], texture[0], 2D;
179 */
180 _mesa_init_instructions(inst + ic, 1);
181 inst[ic].Opcode = OPCODE_TEX;
182 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
183 inst[ic].DstReg.Index = colorTemp;
184 inst[ic].SrcReg[0].File = PROGRAM_INPUT;
185 inst[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
186 inst[ic].TexSrcUnit = 0;
187 inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
188 ic++;
189 fp->Base.InputsRead = (1 << FRAG_ATTRIB_TEX0);
190 fp->Base.OutputsWritten = (1 << FRAG_RESULT_COLOR);
191 fp->Base.SamplersUsed = 0x1; /* sampler 0 (bit 0) is used */
192
193 if (key->scaleAndBias) {
194 static const gl_state_index scale_state[STATE_LENGTH] =
195 { STATE_INTERNAL, STATE_PT_SCALE, 0, 0, 0 };
196 static const gl_state_index bias_state[STATE_LENGTH] =
197 { STATE_INTERNAL, STATE_PT_BIAS, 0, 0, 0 };
198 GLfloat scale[4], bias[4];
199 GLint scale_p, bias_p;
200
201 scale[0] = ctx->Pixel.RedScale;
202 scale[1] = ctx->Pixel.GreenScale;
203 scale[2] = ctx->Pixel.BlueScale;
204 scale[3] = ctx->Pixel.AlphaScale;
205 bias[0] = ctx->Pixel.RedBias;
206 bias[1] = ctx->Pixel.GreenBias;
207 bias[2] = ctx->Pixel.BlueBias;
208 bias[3] = ctx->Pixel.AlphaBias;
209
210 scale_p = _mesa_add_state_reference(params, scale_state);
211 bias_p = _mesa_add_state_reference(params, bias_state);
212
213 /* MAD colorTemp, colorTemp, scale, bias; */
214 _mesa_init_instructions(inst + ic, 1);
215 inst[ic].Opcode = OPCODE_MAD;
216 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
217 inst[ic].DstReg.Index = colorTemp;
218 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
219 inst[ic].SrcReg[0].Index = colorTemp;
220 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
221 inst[ic].SrcReg[1].Index = scale_p;
222 inst[ic].SrcReg[2].File = PROGRAM_STATE_VAR;
223 inst[ic].SrcReg[2].Index = bias_p;
224 ic++;
225 }
226
227 if (key->pixelMaps) {
228 const GLuint temp = 1;
229
230 /* create the colormap/texture now if not already done */
231 if (!st->pixel_xfer.pixelmap_texture) {
232 st->pixel_xfer.pixelmap_texture = create_color_map_texture(ctx);
233 st->pixel_xfer.pixelmap_sampler_view =
234 st_create_texture_sampler_view(st->pipe,
235 st->pixel_xfer.pixelmap_texture);
236 }
237
238 /* with a little effort, we can do four pixel map look-ups with
239 * two TEX instructions:
240 */
241
242 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
243 _mesa_init_instructions(inst + ic, 1);
244 inst[ic].Opcode = OPCODE_TEX;
245 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
246 inst[ic].DstReg.Index = temp;
247 inst[ic].DstReg.WriteMask = WRITEMASK_XY; /* write R,G */
248 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
249 inst[ic].SrcReg[0].Index = colorTemp;
250 inst[ic].TexSrcUnit = 1;
251 inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
252 ic++;
253
254 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
255 _mesa_init_instructions(inst + ic, 1);
256 inst[ic].Opcode = OPCODE_TEX;
257 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
258 inst[ic].DstReg.Index = temp;
259 inst[ic].DstReg.WriteMask = WRITEMASK_ZW; /* write B,A */
260 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
261 inst[ic].SrcReg[0].Index = colorTemp;
262 inst[ic].SrcReg[0].Swizzle = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W,
263 SWIZZLE_Z, SWIZZLE_W);
264 inst[ic].TexSrcUnit = 1;
265 inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
266 ic++;
267
268 /* MOV colorTemp, temp; */
269 _mesa_init_instructions(inst + ic, 1);
270 inst[ic].Opcode = OPCODE_MOV;
271 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
272 inst[ic].DstReg.Index = colorTemp;
273 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
274 inst[ic].SrcReg[0].Index = temp;
275 ic++;
276
277 fp->Base.SamplersUsed |= (1 << 1); /* sampler 1 is used */
278 }
279
280 /* Modify last instruction's dst reg to write to result.color */
281 {
282 struct prog_instruction *last = &inst[ic - 1];
283 last->DstReg.File = PROGRAM_OUTPUT;
284 last->DstReg.Index = FRAG_RESULT_COLOR;
285 }
286
287 /* END; */
288 _mesa_init_instructions(inst + ic, 1);
289 inst[ic].Opcode = OPCODE_END;
290 ic++;
291
292 assert(ic <= MAX_INST);
293
294
295 fp->Base.Instructions = _mesa_alloc_instructions(ic);
296 if (!fp->Base.Instructions) {
297 _mesa_error(ctx, GL_OUT_OF_MEMORY,
298 "generating pixel transfer program");
299 return NULL;
300 }
301
302 _mesa_copy_instructions(fp->Base.Instructions, inst, ic);
303 fp->Base.NumInstructions = ic;
304 fp->Base.Parameters = params;
305
306 #if 0
307 printf("========= pixel transfer prog\n");
308 _mesa_print_program(&fp->Base);
309 _mesa_print_parameter_list(fp->Base.Parameters);
310 #endif
311
312 return fp;
313 }
314
315
316
317 /**
318 * Update st->pixel_xfer.program in response to new pixel-transfer state.
319 */
320 static void
321 update_pixel_transfer(struct st_context *st)
322 {
323 struct gl_context *ctx = st->ctx;
324 struct state_key key;
325 struct gl_fragment_program *fp;
326
327 make_state_key(st->ctx, &key);
328
329 fp = (struct gl_fragment_program *)
330 _mesa_search_program_cache(st->pixel_xfer.cache, &key, sizeof(key));
331 if (!fp) {
332 fp = get_pixel_transfer_program(st->ctx, &key);
333 _mesa_program_cache_insert(st->ctx, st->pixel_xfer.cache,
334 &key, sizeof(key), &fp->Base);
335 }
336
337 if (ctx->Pixel.MapColorFlag) {
338 load_color_map_texture(ctx, st->pixel_xfer.pixelmap_texture);
339 }
340 st->pixel_xfer.pixelmap_enabled = ctx->Pixel.MapColorFlag;
341
342 st->pixel_xfer.program = (struct st_fragment_program *) fp;
343 }
344
345
346
347 const struct st_tracked_state st_update_pixel_transfer = {
348 "st_update_pixel_transfer", /* name */
349 { /* dirty */
350 _NEW_PIXEL, /* mesa */
351 0, /* st */
352 },
353 update_pixel_transfer /* update */
354 };