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