Merge branch 'gallium-vertex-linear' 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;
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 acc_ps = screen->get_tex_surface(screen, acc_strb->texture, 0, 0, 0,
117 PIPE_BUFFER_USAGE_CPU_WRITE);
118 map = screen->surface_map(screen, acc_ps,
119 PIPE_BUFFER_USAGE_CPU_WRITE);
120
121 /* note acc_strb->format might not equal acc_ps->format */
122 switch (acc_strb->format) {
123 case PIPE_FORMAT_R16G16B16A16_SNORM:
124 {
125 GLshort r = FLOAT_TO_SHORT(ctx->Accum.ClearColor[0]);
126 GLshort g = FLOAT_TO_SHORT(ctx->Accum.ClearColor[1]);
127 GLshort b = FLOAT_TO_SHORT(ctx->Accum.ClearColor[2]);
128 GLshort a = FLOAT_TO_SHORT(ctx->Accum.ClearColor[3]);
129 int i, j;
130 for (i = 0; i < height; i++) {
131 GLshort *dst = ((GLshort *) map
132 + ((ypos + i) * acc_ps->pitch + xpos) * 4);
133 for (j = 0; j < width; j++) {
134 dst[0] = r;
135 dst[1] = g;
136 dst[2] = b;
137 dst[3] = a;
138 dst += 4;
139 }
140 }
141 }
142 break;
143 default:
144 _mesa_problem(ctx, "unexpected format in st_clear_accum_buffer()");
145 }
146
147 screen->surface_unmap(screen, acc_ps);
148 pipe_surface_reference(&acc_ps, NULL);
149 }
150
151
152 /** For ADD/MULT */
153 static void
154 accum_mad(GLcontext *ctx, GLfloat scale, GLfloat bias,
155 GLint xpos, GLint ypos, GLint width, GLint height,
156 struct st_renderbuffer *acc_strb)
157 {
158 struct pipe_screen *screen = ctx->st->pipe->screen;
159 struct pipe_surface *acc_ps = acc_strb->surface;
160 GLvoid *map;
161
162 map = screen->surface_map(screen, acc_ps,
163 PIPE_BUFFER_USAGE_CPU_WRITE);
164
165 /* note acc_strb->format might not equal acc_ps->format */
166 switch (acc_strb->format) {
167 case PIPE_FORMAT_R16G16B16A16_SNORM:
168 {
169 int i, j;
170 for (i = 0; i < height; i++) {
171 GLshort *acc = ((GLshort *) map
172 + ((ypos + i) * acc_ps->pitch + xpos) * 4);
173 for (j = 0; j < width * 4; j++) {
174 float val = SHORT_TO_FLOAT(acc[j]) * scale + bias;
175 acc[j] = FLOAT_TO_SHORT(val);
176 }
177 }
178 }
179 break;
180 default:
181 _mesa_problem(NULL, "unexpected format in st_clear_accum_buffer()");
182 }
183
184 screen->surface_unmap(screen, acc_ps);
185 }
186
187
188 static void
189 accum_accum(struct pipe_context *pipe, GLfloat value,
190 GLint xpos, GLint ypos, GLint width, GLint height,
191 struct st_renderbuffer *acc_strb,
192 struct st_renderbuffer *color_strb)
193 {
194 struct pipe_screen *screen = pipe->screen;
195 struct pipe_surface *acc_surf, *color_surf;
196 GLfloat *colorBuf, *accBuf;
197 GLint i;
198
199 acc_surf = screen->get_tex_surface(screen, acc_strb->texture, 0, 0, 0,
200 (PIPE_BUFFER_USAGE_CPU_WRITE |
201 PIPE_BUFFER_USAGE_CPU_READ));
202
203 color_surf = screen->get_tex_surface(screen, color_strb->texture, 0, 0, 0,
204 PIPE_BUFFER_USAGE_CPU_READ);
205
206 colorBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
207 accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
208
209 pipe_get_tile_rgba(pipe, color_surf, xpos, ypos, width, height, colorBuf);
210 acc_get_tile_rgba(pipe, acc_surf, xpos, ypos, width, height, accBuf);
211
212 for (i = 0; i < 4 * width * height; i++) {
213 accBuf[i] = accBuf[i] + colorBuf[i] * value;
214 }
215
216 acc_put_tile_rgba(pipe, acc_surf, xpos, ypos, width, height, accBuf);
217
218 free(colorBuf);
219 free(accBuf);
220 pipe_surface_reference(&acc_surf, NULL);
221 pipe_surface_reference(&color_surf, NULL);
222 }
223
224
225 static void
226 accum_load(struct pipe_context *pipe, GLfloat value,
227 GLint xpos, GLint ypos, GLint width, GLint height,
228 struct st_renderbuffer *acc_strb,
229 struct st_renderbuffer *color_strb)
230 {
231 struct pipe_screen *screen = pipe->screen;
232 struct pipe_surface *acc_surf, *color_surf;
233 GLfloat *buf;
234 GLint i;
235
236 acc_surf = screen->get_tex_surface(screen, acc_strb->texture, 0, 0, 0,
237 PIPE_BUFFER_USAGE_CPU_WRITE);
238
239 color_surf = screen->get_tex_surface(screen, color_strb->texture, 0, 0, 0,
240 PIPE_BUFFER_USAGE_CPU_READ);
241
242 buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
243
244 pipe_get_tile_rgba(pipe, color_surf, xpos, ypos, width, height, buf);
245
246 for (i = 0; i < 4 * width * height; i++) {
247 buf[i] = buf[i] * value;
248 }
249
250 acc_put_tile_rgba(pipe, acc_surf, xpos, ypos, width, height, buf);
251
252 free(buf);
253 pipe_surface_reference(&acc_surf, NULL);
254 pipe_surface_reference(&color_surf, NULL);
255 }
256
257
258 static void
259 accum_return(GLcontext *ctx, GLfloat value,
260 GLint xpos, GLint ypos, GLint width, GLint height,
261 struct st_renderbuffer *acc_strb,
262 struct st_renderbuffer *color_strb)
263 {
264 struct pipe_context *pipe = ctx->st->pipe;
265 struct pipe_screen *screen = pipe->screen;
266 const GLubyte *colormask = ctx->Color.ColorMask;
267 struct pipe_surface *acc_surf, *color_surf;
268 GLfloat *abuf, *cbuf = NULL;
269 GLint i, ch;
270
271 abuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
272
273 acc_surf = screen->get_tex_surface(screen, acc_strb->texture, 0, 0, 0,
274 PIPE_BUFFER_USAGE_CPU_READ);
275
276 color_surf = screen->get_tex_surface(screen, color_strb->texture, 0, 0, 0,
277 (PIPE_BUFFER_USAGE_CPU_READ |
278 PIPE_BUFFER_USAGE_CPU_WRITE));
279
280 acc_get_tile_rgba(pipe, acc_surf, xpos, ypos, width, height, abuf);
281
282 if (!colormask[0] || !colormask[1] || !colormask[2] || !colormask[3]) {
283 cbuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
284 pipe_get_tile_rgba(pipe, color_surf, xpos, ypos, width, height, cbuf);
285 }
286
287 for (i = 0; i < width * height; i++) {
288 for (ch = 0; ch < 4; ch++) {
289 if (colormask[ch]) {
290 GLfloat val = abuf[i * 4 + ch] * value;
291 abuf[i * 4 + ch] = CLAMP(val, 0.0, 1.0);
292 }
293 else {
294 abuf[i * 4 + ch] = cbuf[i * 4 + ch];
295 }
296 }
297 }
298
299 pipe_put_tile_rgba(pipe, color_surf, xpos, ypos, width, height, abuf);
300
301 free(abuf);
302 if (cbuf)
303 free(cbuf);
304 pipe_surface_reference(&acc_surf, NULL);
305 pipe_surface_reference(&color_surf, NULL);
306 }
307
308
309 static void
310 st_Accum(GLcontext *ctx, GLenum op, GLfloat value)
311 {
312 struct st_context *st = ctx->st;
313 struct pipe_context *pipe = st->pipe;
314 struct st_renderbuffer *acc_strb
315 = st_renderbuffer(ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
316 struct st_renderbuffer *color_strb
317 = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
318
319 const GLint xpos = ctx->DrawBuffer->_Xmin;
320 const GLint ypos = ctx->DrawBuffer->_Ymin;
321 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
322 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
323
324 /* make sure color bufs aren't cached */
325 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
326
327 switch (op) {
328 case GL_ADD:
329 if (value != 0.0F) {
330 accum_mad(ctx, 1.0, value, xpos, ypos, width, height, acc_strb);
331 }
332 break;
333 case GL_MULT:
334 if (value != 1.0F) {
335 accum_mad(ctx, value, 0.0, xpos, ypos, width, height, acc_strb);
336 }
337 break;
338 case GL_ACCUM:
339 if (value != 0.0F) {
340 accum_accum(pipe, value, xpos, ypos, width, height, acc_strb, color_strb);
341 }
342 break;
343 case GL_LOAD:
344 accum_load(pipe, value, xpos, ypos, width, height, acc_strb, color_strb);
345 break;
346 case GL_RETURN:
347 accum_return(ctx, value, xpos, ypos, width, height, acc_strb, color_strb);
348 break;
349 default:
350 assert(0);
351 }
352 }
353
354
355
356 void st_init_accum_functions(struct dd_function_table *functions)
357 {
358 functions->Accum = st_Accum;
359 }