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