1510a1e23619f26fd71a00b7068c311965a660c0
[mesa.git] / src / mesa / state_tracker / st_cb_accum.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 * Authors:
30 * Brian Paul
31 */
32
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
36
37 #include "st_context.h"
38 #include "st_cb_accum.h"
39 #include "st_cb_fbo.h"
40 #include "st_draw.h"
41 #include "st_public.h"
42 #include "st_format.h"
43 #include "st_texture.h"
44 #include "pipe/p_context.h"
45 #include "pipe/p_defines.h"
46 #include "pipe/p_inlines.h"
47 #include "util/u_tile.h"
48
49
50 #define UNCLAMPED_FLOAT_TO_SHORT(us, f) \
51 us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
52
53
54 /**
55 * For hardware that supports deep color buffers, we could accelerate
56 * most/all the accum operations with blending/texturing.
57 * For now, just use the get/put_tile() functions and do things in software.
58 */
59
60
61 /**
62 * Wrapper for pipe_get_tile_rgba(). Do format/cpp override to make the
63 * tile util function think the surface is 16bit/channel, even if it's not.
64 * See also: st_renderbuffer_alloc_storage()
65 */
66 static void
67 acc_get_tile_rgba(struct pipe_context *pipe, struct pipe_transfer *acc_pt,
68 uint x, uint y, uint w, uint h, float *p)
69 {
70 const enum pipe_format f = acc_pt->format;
71 const struct pipe_format_block b = acc_pt->block;
72
73 acc_pt->format = DEFAULT_ACCUM_PIPE_FORMAT;
74 acc_pt->block.size = 8;
75 acc_pt->block.width = 1;
76 acc_pt->block.height = 1;
77
78 pipe_get_tile_rgba(acc_pt, x, y, w, h, p);
79
80 acc_pt->format = f;
81 acc_pt->block = b;
82 }
83
84
85 /**
86 * Wrapper for pipe_put_tile_rgba(). Do format/cpp override to make the
87 * tile util function think the surface is 16bit/channel, even if it's not.
88 * See also: st_renderbuffer_alloc_storage()
89 */
90 static void
91 acc_put_tile_rgba(struct pipe_context *pipe, struct pipe_transfer *acc_pt,
92 uint x, uint y, uint w, uint h, const float *p)
93 {
94 enum pipe_format f = acc_pt->format;
95 const struct pipe_format_block b = acc_pt->block;
96
97 acc_pt->format = DEFAULT_ACCUM_PIPE_FORMAT;
98 acc_pt->block.size = 8;
99 acc_pt->block.width = 1;
100 acc_pt->block.height = 1;
101
102 pipe_put_tile_rgba(acc_pt, x, y, w, h, p);
103
104 acc_pt->format = f;
105 acc_pt->block = b;
106 }
107
108
109
110 void
111 st_clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
112 {
113 struct st_renderbuffer *acc_strb = st_renderbuffer(rb);
114 struct pipe_transfer *acc_pt;
115 struct pipe_screen *screen = ctx->st->pipe->screen;
116 const GLint xpos = ctx->DrawBuffer->_Xmin;
117 const GLint ypos = ctx->DrawBuffer->_Ymin;
118 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
119 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
120 GLubyte *map;
121
122 st_teximage_flush_before_map(ctx->st, acc_strb->texture, 0, 0,
123 PIPE_TRANSFER_WRITE);
124
125 acc_pt = screen->get_tex_transfer(screen, acc_strb->texture, 0, 0, 0,
126 PIPE_TRANSFER_WRITE, xpos, ypos,
127 width, height);
128 map = screen->transfer_map(screen, acc_pt);
129
130 /* note acc_strb->format might not equal acc_pt->format */
131 switch (acc_strb->format) {
132 case PIPE_FORMAT_R16G16B16A16_SNORM:
133 {
134 GLshort r = FLOAT_TO_SHORT(ctx->Accum.ClearColor[0]);
135 GLshort g = FLOAT_TO_SHORT(ctx->Accum.ClearColor[1]);
136 GLshort b = FLOAT_TO_SHORT(ctx->Accum.ClearColor[2]);
137 GLshort a = FLOAT_TO_SHORT(ctx->Accum.ClearColor[3]);
138 int i, j;
139 for (i = 0; i < height; i++) {
140 GLshort *dst = (GLshort *) (map + i * acc_pt->stride + xpos * 8);
141 for (j = 0; j < width; j++) {
142 dst[0] = r;
143 dst[1] = g;
144 dst[2] = b;
145 dst[3] = a;
146 dst += 4;
147 }
148 }
149 }
150 break;
151 default:
152 _mesa_problem(ctx, "unexpected format in st_clear_accum_buffer()");
153 }
154
155 screen->transfer_unmap(screen, acc_pt);
156 screen->tex_transfer_destroy(acc_pt);
157 }
158
159
160 /** For ADD/MULT */
161 static void
162 accum_mad(GLcontext *ctx, GLfloat scale, GLfloat bias,
163 GLint xpos, GLint ypos, GLint width, GLint height,
164 struct st_renderbuffer *acc_strb)
165 {
166 struct pipe_screen *screen = ctx->st->pipe->screen;
167 struct pipe_transfer *acc_pt;
168 GLubyte *map;
169
170 st_teximage_flush_before_map(ctx->st, acc_strb->texture, 0, 0,
171 PIPE_TRANSFER_READ_WRITE);
172
173 acc_pt = screen->get_tex_transfer(screen, acc_strb->texture, 0, 0, 0,
174 PIPE_TRANSFER_READ_WRITE, xpos, ypos,
175 width, height);
176 map = screen->transfer_map(screen, acc_pt);
177
178 /* note acc_strb->format might not equal acc_pt->format */
179 switch (acc_strb->format) {
180 case PIPE_FORMAT_R16G16B16A16_SNORM:
181 {
182 int i, j;
183 for (i = 0; i < height; i++) {
184 GLshort *acc = (GLshort *) (map + (ypos + i) * acc_pt->stride + xpos * 8);
185 for (j = 0; j < width * 4; j++) {
186 float val = SHORT_TO_FLOAT(acc[j]) * scale + bias;
187 acc[j] = FLOAT_TO_SHORT(val);
188 }
189 }
190 }
191 break;
192 default:
193 _mesa_problem(NULL, "unexpected format in st_clear_accum_buffer()");
194 }
195
196 screen->transfer_unmap(screen, acc_pt);
197 screen->tex_transfer_destroy(acc_pt);
198 }
199
200
201 static void
202 accum_accum(struct st_context *st, GLfloat value,
203 GLint xpos, GLint ypos, GLint width, GLint height,
204 struct st_renderbuffer *acc_strb,
205 struct st_renderbuffer *color_strb)
206 {
207 struct pipe_context *pipe = st->pipe;
208 struct pipe_screen *screen = pipe->screen;
209 struct pipe_transfer *acc_trans, *color_trans;
210 GLfloat *colorBuf, *accBuf;
211 GLint i;
212
213 st_teximage_flush_before_map(st, acc_strb->texture, 0, 0,
214 PIPE_TRANSFER_READ);
215
216 acc_trans = screen->get_tex_transfer(screen, acc_strb->texture, 0, 0, 0,
217 PIPE_TRANSFER_READ, xpos, ypos,
218 width, height);
219
220 st_teximage_flush_before_map(st, color_strb->texture, 0, 0,
221 PIPE_TRANSFER_READ);
222
223 color_trans = screen->get_tex_transfer(screen, color_strb->texture, 0, 0, 0,
224 PIPE_TRANSFER_READ, xpos, ypos,
225 width, height);
226
227 colorBuf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
228 accBuf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
229
230 pipe_get_tile_rgba(color_trans, 0, 0, width, height, colorBuf);
231 acc_get_tile_rgba(pipe, acc_trans, 0, 0, width, height, accBuf);
232
233 for (i = 0; i < 4 * width * height; i++) {
234 accBuf[i] = accBuf[i] + colorBuf[i] * value;
235 }
236
237 screen->tex_transfer_destroy(acc_trans);
238 acc_trans = screen->get_tex_transfer(screen, acc_strb->texture, 0, 0, 0,
239 PIPE_TRANSFER_WRITE, xpos, ypos,
240 width, height);
241
242 acc_put_tile_rgba(pipe, acc_trans, 0, 0, width, height, accBuf);
243
244 _mesa_free(colorBuf);
245 _mesa_free(accBuf);
246 screen->tex_transfer_destroy(acc_trans);
247 screen->tex_transfer_destroy(color_trans);
248 }
249
250
251 static void
252 accum_load(struct st_context *st, GLfloat value,
253 GLint xpos, GLint ypos, GLint width, GLint height,
254 struct st_renderbuffer *acc_strb,
255 struct st_renderbuffer *color_strb)
256 {
257 struct pipe_context *pipe = st->pipe;
258 struct pipe_screen *screen = pipe->screen;
259 struct pipe_transfer *acc_trans, *color_trans;
260 GLfloat *buf;
261 GLint i;
262
263 st_teximage_flush_before_map(st, acc_strb->texture, 0, 0,
264 PIPE_TRANSFER_WRITE);
265
266 acc_trans = screen->get_tex_transfer(screen, acc_strb->texture, 0, 0, 0,
267 PIPE_TRANSFER_WRITE, xpos, ypos,
268 width, height);
269
270 st_teximage_flush_before_map(st, color_strb->texture, 0, 0,
271 PIPE_TRANSFER_READ);
272
273 color_trans = screen->get_tex_transfer(screen, color_strb->texture, 0, 0, 0,
274 PIPE_TRANSFER_READ, xpos, ypos,
275 width, height);
276
277 buf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
278
279 pipe_get_tile_rgba(color_trans, 0, 0, width, height, buf);
280
281 for (i = 0; i < 4 * width * height; i++) {
282 buf[i] = buf[i] * value;
283 }
284
285 acc_put_tile_rgba(pipe, acc_trans, 0, 0, width, height, buf);
286
287 _mesa_free(buf);
288 screen->tex_transfer_destroy(acc_trans);
289 screen->tex_transfer_destroy(color_trans);
290 }
291
292
293 static void
294 accum_return(GLcontext *ctx, GLfloat value,
295 GLint xpos, GLint ypos, GLint width, GLint height,
296 struct st_renderbuffer *acc_strb,
297 struct st_renderbuffer *color_strb)
298 {
299 struct pipe_context *pipe = ctx->st->pipe;
300 struct pipe_screen *screen = pipe->screen;
301 const GLubyte *colormask = ctx->Color.ColorMask;
302 struct pipe_transfer *acc_trans, *color_trans;
303 GLfloat *abuf, *cbuf = NULL;
304 GLint i, ch;
305
306 abuf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
307
308 st_teximage_flush_before_map(ctx->st, acc_strb->texture, 0, 0,
309 PIPE_TRANSFER_READ);
310
311 acc_trans = screen->get_tex_transfer(screen, acc_strb->texture, 0, 0, 0,
312 PIPE_TRANSFER_READ, xpos, ypos,
313 width, height);
314
315 st_teximage_flush_before_map(ctx->st, color_strb->texture, 0, 0,
316 PIPE_TRANSFER_READ_WRITE);
317
318 color_trans = screen->get_tex_transfer(screen, color_strb->texture, 0, 0, 0,
319 PIPE_TRANSFER_READ_WRITE, xpos, ypos,
320 width, height);
321
322 acc_get_tile_rgba(pipe, acc_trans, 0, 0, width, height, abuf);
323
324 if (!colormask[0] || !colormask[1] || !colormask[2] || !colormask[3]) {
325 cbuf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
326 pipe_get_tile_rgba(color_trans, 0, 0, width, height, cbuf);
327 }
328
329 for (i = 0; i < width * height; i++) {
330 for (ch = 0; ch < 4; ch++) {
331 if (colormask[ch]) {
332 GLfloat val = abuf[i * 4 + ch] * value;
333 abuf[i * 4 + ch] = CLAMP(val, 0.0f, 1.0f);
334 }
335 else {
336 abuf[i * 4 + ch] = cbuf[i * 4 + ch];
337 }
338 }
339 }
340
341 pipe_put_tile_rgba(color_trans, 0, 0, width, height, abuf);
342
343 _mesa_free(abuf);
344 if (cbuf)
345 _mesa_free(cbuf);
346 screen->tex_transfer_destroy(acc_trans);
347 screen->tex_transfer_destroy(color_trans);
348 }
349
350
351 static void
352 st_Accum(GLcontext *ctx, GLenum op, GLfloat value)
353 {
354 struct st_context *st = ctx->st;
355 struct st_renderbuffer *acc_strb
356 = st_renderbuffer(ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
357 struct st_renderbuffer *color_strb
358 = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
359
360 const GLint xpos = ctx->DrawBuffer->_Xmin;
361 const GLint ypos = ctx->DrawBuffer->_Ymin;
362 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
363 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
364
365 /* make sure color bufs aren't cached */
366 st_flush( st, PIPE_FLUSH_RENDER_CACHE, NULL );
367
368 switch (op) {
369 case GL_ADD:
370 if (value != 0.0F) {
371 accum_mad(ctx, 1.0, value, xpos, ypos, width, height, acc_strb);
372 }
373 break;
374 case GL_MULT:
375 if (value != 1.0F) {
376 accum_mad(ctx, value, 0.0, xpos, ypos, width, height, acc_strb);
377 }
378 break;
379 case GL_ACCUM:
380 if (value != 0.0F) {
381 accum_accum(st, value, xpos, ypos, width, height, acc_strb, color_strb);
382 }
383 break;
384 case GL_LOAD:
385 accum_load(st, value, xpos, ypos, width, height, acc_strb, color_strb);
386 break;
387 case GL_RETURN:
388 accum_return(ctx, value, xpos, ypos, width, height, acc_strb, color_strb);
389 break;
390 default:
391 assert(0);
392 }
393 }
394
395
396
397 void st_init_accum_functions(struct dd_function_table *functions)
398 {
399 functions->Accum = st_Accum;
400 }