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