1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
37 #include "st_context.h"
39 #include "st_cb_accum.h"
40 #include "st_cb_fbo.h"
42 #include "st_format.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "pipe/p_inlines.h"
48 #define UNCLAMPED_FLOAT_TO_SHORT(us, f) \
49 us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
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.
60 st_clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
62 struct st_renderbuffer
*acc_strb
= st_renderbuffer(rb
);
63 struct pipe_surface
*acc_ps
= acc_strb
->surface
;
64 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
65 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
66 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
67 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
68 const GLfloat r
= ctx
->Accum
.ClearColor
[0];
69 const GLfloat g
= ctx
->Accum
.ClearColor
[1];
70 const GLfloat b
= ctx
->Accum
.ClearColor
[2];
71 const GLfloat a
= ctx
->Accum
.ClearColor
[3];
73 (void) pipe_surface_map(acc_ps
);
75 switch (acc_ps
->format
) {
76 case PIPE_FORMAT_R16G16B16A16_SNORM
:
78 const short sr
= (short) (32767 * r
);
79 const short sg
= (short) (32767 * g
);
80 const short sb
= (short) (32767 * b
);
81 const short sa
= (short) (32767 * a
);
82 short *acc
= ((short *) acc_ps
->map
)
83 + (ypos
* acc_ps
->pitch
+ xpos
) * 4;
85 for (i
= 0; i
< height
; i
++) {
86 for (j
= 0; j
< width
; j
++) {
92 acc
+= acc_ps
->pitch
* 4;
100 pipe_surface_unmap(acc_ps
);
104 /** Get block of values from accum buffer, converting to float */
106 get_accum_tile(struct pipe_context
*pipe
,
107 struct pipe_surface
*acc_surf
,
108 int xpos
, int ypos
, int width
, int height
,
111 switch (acc_surf
->format
) {
112 case PIPE_FORMAT_R16G16B16A16_SNORM
:
114 const short *acc
= ((const short *) acc_surf
->map
)
115 + (ypos
* acc_surf
->pitch
+ xpos
) * 4;
117 for (i
= 0; i
< height
; i
++) {
118 for (j
= 0; j
< width
; j
++) {
119 buf
[j
*4+0] = SHORT_TO_FLOAT(acc
[j
*4+0]);
120 buf
[j
*4+1] = SHORT_TO_FLOAT(acc
[j
*4+1]);
121 buf
[j
*4+2] = SHORT_TO_FLOAT(acc
[j
*4+2]);
122 buf
[j
*4+3] = SHORT_TO_FLOAT(acc
[j
*4+3]);
124 acc
+= acc_surf
->pitch
* 4;
135 /** Put block of values into accum buffer, converting from float */
137 put_accum_tile(struct pipe_context
*pipe
,
138 struct pipe_surface
*acc_surf
,
139 int xpos
, int ypos
, int width
, int height
,
142 switch (acc_surf
->format
) {
143 case PIPE_FORMAT_R16G16B16A16_SNORM
:
145 short *acc
= ((short *) acc_surf
->map
)
146 + (ypos
* acc_surf
->pitch
+ xpos
) * 4;
148 for (i
= 0; i
< height
; i
++) {
149 for (j
= 0; j
< width
; j
++) {
151 UNCLAMPED_FLOAT_TO_SHORT(r
, buf
[j
*4+0]);
152 UNCLAMPED_FLOAT_TO_SHORT(g
, buf
[j
*4+1]);
153 UNCLAMPED_FLOAT_TO_SHORT(b
, buf
[j
*4+2]);
154 UNCLAMPED_FLOAT_TO_SHORT(a
, buf
[j
*4+3]);
160 acc
+= acc_surf
->pitch
* 4;
173 accum_mad(struct pipe_context
*pipe
, GLfloat scale
, GLfloat bias
,
174 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
175 struct pipe_surface
*acc_ps
)
180 accBuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
182 (void) pipe_surface_map(acc_ps
);
184 get_accum_tile(pipe
, acc_ps
, xpos
, ypos
, width
, height
, accBuf
);
186 for (i
= 0; i
< 4 * width
* height
; i
++) {
187 accBuf
[i
] = accBuf
[i
] * scale
+ bias
;
190 put_accum_tile(pipe
, acc_ps
, xpos
, ypos
, width
, height
, accBuf
);
194 pipe_surface_unmap(acc_ps
);
199 accum_accum(struct pipe_context
*pipe
, GLfloat value
,
200 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
201 struct pipe_surface
*acc_ps
,
202 struct pipe_surface
*color_ps
)
204 ubyte
*colorMap
, *accMap
;
205 GLfloat
*colorBuf
, *accBuf
;
208 colorBuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
209 accBuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
211 colorMap
= pipe_surface_map(color_ps
);
212 accMap
= pipe_surface_map(acc_ps
);
214 pipe
->get_tile_rgba(pipe
, color_ps
, xpos
, ypos
, width
, height
, colorBuf
);
215 get_accum_tile(pipe
, acc_ps
, xpos
, ypos
, width
, height
, accBuf
);
217 for (i
= 0; i
< 4 * width
* height
; i
++) {
218 accBuf
[i
] = accBuf
[i
] + colorBuf
[i
] * value
;
221 put_accum_tile(pipe
, acc_ps
, xpos
, ypos
, width
, height
, accBuf
);
226 pipe_surface_unmap(color_ps
);
227 pipe_surface_unmap(acc_ps
);
232 accum_load(struct pipe_context
*pipe
, GLfloat value
,
233 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
234 struct pipe_surface
*acc_ps
,
235 struct pipe_surface
*color_ps
)
240 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
242 (void) pipe_surface_map(color_ps
);
243 (void) pipe_surface_map(acc_ps
);
245 pipe
->get_tile_rgba(pipe
, color_ps
, xpos
, ypos
, width
, height
, buf
);
247 for (i
= 0; i
< 4 * width
* height
; i
++) {
248 buf
[i
] = buf
[i
] * value
;
251 put_accum_tile(pipe
, acc_ps
, xpos
, ypos
, width
, height
, buf
);
255 pipe_surface_unmap(color_ps
);
256 pipe_surface_unmap(acc_ps
);
261 accum_return(GLcontext
*ctx
, GLfloat value
,
262 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
263 struct pipe_surface
*acc_ps
,
264 struct pipe_surface
*color_ps
)
266 struct pipe_context
*pipe
= ctx
->st
->pipe
;
267 const GLubyte
*colormask
= ctx
->Color
.ColorMask
;
268 GLfloat
*abuf
, *cbuf
= NULL
;
271 abuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
273 (void) pipe_surface_map(color_ps
);
274 (void) pipe_surface_map(acc_ps
);
276 get_accum_tile(pipe
, acc_ps
, xpos
, ypos
, width
, height
, abuf
);
278 if (!colormask
[0] || !colormask
[1] || !colormask
[2] || !colormask
[3]) {
279 cbuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
280 pipe
->get_tile_rgba(pipe
, color_ps
, xpos
, ypos
, width
, height
, cbuf
);
283 for (i
= 0; i
< width
* height
; i
++) {
284 for (ch
= 0; ch
< 4; ch
++) {
286 GLfloat val
= abuf
[i
* 4 + ch
] * value
;
287 abuf
[i
* 4 + ch
] = CLAMP(val
, 0.0, 1.0);
290 abuf
[i
* 4 + ch
] = cbuf
[i
* 4 + ch
];
295 pipe
->put_tile_rgba(pipe
, color_ps
, xpos
, ypos
, width
, height
, abuf
);
301 pipe_surface_unmap(color_ps
);
302 pipe_surface_unmap(acc_ps
);
307 st_Accum(GLcontext
*ctx
, GLenum op
, GLfloat value
)
309 struct st_context
*st
= ctx
->st
;
310 struct pipe_context
*pipe
= st
->pipe
;
311 struct st_renderbuffer
*acc_strb
312 = st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
313 struct st_renderbuffer
*color_strb
314 = st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
315 struct pipe_surface
*acc_ps
= acc_strb
->surface
;
316 struct pipe_surface
*color_ps
= color_strb
->surface
;
318 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
319 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
320 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
321 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
323 /* make sure color bufs aren't cached */
324 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
329 accum_mad(pipe
, 1.0, value
, xpos
, ypos
, width
, height
, acc_ps
);
334 accum_mad(pipe
, value
, 0.0, xpos
, ypos
, width
, height
, acc_ps
);
339 accum_accum(pipe
, value
, xpos
, ypos
, width
, height
, acc_ps
, color_ps
);
343 accum_load(pipe
, value
, xpos
, ypos
, width
, height
, acc_ps
, color_ps
);
346 accum_return(ctx
, value
, xpos
, ypos
, width
, height
, acc_ps
, color_ps
);
355 void st_init_accum_functions(struct dd_function_table
*functions
)
357 functions
->Accum
= st_Accum
;