7a245b0ed6d56dfd17adb719dccf890cc844e411
[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
46
47 /**
48 * For hardware that supports deep color buffers, we could accelerate
49 * most/all the accum operations with blending/texturing.
50 * For now, just use the get/put_tile() functions and do things in software.
51 */
52
53
54 void
55 st_clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
56 {
57 struct pipe_context *pipe = ctx->st->pipe;
58 struct st_renderbuffer *acc_strb = st_renderbuffer(rb);
59 struct pipe_surface *acc_ps = acc_strb->surface;
60 const GLint xpos = ctx->DrawBuffer->_Xmin;
61 const GLint ypos = ctx->DrawBuffer->_Ymin;
62 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
63 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
64 const GLfloat r = ctx->Accum.ClearColor[0];
65 const GLfloat g = ctx->Accum.ClearColor[1];
66 const GLfloat b = ctx->Accum.ClearColor[2];
67 const GLfloat a = ctx->Accum.ClearColor[3];
68 GLfloat *accBuf;
69 GLint i;
70
71 (void) pipe->region_map(pipe, acc_ps->region);
72
73 accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
74
75 for (i = 0; i < width * height; i++) {
76 accBuf[i * 4 + 0] = r;
77 accBuf[i * 4 + 1] = g;
78 accBuf[i * 4 + 2] = b;
79 accBuf[i * 4 + 3] = a;
80 }
81
82 pipe->put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
83
84 free(accBuf);
85
86 pipe->region_unmap(pipe, acc_ps->region);
87 }
88
89
90
91 /** For ADD/MULT */
92 static void
93 accum_mad(struct pipe_context *pipe, GLfloat scale, GLfloat bias,
94 GLint xpos, GLint ypos, GLint width, GLint height,
95 struct pipe_surface *acc_ps)
96 {
97 GLfloat *accBuf;
98 GLint i;
99
100 accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
101
102 (void) pipe->region_map(pipe, acc_ps->region);
103
104 pipe->get_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
105
106 for (i = 0; i < 4 * width * height; i++) {
107 accBuf[i] = accBuf[i] * scale + bias;
108 }
109
110 pipe->put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
111
112 free(accBuf);
113
114 pipe->region_unmap(pipe, acc_ps->region);
115 }
116
117
118 static void
119 accum_accum(struct pipe_context *pipe, GLfloat value,
120 GLint xpos, GLint ypos, GLint width, GLint height,
121 struct pipe_surface *acc_ps,
122 struct pipe_surface *color_ps)
123 {
124 ubyte *colorMap, *accMap;
125 GLfloat *colorBuf, *accBuf;
126 GLint i;
127
128 colorBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
129 accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
130
131 colorMap = pipe->region_map(pipe, color_ps->region);
132 accMap = pipe->region_map(pipe, acc_ps->region);
133
134 pipe->get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, colorBuf);
135 pipe->get_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
136
137 for (i = 0; i < 4 * width * height; i++) {
138 accBuf[i] = accBuf[i] + colorBuf[i] * value;
139 }
140
141 pipe->put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
142
143 free(colorBuf);
144 free(accBuf);
145
146 pipe->region_unmap(pipe, color_ps->region);
147 pipe->region_unmap(pipe, acc_ps->region);
148 }
149
150
151 static void
152 accum_load(struct pipe_context *pipe, GLfloat value,
153 GLint xpos, GLint ypos, GLint width, GLint height,
154 struct pipe_surface *acc_ps,
155 struct pipe_surface *color_ps)
156 {
157 GLfloat *buf;
158 GLint i;
159
160 buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
161
162 (void) pipe->region_map(pipe, color_ps->region);
163 (void) pipe->region_map(pipe, acc_ps->region);
164
165 pipe->get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, buf);
166
167 for (i = 0; i < 4 * width * height; i++) {
168 buf[i] = buf[i] * value;
169 }
170
171 pipe->put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, buf);
172
173 free(buf);
174
175 pipe->region_unmap(pipe, color_ps->region);
176 pipe->region_unmap(pipe, acc_ps->region);
177 }
178
179
180 static void
181 accum_return(GLcontext *ctx, GLfloat value,
182 GLint xpos, GLint ypos, GLint width, GLint height,
183 struct pipe_surface *acc_ps,
184 struct pipe_surface *color_ps)
185 {
186 struct pipe_context *pipe = ctx->st->pipe;
187 const GLubyte *colormask = ctx->Color.ColorMask;
188 GLfloat *abuf, *cbuf = NULL;
189 GLint i, ch;
190
191 abuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
192
193 (void) pipe->region_map(pipe, color_ps->region);
194 (void) pipe->region_map(pipe, acc_ps->region);
195
196 pipe->get_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, abuf);
197
198 if (!colormask[0] || !colormask[1] || !colormask[2] || !colormask[3]) {
199 cbuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
200 pipe->get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, cbuf);
201 }
202
203 for (i = 0; i < width * height; i++) {
204 for (ch = 0; ch < 4; ch++) {
205 if (colormask[ch]) {
206 GLfloat val = abuf[i * 4 + ch] * value;
207 abuf[i * 4 + ch] = CLAMP(val, 0.0, 1.0);
208 }
209 else {
210 abuf[i * 4 + ch] = cbuf[i * 4 + ch];
211 }
212 }
213 }
214
215 pipe->put_tile_rgba(pipe, color_ps, xpos, ypos, width, height, abuf);
216
217 free(abuf);
218 if (cbuf)
219 free(cbuf);
220
221 pipe->region_unmap(pipe, color_ps->region);
222 pipe->region_unmap(pipe, acc_ps->region);
223 }
224
225
226 static void
227 st_Accum(GLcontext *ctx, GLenum op, GLfloat value)
228 {
229 struct st_context *st = ctx->st;
230 struct pipe_context *pipe = st->pipe;
231 struct st_renderbuffer *acc_strb
232 = st_renderbuffer(ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
233 struct st_renderbuffer *color_strb
234 = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
235 struct pipe_surface *acc_ps = acc_strb->surface;
236 struct pipe_surface *color_ps = color_strb->surface;
237
238 const GLint xpos = ctx->DrawBuffer->_Xmin;
239 const GLint ypos = ctx->DrawBuffer->_Ymin;
240 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
241 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
242
243 /* make sure color bufs aren't cached */
244 pipe->flush(pipe, 0);
245
246 switch (op) {
247 case GL_ADD:
248 if (value != 0.0F) {
249 accum_mad(pipe, 1.0, value, xpos, ypos, width, height, acc_ps);
250 }
251 break;
252 case GL_MULT:
253 if (value != 1.0F) {
254 accum_mad(pipe, value, 0.0, xpos, ypos, width, height, acc_ps);
255 }
256 break;
257 case GL_ACCUM:
258 if (value != 0.0F) {
259 accum_accum(pipe, value, xpos, ypos, width, height, acc_ps, color_ps);
260 }
261 break;
262 case GL_LOAD:
263 accum_load(pipe, value, xpos, ypos, width, height, acc_ps, color_ps);
264 break;
265 case GL_RETURN:
266 accum_return(ctx, value, xpos, ypos, width, height, acc_ps, color_ps);
267 break;
268 default:
269 assert(0);
270 }
271 }
272
273
274
275 void st_init_accum_functions(struct dd_function_table *functions)
276 {
277 functions->Accum = st_Accum;
278 }