cf2e9db51cb410404f45c1a7c36f9d35ac3a51eb
[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_cache.h"
39 #include "st_cb_accum.h"
40 #include "st_cb_fbo.h"
41 #include "st_draw.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
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 void
60 st_clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
61 {
62 struct st_renderbuffer *acc_strb = st_renderbuffer(rb);
63 struct pipe_surface *acc_ps = acc_strb->surface;
64 const GLint xpos = ctx->DrawBuffer->_Xmin;
65 const GLint ypos = ctx->DrawBuffer->_Ymin;
66 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
67 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
68 const GLfloat r = ctx->Accum.ClearColor[0];
69 const GLfloat g = ctx->Accum.ClearColor[1];
70 const GLfloat b = ctx->Accum.ClearColor[2];
71 const GLfloat a = ctx->Accum.ClearColor[3];
72
73 (void) pipe_surface_map(acc_ps);
74
75 switch (acc_ps->format) {
76 case PIPE_FORMAT_R16G16B16A16_SNORM:
77 {
78 const short sr = (short) (32767 * r);
79 const short sg = (short) (32767 * g);
80 const short sb = (short) (32767 * b);
81 const short sa = (short) (32767 * a);
82 short *acc = ((short *) acc_ps->map)
83 + (ypos * acc_ps->pitch + xpos) * 4;
84 int i, j;
85 for (i = 0; i < height; i++) {
86 for (j = 0; j < width; j++) {
87 acc[j*4+0] = sr;
88 acc[j*4+1] = sg;
89 acc[j*4+2] = sb;
90 acc[j*4+3] = sa;
91 }
92 acc += acc_ps->pitch * 4;
93 }
94 }
95 break;
96 default:
97 assert(0);
98 }
99
100 pipe_surface_unmap(acc_ps);
101 }
102
103
104 /** Get block of values from accum buffer, converting to float */
105 static void
106 get_accum_tile(struct pipe_context *pipe,
107 struct pipe_surface *acc_surf,
108 int xpos, int ypos, int width, int height,
109 float *buf)
110 {
111 switch (acc_surf->format) {
112 case PIPE_FORMAT_R16G16B16A16_SNORM:
113 {
114 const short *acc = ((const short *) acc_surf->map)
115 + (ypos * acc_surf->pitch + xpos) * 4;
116 int i, j;
117 for (i = 0; i < height; i++) {
118 for (j = 0; j < width; j++) {
119 buf[j*4+0] = SHORT_TO_FLOAT(acc[j*4+0]);
120 buf[j*4+1] = SHORT_TO_FLOAT(acc[j*4+1]);
121 buf[j*4+2] = SHORT_TO_FLOAT(acc[j*4+2]);
122 buf[j*4+3] = SHORT_TO_FLOAT(acc[j*4+3]);
123 }
124 acc += acc_surf->pitch * 4;
125 buf += width * 4;
126 }
127 }
128 break;
129 default:
130 assert(0);
131 }
132 }
133
134
135 /** Put block of values into accum buffer, converting from float */
136 static void
137 put_accum_tile(struct pipe_context *pipe,
138 struct pipe_surface *acc_surf,
139 int xpos, int ypos, int width, int height,
140 const float *buf)
141 {
142 switch (acc_surf->format) {
143 case PIPE_FORMAT_R16G16B16A16_SNORM:
144 {
145 short *acc = ((short *) acc_surf->map)
146 + (ypos * acc_surf->pitch + xpos) * 4;
147 int i, j;
148 for (i = 0; i < height; i++) {
149 for (j = 0; j < width; j++) {
150 short r, g, b, a;
151 UNCLAMPED_FLOAT_TO_SHORT(r, buf[j*4+0]);
152 UNCLAMPED_FLOAT_TO_SHORT(g, buf[j*4+1]);
153 UNCLAMPED_FLOAT_TO_SHORT(b, buf[j*4+2]);
154 UNCLAMPED_FLOAT_TO_SHORT(a, buf[j*4+3]);
155 acc[j*4+0] = r;
156 acc[j*4+1] = g;
157 acc[j*4+2] = b;
158 acc[j*4+3] = a;
159 }
160 acc += acc_surf->pitch * 4;
161 buf += width * 4;
162 }
163 }
164 break;
165 default:
166 assert(0);
167 }
168 }
169
170
171 /** For ADD/MULT */
172 static void
173 accum_mad(struct pipe_context *pipe, GLfloat scale, GLfloat bias,
174 GLint xpos, GLint ypos, GLint width, GLint height,
175 struct pipe_surface *acc_ps)
176 {
177 GLfloat *accBuf;
178 GLint i;
179
180 accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
181
182 (void) pipe_surface_map(acc_ps);
183
184 get_accum_tile(pipe, acc_ps, xpos, ypos, width, height, accBuf);
185
186 for (i = 0; i < 4 * width * height; i++) {
187 accBuf[i] = accBuf[i] * scale + bias;
188 }
189
190 put_accum_tile(pipe, acc_ps, xpos, ypos, width, height, accBuf);
191
192 free(accBuf);
193
194 pipe_surface_unmap(acc_ps);
195 }
196
197
198 static void
199 accum_accum(struct pipe_context *pipe, GLfloat value,
200 GLint xpos, GLint ypos, GLint width, GLint height,
201 struct pipe_surface *acc_ps,
202 struct pipe_surface *color_ps)
203 {
204 ubyte *colorMap, *accMap;
205 GLfloat *colorBuf, *accBuf;
206 GLint i;
207
208 colorBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
209 accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
210
211 colorMap = pipe_surface_map(color_ps);
212 accMap = pipe_surface_map(acc_ps);
213
214 pipe->get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, colorBuf);
215 get_accum_tile(pipe, acc_ps, xpos, ypos, width, height, accBuf);
216
217 for (i = 0; i < 4 * width * height; i++) {
218 accBuf[i] = accBuf[i] + colorBuf[i] * value;
219 }
220
221 put_accum_tile(pipe, acc_ps, xpos, ypos, width, height, accBuf);
222
223 free(colorBuf);
224 free(accBuf);
225
226 pipe_surface_unmap(color_ps);
227 pipe_surface_unmap(acc_ps);
228 }
229
230
231 static void
232 accum_load(struct pipe_context *pipe, GLfloat value,
233 GLint xpos, GLint ypos, GLint width, GLint height,
234 struct pipe_surface *acc_ps,
235 struct pipe_surface *color_ps)
236 {
237 GLfloat *buf;
238 GLint i;
239
240 buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
241
242 (void) pipe_surface_map(color_ps);
243 (void) pipe_surface_map(acc_ps);
244
245 pipe->get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, buf);
246
247 for (i = 0; i < 4 * width * height; i++) {
248 buf[i] = buf[i] * value;
249 }
250
251 put_accum_tile(pipe, acc_ps, xpos, ypos, width, height, buf);
252
253 free(buf);
254
255 pipe_surface_unmap(color_ps);
256 pipe_surface_unmap(acc_ps);
257 }
258
259
260 static void
261 accum_return(GLcontext *ctx, GLfloat value,
262 GLint xpos, GLint ypos, GLint width, GLint height,
263 struct pipe_surface *acc_ps,
264 struct pipe_surface *color_ps)
265 {
266 struct pipe_context *pipe = ctx->st->pipe;
267 const GLubyte *colormask = ctx->Color.ColorMask;
268 GLfloat *abuf, *cbuf = NULL;
269 GLint i, ch;
270
271 abuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
272
273 (void) pipe_surface_map(color_ps);
274 (void) pipe_surface_map(acc_ps);
275
276 get_accum_tile(pipe, acc_ps, xpos, ypos, width, height, abuf);
277
278 if (!colormask[0] || !colormask[1] || !colormask[2] || !colormask[3]) {
279 cbuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
280 pipe->get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, cbuf);
281 }
282
283 for (i = 0; i < width * height; i++) {
284 for (ch = 0; ch < 4; ch++) {
285 if (colormask[ch]) {
286 GLfloat val = abuf[i * 4 + ch] * value;
287 abuf[i * 4 + ch] = CLAMP(val, 0.0, 1.0);
288 }
289 else {
290 abuf[i * 4 + ch] = cbuf[i * 4 + ch];
291 }
292 }
293 }
294
295 pipe->put_tile_rgba(pipe, color_ps, xpos, ypos, width, height, abuf);
296
297 free(abuf);
298 if (cbuf)
299 free(cbuf);
300
301 pipe_surface_unmap(color_ps);
302 pipe_surface_unmap(acc_ps);
303 }
304
305
306 static void
307 st_Accum(GLcontext *ctx, GLenum op, GLfloat value)
308 {
309 struct st_context *st = ctx->st;
310 struct pipe_context *pipe = st->pipe;
311 struct st_renderbuffer *acc_strb
312 = st_renderbuffer(ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
313 struct st_renderbuffer *color_strb
314 = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
315 struct pipe_surface *acc_ps = acc_strb->surface;
316 struct pipe_surface *color_ps = color_strb->surface;
317
318 const GLint xpos = ctx->DrawBuffer->_Xmin;
319 const GLint ypos = ctx->DrawBuffer->_Ymin;
320 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
321 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
322
323 /* make sure color bufs aren't cached */
324 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE);
325
326 switch (op) {
327 case GL_ADD:
328 if (value != 0.0F) {
329 accum_mad(pipe, 1.0, value, xpos, ypos, width, height, acc_ps);
330 }
331 break;
332 case GL_MULT:
333 if (value != 1.0F) {
334 accum_mad(pipe, value, 0.0, xpos, ypos, width, height, acc_ps);
335 }
336 break;
337 case GL_ACCUM:
338 if (value != 0.0F) {
339 accum_accum(pipe, value, xpos, ypos, width, height, acc_ps, color_ps);
340 }
341 break;
342 case GL_LOAD:
343 accum_load(pipe, value, xpos, ypos, width, height, acc_ps, color_ps);
344 break;
345 case GL_RETURN:
346 accum_return(ctx, value, xpos, ypos, width, height, acc_ps, color_ps);
347 break;
348 default:
349 assert(0);
350 }
351 }
352
353
354
355 void st_init_accum_functions(struct dd_function_table *functions)
356 {
357 functions->Accum = st_Accum;
358 }