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