03c65db70ba9c40ada5fa5239dd1125419997df8
[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 /** For ADD/MULT */
55 static void
56 accum_mad(struct pipe_context *pipe, GLfloat scale, GLfloat bias,
57 GLint xpos, GLint ypos, GLint width, GLint height,
58 struct pipe_surface *acc_ps)
59 {
60 GLfloat *accBuf;
61 GLint i;
62
63 accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
64
65 (void) pipe->region_map(pipe, acc_ps->region);
66
67 acc_ps->get_tile(acc_ps, xpos, ypos, width, height, accBuf);
68
69 for (i = 0; i < 4 * width * height; i++) {
70 GLfloat val = accBuf[i] * scale + bias;
71 accBuf[i] = CLAMP(val, 0.0, 1.0);
72 }
73
74 acc_ps->put_tile(acc_ps, xpos, ypos, width, height, accBuf);
75
76 _mesa_free(accBuf);
77
78 pipe->region_unmap(pipe, acc_ps->region);
79 }
80
81
82 static void
83 accum_accum(struct pipe_context *pipe, GLfloat value,
84 GLint xpos, GLint ypos, GLint width, GLint height,
85 struct pipe_surface *acc_ps,
86 struct pipe_surface *color_ps)
87 {
88 ubyte *colorMap, *accMap;
89 GLfloat *colorBuf, *accBuf;
90 GLint i;
91
92 colorBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
93 accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
94
95 colorMap = pipe->region_map(pipe, color_ps->region);
96 accMap = pipe->region_map(pipe, acc_ps->region);
97
98 color_ps->get_tile(color_ps, xpos, ypos, width, height, colorBuf);
99 acc_ps->get_tile(acc_ps, xpos, ypos, width, height, accBuf);
100
101 for (i = 0; i < 4 * width * height; i++) {
102 GLfloat val = accBuf[i] + colorBuf[i] * value;
103 accBuf[i] = CLAMP(val, 0.0, 1.0);
104 }
105
106 acc_ps->put_tile(acc_ps, xpos, ypos, width, height, accBuf);
107
108 _mesa_free(colorBuf);
109 _mesa_free(accBuf);
110
111 pipe->region_unmap(pipe, color_ps->region);
112 pipe->region_unmap(pipe, acc_ps->region);
113 }
114
115
116 static void
117 accum_load(struct pipe_context *pipe, GLfloat value,
118 GLint xpos, GLint ypos, GLint width, GLint height,
119 struct pipe_surface *acc_ps,
120 struct pipe_surface *color_ps)
121 {
122 GLfloat *buf;
123 GLint i;
124
125 buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
126
127 (void) pipe->region_map(pipe, color_ps->region);
128 (void) pipe->region_map(pipe, acc_ps->region);
129
130 color_ps->get_tile(color_ps, xpos, ypos, width, height, buf);
131
132 for (i = 0; i < 4 * width * height; i++) {
133 GLfloat val = buf[i] * value;
134 buf[i] = CLAMP(val, 0.0, 1.0);
135 }
136
137 acc_ps->put_tile(acc_ps, xpos, ypos, width, height, buf);
138
139 _mesa_free(buf);
140
141 pipe->region_unmap(pipe, color_ps->region);
142 pipe->region_unmap(pipe, acc_ps->region);
143 }
144
145
146 static void
147 accum_return(GLcontext *ctx, GLfloat value,
148 GLint xpos, GLint ypos, GLint width, GLint height,
149 struct pipe_surface *acc_ps,
150 struct pipe_surface *color_ps)
151 {
152 struct pipe_context *pipe = ctx->st->pipe;
153 const GLboolean writeR = ctx->Color.ColorMask[0];
154 const GLboolean writeG = ctx->Color.ColorMask[1];
155 const GLboolean writeB = ctx->Color.ColorMask[2];
156 const GLboolean writeA = ctx->Color.ColorMask[3];
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 acc_ps->get_tile(acc_ps, xpos, ypos, width, height, buf);
166
167 for (i = 0; i < width * height; i++) {
168 if (writeR) {
169 GLfloat r = buf[i * 4 + 0] * value;
170 buf[i * 4 + 0] = CLAMP(r, 0.0, 1.0);
171 }
172 if (writeG) {
173 GLfloat g = buf[i * 4 + 1] * value;
174 buf[i * 4 + 1] = CLAMP(g, 0.0, 1.0);
175 }
176 if (writeB) {
177 GLfloat b = buf[i * 4 + 2] * value;
178 buf[i * 4 + 2] = CLAMP(b, 0.0, 1.0);
179 }
180 if (writeA) {
181 GLfloat a = buf[i * 4 + 3] * value;
182 buf[i * 4 + 3] = CLAMP(a, 0.0, 1.0);
183 }
184 }
185
186 color_ps->put_tile(color_ps, xpos, ypos, width, height, buf);
187
188 _mesa_free(buf);
189
190 pipe->region_unmap(pipe, color_ps->region);
191 pipe->region_unmap(pipe, acc_ps->region);
192 }
193
194
195 static void
196 st_Accum(GLcontext *ctx, GLenum op, GLfloat value)
197 {
198 struct st_context *st = ctx->st;
199 struct pipe_context *pipe = st->pipe;
200 struct st_renderbuffer *acc_strb
201 = st_renderbuffer(ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
202 struct st_renderbuffer *color_strb
203 = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
204 struct pipe_surface *acc_ps = acc_strb->surface;
205 struct pipe_surface *color_ps = color_strb->surface;
206
207 const GLint xpos = ctx->DrawBuffer->_Xmin;
208 const GLint ypos = ctx->DrawBuffer->_Ymin;
209 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
210 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
211
212 switch (op) {
213 case GL_ADD:
214 if (value != 0.0F) {
215 accum_mad(pipe, 1.0, value, xpos, ypos, width, height, acc_ps);
216 }
217 break;
218 case GL_MULT:
219 if (value != 1.0F) {
220 accum_mad(pipe, value, 0.0, xpos, ypos, width, height, acc_ps);
221 }
222 break;
223 case GL_ACCUM:
224 if (value != 0.0F) {
225 accum_accum(pipe, value, xpos, ypos, width, height, acc_ps, color_ps);
226 }
227 break;
228 case GL_LOAD:
229 accum_load(pipe, value, xpos, ypos, width, height, acc_ps, color_ps);
230 break;
231 case GL_RETURN:
232 accum_return(ctx, value, xpos, ypos, width, height, acc_ps, color_ps);
233 break;
234 default:
235 assert(0);
236 }
237 }
238
239
240
241 void st_init_accum_functions(struct dd_function_table *functions)
242 {
243 functions->Accum = st_Accum;
244 }