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"
38 #include "st_cb_accum.h"
39 #include "st_cb_fbo.h"
41 #include "st_public.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 "util/u_tile.h"
49 #define UNCLAMPED_FLOAT_TO_SHORT(us, f) \
50 us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
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.
61 * Wrapper for pipe_get_tile_rgba(). Do format/cpp override to make the
62 * tile util function think the surface is 16bit/channel, even if it's not.
63 * See also: st_renderbuffer_alloc_storage()
66 acc_get_tile_rgba(struct pipe_context
*pipe
, struct pipe_surface
*acc_ps
,
67 uint x
, uint y
, uint w
, uint h
, float *p
)
69 const enum pipe_format f
= acc_ps
->format
;
70 const struct pipe_format_block b
= acc_ps
->block
;
72 acc_ps
->format
= DEFAULT_ACCUM_PIPE_FORMAT
;
73 acc_ps
->block
.size
= 8;
74 acc_ps
->block
.width
= 1;
75 acc_ps
->block
.height
= 1;
77 pipe_get_tile_rgba(acc_ps
, x
, y
, w
, h
, p
);
85 * Wrapper for pipe_put_tile_rgba(). Do format/cpp override to make the
86 * tile util function think the surface is 16bit/channel, even if it's not.
87 * See also: st_renderbuffer_alloc_storage()
90 acc_put_tile_rgba(struct pipe_context
*pipe
, struct pipe_surface
*acc_ps
,
91 uint x
, uint y
, uint w
, uint h
, const float *p
)
93 enum pipe_format f
= acc_ps
->format
;
94 const struct pipe_format_block b
= acc_ps
->block
;
96 acc_ps
->format
= DEFAULT_ACCUM_PIPE_FORMAT
;
97 acc_ps
->block
.size
= 8;
98 acc_ps
->block
.width
= 1;
99 acc_ps
->block
.height
= 1;
101 pipe_put_tile_rgba(acc_ps
, x
, y
, w
, h
, p
);
110 st_clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
112 struct st_renderbuffer
*acc_strb
= st_renderbuffer(rb
);
113 struct pipe_surface
*acc_ps
;
114 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
115 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
116 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
117 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
118 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
121 acc_ps
= screen
->get_tex_surface(screen
, acc_strb
->texture
, 0, 0, 0,
122 PIPE_BUFFER_USAGE_CPU_WRITE
);
123 map
= screen
->surface_map(screen
, acc_ps
,
124 PIPE_BUFFER_USAGE_CPU_WRITE
);
126 /* note acc_strb->format might not equal acc_ps->format */
127 switch (acc_strb
->format
) {
128 case PIPE_FORMAT_R16G16B16A16_SNORM
:
130 GLshort r
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
131 GLshort g
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
132 GLshort b
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
133 GLshort a
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
135 for (i
= 0; i
< height
; i
++) {
136 GLshort
*dst
= (GLshort
*) (map
+ (ypos
+ i
) * acc_ps
->stride
+ xpos
* 8);
137 for (j
= 0; j
< width
; j
++) {
148 _mesa_problem(ctx
, "unexpected format in st_clear_accum_buffer()");
151 screen
->surface_unmap(screen
, acc_ps
);
152 pipe_surface_reference(&acc_ps
, NULL
);
158 accum_mad(GLcontext
*ctx
, GLfloat scale
, GLfloat bias
,
159 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
160 struct st_renderbuffer
*acc_strb
)
162 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
163 struct pipe_surface
*acc_ps
= acc_strb
->surface
;
166 map
= screen
->surface_map(screen
, acc_ps
,
167 PIPE_BUFFER_USAGE_CPU_WRITE
);
169 /* note acc_strb->format might not equal acc_ps->format */
170 switch (acc_strb
->format
) {
171 case PIPE_FORMAT_R16G16B16A16_SNORM
:
174 for (i
= 0; i
< height
; i
++) {
175 GLshort
*acc
= (GLshort
*) (map
+ (ypos
+ i
) * acc_ps
->stride
+ xpos
* 8);
176 for (j
= 0; j
< width
* 4; j
++) {
177 float val
= SHORT_TO_FLOAT(acc
[j
]) * scale
+ bias
;
178 acc
[j
] = FLOAT_TO_SHORT(val
);
184 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
187 screen
->surface_unmap(screen
, acc_ps
);
192 accum_accum(struct pipe_context
*pipe
, GLfloat value
,
193 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
194 struct st_renderbuffer
*acc_strb
,
195 struct st_renderbuffer
*color_strb
)
197 struct pipe_screen
*screen
= pipe
->screen
;
198 struct pipe_surface
*acc_surf
, *color_surf
;
199 GLfloat
*colorBuf
, *accBuf
;
202 acc_surf
= screen
->get_tex_surface(screen
, acc_strb
->texture
, 0, 0, 0,
203 (PIPE_BUFFER_USAGE_CPU_WRITE
|
204 PIPE_BUFFER_USAGE_CPU_READ
));
206 color_surf
= screen
->get_tex_surface(screen
, color_strb
->texture
, 0, 0, 0,
207 PIPE_BUFFER_USAGE_CPU_READ
);
209 colorBuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
210 accBuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
212 pipe_get_tile_rgba(color_surf
, xpos
, ypos
, width
, height
, colorBuf
);
213 acc_get_tile_rgba(pipe
, acc_surf
, xpos
, ypos
, width
, height
, accBuf
);
215 for (i
= 0; i
< 4 * width
* height
; i
++) {
216 accBuf
[i
] = accBuf
[i
] + colorBuf
[i
] * value
;
219 acc_put_tile_rgba(pipe
, acc_surf
, xpos
, ypos
, width
, height
, accBuf
);
223 pipe_surface_reference(&acc_surf
, NULL
);
224 pipe_surface_reference(&color_surf
, NULL
);
229 accum_load(struct pipe_context
*pipe
, GLfloat value
,
230 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
231 struct st_renderbuffer
*acc_strb
,
232 struct st_renderbuffer
*color_strb
)
234 struct pipe_screen
*screen
= pipe
->screen
;
235 struct pipe_surface
*acc_surf
, *color_surf
;
239 acc_surf
= screen
->get_tex_surface(screen
, acc_strb
->texture
, 0, 0, 0,
240 PIPE_BUFFER_USAGE_CPU_WRITE
);
242 color_surf
= screen
->get_tex_surface(screen
, color_strb
->texture
, 0, 0, 0,
243 PIPE_BUFFER_USAGE_CPU_READ
);
245 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
247 pipe_get_tile_rgba(color_surf
, xpos
, ypos
, width
, height
, buf
);
249 for (i
= 0; i
< 4 * width
* height
; i
++) {
250 buf
[i
] = buf
[i
] * value
;
253 acc_put_tile_rgba(pipe
, acc_surf
, xpos
, ypos
, width
, height
, buf
);
256 pipe_surface_reference(&acc_surf
, NULL
);
257 pipe_surface_reference(&color_surf
, NULL
);
262 accum_return(GLcontext
*ctx
, GLfloat value
,
263 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
264 struct st_renderbuffer
*acc_strb
,
265 struct st_renderbuffer
*color_strb
)
267 struct pipe_context
*pipe
= ctx
->st
->pipe
;
268 struct pipe_screen
*screen
= pipe
->screen
;
269 const GLubyte
*colormask
= ctx
->Color
.ColorMask
;
270 struct pipe_surface
*acc_surf
, *color_surf
;
271 GLfloat
*abuf
, *cbuf
= NULL
;
274 abuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
276 acc_surf
= screen
->get_tex_surface(screen
, acc_strb
->texture
, 0, 0, 0,
277 PIPE_BUFFER_USAGE_CPU_READ
);
279 color_surf
= screen
->get_tex_surface(screen
, color_strb
->texture
, 0, 0, 0,
280 (PIPE_BUFFER_USAGE_CPU_READ
|
281 PIPE_BUFFER_USAGE_CPU_WRITE
));
283 acc_get_tile_rgba(pipe
, acc_surf
, xpos
, ypos
, width
, height
, abuf
);
285 if (!colormask
[0] || !colormask
[1] || !colormask
[2] || !colormask
[3]) {
286 cbuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
287 pipe_get_tile_rgba(color_surf
, xpos
, ypos
, width
, height
, cbuf
);
290 for (i
= 0; i
< width
* height
; i
++) {
291 for (ch
= 0; ch
< 4; ch
++) {
293 GLfloat val
= abuf
[i
* 4 + ch
] * value
;
294 abuf
[i
* 4 + ch
] = CLAMP(val
, 0.0f
, 1.0f
);
297 abuf
[i
* 4 + ch
] = cbuf
[i
* 4 + ch
];
302 pipe_put_tile_rgba(color_surf
, xpos
, ypos
, width
, height
, abuf
);
307 pipe_surface_reference(&acc_surf
, NULL
);
308 pipe_surface_reference(&color_surf
, NULL
);
313 st_Accum(GLcontext
*ctx
, GLenum op
, GLfloat value
)
315 struct st_context
*st
= ctx
->st
;
316 struct pipe_context
*pipe
= st
->pipe
;
317 struct st_renderbuffer
*acc_strb
318 = st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
319 struct st_renderbuffer
*color_strb
320 = st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
322 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
323 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
324 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
325 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
327 /* make sure color bufs aren't cached */
328 st_flush( st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
333 accum_mad(ctx
, 1.0, value
, xpos
, ypos
, width
, height
, acc_strb
);
338 accum_mad(ctx
, value
, 0.0, xpos
, ypos
, width
, height
, acc_strb
);
343 accum_accum(pipe
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
347 accum_load(pipe
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
350 accum_return(ctx
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
359 void st_init_accum_functions(struct dd_function_table
*functions
)
361 functions
->Accum
= st_Accum
;