st/mesa: silence int/float and double/float conversion warnings
[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, GL_NONE, GL_NONE,
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, 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(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 = BITFIELD64_BIT(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 GLint scale_p, bias_p;
199
200 scale_p = _mesa_add_state_reference(params, scale_state);
201 bias_p = _mesa_add_state_reference(params, bias_state);
202
203 /* MAD colorTemp, colorTemp, scale, bias; */
204 _mesa_init_instructions(inst + ic, 1);
205 inst[ic].Opcode = OPCODE_MAD;
206 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
207 inst[ic].DstReg.Index = colorTemp;
208 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
209 inst[ic].SrcReg[0].Index = colorTemp;
210 inst[ic].SrcReg[1].File = PROGRAM_STATE_VAR;
211 inst[ic].SrcReg[1].Index = scale_p;
212 inst[ic].SrcReg[2].File = PROGRAM_STATE_VAR;
213 inst[ic].SrcReg[2].Index = bias_p;
214 ic++;
215 }
216
217 if (key->pixelMaps) {
218 const GLuint temp = 1;
219
220 /* create the colormap/texture now if not already done */
221 if (!st->pixel_xfer.pixelmap_texture) {
222 st->pixel_xfer.pixelmap_texture = create_color_map_texture(ctx);
223 st->pixel_xfer.pixelmap_sampler_view =
224 st_create_texture_sampler_view(st->pipe,
225 st->pixel_xfer.pixelmap_texture);
226 }
227
228 /* with a little effort, we can do four pixel map look-ups with
229 * two TEX instructions:
230 */
231
232 /* TEX temp.rg, colorTemp.rgba, texture[1], 2D; */
233 _mesa_init_instructions(inst + ic, 1);
234 inst[ic].Opcode = OPCODE_TEX;
235 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
236 inst[ic].DstReg.Index = temp;
237 inst[ic].DstReg.WriteMask = WRITEMASK_XY; /* write R,G */
238 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
239 inst[ic].SrcReg[0].Index = colorTemp;
240 inst[ic].TexSrcUnit = 1;
241 inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
242 ic++;
243
244 /* TEX temp.ba, colorTemp.baba, texture[1], 2D; */
245 _mesa_init_instructions(inst + ic, 1);
246 inst[ic].Opcode = OPCODE_TEX;
247 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
248 inst[ic].DstReg.Index = temp;
249 inst[ic].DstReg.WriteMask = WRITEMASK_ZW; /* write B,A */
250 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
251 inst[ic].SrcReg[0].Index = colorTemp;
252 inst[ic].SrcReg[0].Swizzle = MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W,
253 SWIZZLE_Z, SWIZZLE_W);
254 inst[ic].TexSrcUnit = 1;
255 inst[ic].TexSrcTarget = TEXTURE_2D_INDEX;
256 ic++;
257
258 /* MOV colorTemp, temp; */
259 _mesa_init_instructions(inst + ic, 1);
260 inst[ic].Opcode = OPCODE_MOV;
261 inst[ic].DstReg.File = PROGRAM_TEMPORARY;
262 inst[ic].DstReg.Index = colorTemp;
263 inst[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
264 inst[ic].SrcReg[0].Index = temp;
265 ic++;
266
267 fp->Base.SamplersUsed |= (1 << 1); /* sampler 1 is used */
268 }
269
270 /* Modify last instruction's dst reg to write to result.color */
271 {
272 struct prog_instruction *last = &inst[ic - 1];
273 last->DstReg.File = PROGRAM_OUTPUT;
274 last->DstReg.Index = FRAG_RESULT_COLOR;
275 }
276
277 /* END; */
278 _mesa_init_instructions(inst + ic, 1);
279 inst[ic].Opcode = OPCODE_END;
280 ic++;
281
282 assert(ic <= MAX_INST);
283
284
285 fp->Base.Instructions = _mesa_alloc_instructions(ic);
286 if (!fp->Base.Instructions) {
287 _mesa_error(ctx, GL_OUT_OF_MEMORY,
288 "generating pixel transfer program");
289 return NULL;
290 }
291
292 _mesa_copy_instructions(fp->Base.Instructions, inst, ic);
293 fp->Base.NumInstructions = ic;
294 fp->Base.Parameters = params;
295
296 #if 0
297 printf("========= pixel transfer prog\n");
298 _mesa_print_program(&fp->Base);
299 _mesa_print_parameter_list(fp->Base.Parameters);
300 #endif
301
302 return fp;
303 }
304
305
306
307 /**
308 * Update st->pixel_xfer.program in response to new pixel-transfer state.
309 */
310 static void
311 update_pixel_transfer(struct st_context *st)
312 {
313 struct gl_context *ctx = st->ctx;
314 struct state_key key;
315 struct gl_fragment_program *fp;
316
317 make_state_key(st->ctx, &key);
318
319 fp = (struct gl_fragment_program *)
320 _mesa_search_program_cache(st->pixel_xfer.cache, &key, sizeof(key));
321 if (!fp) {
322 fp = get_pixel_transfer_program(st->ctx, &key);
323 _mesa_program_cache_insert(st->ctx, st->pixel_xfer.cache,
324 &key, sizeof(key), &fp->Base);
325 }
326
327 if (ctx->Pixel.MapColorFlag) {
328 load_color_map_texture(ctx, st->pixel_xfer.pixelmap_texture);
329 }
330 st->pixel_xfer.pixelmap_enabled = ctx->Pixel.MapColorFlag;
331
332 st->pixel_xfer.program = (struct st_fragment_program *) fp;
333 }
334
335
336
337 const struct st_tracked_state st_update_pixel_transfer = {
338 "st_update_pixel_transfer", /* name */
339 { /* dirty */
340 _NEW_PIXEL, /* mesa */
341 0, /* st */
342 },
343 update_pixel_transfer /* update */
344 };