Merge branch 'master' into gallium-0.2
[mesa.git] / src / mesa / state_tracker / acc2.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/p_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 static void
60 acc_get_tile_rgba(struct pipe_context *pipe, struct pipe_surface *acc_ps,
61 uint x, uint y, uint w, uint h, float *p)
62 {
63 const enum pipe_format f = acc_ps->format;
64 const int cpp = acc_ps->cpp;
65
66 acc_ps->format = PIPE_FORMAT_R16G16B16A16_SNORM;
67 acc_ps->cpp = 8;
68
69 pipe_get_tile_rgba(pipe, acc_ps, x, y, w, h, p);
70
71 acc_ps->format = f;
72 acc_ps->cpp = cpp;
73 }
74
75
76 static void
77 acc_put_tile_rgba(struct pipe_context *pipe, struct pipe_surface *acc_ps,
78 uint x, uint y, uint w, uint h, const float *p)
79 {
80 enum pipe_format f = acc_ps->format;
81 const int cpp = acc_ps->cpp;
82
83 acc_ps->format = PIPE_FORMAT_R16G16B16A16_SNORM;
84 acc_ps->cpp = 8;
85
86 pipe_put_tile_rgba(pipe, acc_ps, x, y, w, h, p);
87
88 acc_ps->format = f;
89 acc_ps->cpp = cpp;
90 }
91
92
93
94 void
95 st_clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
96 {
97 struct pipe_context *pipe = ctx->st->pipe;
98 struct st_renderbuffer *acc_strb = st_renderbuffer(rb);
99 struct pipe_surface *acc_ps = acc_strb->surface;
100 const GLint xpos = ctx->DrawBuffer->_Xmin;
101 const GLint ypos = ctx->DrawBuffer->_Ymin;
102 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
103 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
104 const GLfloat r = ctx->Accum.ClearColor[0];
105 const GLfloat g = ctx->Accum.ClearColor[1];
106 const GLfloat b = ctx->Accum.ClearColor[2];
107 const GLfloat a = ctx->Accum.ClearColor[3];
108 GLfloat *accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
109 int i;
110
111 #if 1
112 GLvoid *map;
113
114 map = pipe_surface_map(acc_ps);
115 switch (acc_strb->format) {
116 case PIPE_FORMAT_R16G16B16A16_SNORM:
117 {
118 GLshort r = FLOAT_TO_SHORT(ctx->Accum.ClearColor[0]);
119 GLshort g = FLOAT_TO_SHORT(ctx->Accum.ClearColor[1]);
120 GLshort b = FLOAT_TO_SHORT(ctx->Accum.ClearColor[2]);
121 GLshort a = FLOAT_TO_SHORT(ctx->Accum.ClearColor[3]);
122 int i, j;
123 for (i = 0; i < height; i++) {
124 GLshort *dst = ((GLshort *) map
125 + ((ypos + i) * acc_ps->pitch + xpos) * 4);
126 for (j = 0; j < width; j++) {
127 dst[0] = r;
128 dst[1] = g;
129 dst[2] = b;
130 dst[3] = a;
131 dst += 4;
132 }
133 }
134 }
135 break;
136 default:
137 _mesa_problem(ctx, "unexpected format in st_clear_accum_buffer()");
138 }
139
140 pipe_surface_unmap(acc_ps);
141
142 #else
143 for (i = 0; i < width * height; i++) {
144 accBuf[i*4+0] = r;
145 accBuf[i*4+1] = g;
146 accBuf[i*4+2] = b;
147 accBuf[i*4+3] = a;
148 }
149
150 acc_put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
151 #endif
152 }
153
154
155 /** For ADD/MULT */
156 static void
157 accum_mad(struct pipe_context *pipe, GLfloat scale, GLfloat bias,
158 GLint xpos, GLint ypos, GLint width, GLint height,
159 struct pipe_surface *acc_ps)
160 {
161 GLfloat *accBuf;
162 GLint i;
163
164 accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
165
166 pipe_get_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
167
168 for (i = 0; i < 4 * width * height; i++) {
169 accBuf[i] = accBuf[i] * scale + bias;
170 }
171
172 pipe_put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
173
174 free(accBuf);
175 }
176
177
178 static void
179 accum_accum(struct pipe_context *pipe, GLfloat value,
180 GLint xpos, GLint ypos, GLint width, GLint height,
181 struct pipe_surface *acc_ps,
182 struct pipe_surface *color_ps)
183 {
184 GLfloat *colorBuf, *accBuf;
185 GLint i;
186
187 colorBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
188 accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
189
190 pipe_get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, colorBuf);
191 acc_get_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
192
193 for (i = 0; i < 4 * width * height; i++) {
194 accBuf[i] = accBuf[i] + colorBuf[i] * value;
195 }
196
197 acc_put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
198
199 free(colorBuf);
200 free(accBuf);
201 }
202
203
204 static void
205 accum_load(struct pipe_context *pipe, GLfloat value,
206 GLint xpos, GLint ypos, GLint width, GLint height,
207 struct pipe_surface *acc_ps,
208 struct pipe_surface *color_ps)
209 {
210 GLfloat *buf;
211 GLint i;
212
213 buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
214
215 pipe_get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, buf);
216
217 for (i = 0; i < 4 * width * height; i++) {
218 buf[i] = buf[i] * value;
219 }
220
221 acc_put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, buf);
222
223 free(buf);
224 }
225
226
227 static void
228 accum_return(GLcontext *ctx, GLfloat value,
229 GLint xpos, GLint ypos, GLint width, GLint height,
230 struct pipe_surface *acc_ps,
231 struct pipe_surface *color_ps)
232 {
233 struct pipe_context *pipe = ctx->st->pipe;
234 const GLubyte *colormask = ctx->Color.ColorMask;
235 GLfloat *abuf, *cbuf = NULL;
236 GLint i, ch;
237
238 abuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
239
240 acc_get_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, abuf);
241
242 if (!colormask[0] || !colormask[1] || !colormask[2] || !colormask[3]) {
243 cbuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
244 pipe_get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, cbuf);
245 }
246
247 for (i = 0; i < width * height; i++) {
248 for (ch = 0; ch < 4; ch++) {
249 if (colormask[ch]) {
250 GLfloat val = abuf[i * 4 + ch] * value;
251 abuf[i * 4 + ch] = CLAMP(val, 0.0, 1.0);
252 }
253 else {
254 abuf[i * 4 + ch] = cbuf[i * 4 + ch];
255 }
256 }
257 }
258
259 pipe_put_tile_rgba(pipe, color_ps, xpos, ypos, width, height, abuf);
260
261 free(abuf);
262 if (cbuf)
263 free(cbuf);
264 }
265
266
267 static void
268 st_Accum(GLcontext *ctx, GLenum op, GLfloat value)
269 {
270 struct st_context *st = ctx->st;
271 struct pipe_context *pipe = st->pipe;
272 struct st_renderbuffer *acc_strb
273 = st_renderbuffer(ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
274 struct st_renderbuffer *color_strb
275 = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
276 struct pipe_surface *acc_ps = acc_strb->surface;
277 struct pipe_surface *color_ps = color_strb->surface;
278
279 const GLint xpos = ctx->DrawBuffer->_Xmin;
280 const GLint ypos = ctx->DrawBuffer->_Ymin;
281 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
282 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
283
284 /* make sure color bufs aren't cached */
285 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
286
287 switch (op) {
288 case GL_ADD:
289 if (value != 0.0F) {
290 accum_mad(pipe, 1.0, value, xpos, ypos, width, height, acc_ps);
291 }
292 break;
293 case GL_MULT:
294 if (value != 1.0F) {
295 accum_mad(pipe, value, 0.0, xpos, ypos, width, height, acc_ps);
296 }
297 break;
298 case GL_ACCUM:
299 if (value != 0.0F) {
300 accum_accum(pipe, value, xpos, ypos, width, height, acc_ps, color_ps);
301 }
302 break;
303 case GL_LOAD:
304 accum_load(pipe, value, xpos, ypos, width, height, acc_ps, color_ps);
305 break;
306 case GL_RETURN:
307 accum_return(ctx, value, xpos, ypos, width, height, acc_ps, color_ps);
308 break;
309 default:
310 assert(0);
311 }
312 }
313
314
315
316 void st_init_accum_functions(struct dd_function_table *functions)
317 {
318 functions->Accum = st_Accum;
319 }