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