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