Remove mapping fields from struct pipe_surface.
[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 #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 void
60 st_clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
61 {
62 struct pipe_context *pipe = ctx->st->pipe;
63 struct st_renderbuffer *acc_strb = st_renderbuffer(rb);
64 struct pipe_surface *acc_ps = acc_strb->surface;
65 const GLint xpos = ctx->DrawBuffer->_Xmin;
66 const GLint ypos = ctx->DrawBuffer->_Ymin;
67 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
68 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
69 const GLfloat r = ctx->Accum.ClearColor[0];
70 const GLfloat g = ctx->Accum.ClearColor[1];
71 const GLfloat b = ctx->Accum.ClearColor[2];
72 const GLfloat a = ctx->Accum.ClearColor[3];
73 GLfloat *accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
74 int i;
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
86
87 /** For ADD/MULT */
88 static void
89 accum_mad(struct pipe_context *pipe, GLfloat scale, GLfloat bias,
90 GLint xpos, GLint ypos, GLint width, GLint height,
91 struct pipe_surface *acc_ps)
92 {
93 GLfloat *accBuf;
94 GLint i;
95
96 accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
97
98 pipe->get_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
99
100 for (i = 0; i < 4 * width * height; i++) {
101 accBuf[i] = accBuf[i] * scale + bias;
102 }
103
104 pipe->put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
105
106 free(accBuf);
107 }
108
109
110 static void
111 accum_accum(struct pipe_context *pipe, GLfloat value,
112 GLint xpos, GLint ypos, GLint width, GLint height,
113 struct pipe_surface *acc_ps,
114 struct pipe_surface *color_ps)
115 {
116 GLfloat *colorBuf, *accBuf;
117 GLint i;
118
119 colorBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
120 accBuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
121
122 pipe->get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, colorBuf);
123 pipe->get_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
124
125 for (i = 0; i < 4 * width * height; i++) {
126 accBuf[i] = accBuf[i] + colorBuf[i] * value;
127 }
128
129 pipe->put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, accBuf);
130
131 free(colorBuf);
132 free(accBuf);
133 }
134
135
136 static void
137 accum_load(struct pipe_context *pipe, GLfloat value,
138 GLint xpos, GLint ypos, GLint width, GLint height,
139 struct pipe_surface *acc_ps,
140 struct pipe_surface *color_ps)
141 {
142 GLfloat *buf;
143 GLint i;
144
145 buf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
146
147 pipe->get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, buf);
148
149 for (i = 0; i < 4 * width * height; i++) {
150 buf[i] = buf[i] * value;
151 }
152
153 pipe->put_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, buf);
154
155 free(buf);
156 }
157
158
159 static void
160 accum_return(GLcontext *ctx, GLfloat value,
161 GLint xpos, GLint ypos, GLint width, GLint height,
162 struct pipe_surface *acc_ps,
163 struct pipe_surface *color_ps)
164 {
165 struct pipe_context *pipe = ctx->st->pipe;
166 const GLubyte *colormask = ctx->Color.ColorMask;
167 GLfloat *abuf, *cbuf = NULL;
168 GLint i, ch;
169
170 abuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
171
172 pipe->get_tile_rgba(pipe, acc_ps, xpos, ypos, width, height, abuf);
173
174 if (!colormask[0] || !colormask[1] || !colormask[2] || !colormask[3]) {
175 cbuf = (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
176 pipe->get_tile_rgba(pipe, color_ps, xpos, ypos, width, height, cbuf);
177 }
178
179 for (i = 0; i < width * height; i++) {
180 for (ch = 0; ch < 4; ch++) {
181 if (colormask[ch]) {
182 GLfloat val = abuf[i * 4 + ch] * value;
183 abuf[i * 4 + ch] = CLAMP(val, 0.0, 1.0);
184 }
185 else {
186 abuf[i * 4 + ch] = cbuf[i * 4 + ch];
187 }
188 }
189 }
190
191 pipe->put_tile_rgba(pipe, color_ps, xpos, ypos, width, height, abuf);
192
193 free(abuf);
194 if (cbuf)
195 free(cbuf);
196 }
197
198
199 static void
200 st_Accum(GLcontext *ctx, GLenum op, GLfloat value)
201 {
202 struct st_context *st = ctx->st;
203 struct pipe_context *pipe = st->pipe;
204 struct st_renderbuffer *acc_strb
205 = st_renderbuffer(ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
206 struct st_renderbuffer *color_strb
207 = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
208 struct pipe_surface *acc_ps = acc_strb->surface;
209 struct pipe_surface *color_ps = color_strb->surface;
210
211 const GLint xpos = ctx->DrawBuffer->_Xmin;
212 const GLint ypos = ctx->DrawBuffer->_Ymin;
213 const GLint width = ctx->DrawBuffer->_Xmax - xpos;
214 const GLint height = ctx->DrawBuffer->_Ymax - ypos;
215
216 /* make sure color bufs aren't cached */
217 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE);
218
219 switch (op) {
220 case GL_ADD:
221 if (value != 0.0F) {
222 accum_mad(pipe, 1.0, value, xpos, ypos, width, height, acc_ps);
223 }
224 break;
225 case GL_MULT:
226 if (value != 1.0F) {
227 accum_mad(pipe, value, 0.0, xpos, ypos, width, height, acc_ps);
228 }
229 break;
230 case GL_ACCUM:
231 if (value != 0.0F) {
232 accum_accum(pipe, value, xpos, ypos, width, height, acc_ps, color_ps);
233 }
234 break;
235 case GL_LOAD:
236 accum_load(pipe, value, xpos, ypos, width, height, acc_ps, color_ps);
237 break;
238 case GL_RETURN:
239 accum_return(ctx, value, xpos, ypos, width, height, acc_ps, color_ps);
240 break;
241 default:
242 assert(0);
243 }
244 }
245
246
247
248 void st_init_accum_functions(struct dd_function_table *functions)
249 {
250 functions->Accum = st_Accum;
251 }