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_format.h"
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "pipe/p_inlines.h"
45 #include "util/u_tile.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 * Wrapper for pipe_get_tile_rgba(). Do format/cpp override to make the
61 * tile util function think the surface is 16bit/channel, even if it's not.
62 * See also: st_renderbuffer_alloc_storage()
65 acc_get_tile_rgba(struct pipe_context
*pipe
, struct pipe_surface
*acc_ps
,
66 uint x
, uint y
, uint w
, uint h
, float *p
)
68 const enum pipe_format f
= acc_ps
->format
;
69 const struct pipe_format_block b
= acc_ps
->block
;
71 acc_ps
->format
= DEFAULT_ACCUM_PIPE_FORMAT
;
72 acc_ps
->block
.size
= 8;
73 acc_ps
->block
.width
= 1;
74 acc_ps
->block
.height
= 1;
76 pipe_get_tile_rgba(acc_ps
, x
, y
, w
, h
, p
);
84 * Wrapper for pipe_put_tile_rgba(). Do format/cpp override to make the
85 * tile util function think the surface is 16bit/channel, even if it's not.
86 * See also: st_renderbuffer_alloc_storage()
89 acc_put_tile_rgba(struct pipe_context
*pipe
, struct pipe_surface
*acc_ps
,
90 uint x
, uint y
, uint w
, uint h
, const float *p
)
92 enum pipe_format f
= acc_ps
->format
;
93 const struct pipe_format_block b
= acc_ps
->block
;
95 acc_ps
->format
= DEFAULT_ACCUM_PIPE_FORMAT
;
96 acc_ps
->block
.size
= 8;
97 acc_ps
->block
.width
= 1;
98 acc_ps
->block
.height
= 1;
100 pipe_put_tile_rgba(acc_ps
, x
, y
, w
, h
, p
);
109 st_clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
111 struct st_renderbuffer
*acc_strb
= st_renderbuffer(rb
);
112 struct pipe_surface
*acc_ps
;
113 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
114 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
115 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
116 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
117 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
120 acc_ps
= screen
->get_tex_surface(screen
, acc_strb
->texture
, 0, 0, 0,
121 PIPE_BUFFER_USAGE_CPU_WRITE
);
122 map
= screen
->surface_map(screen
, acc_ps
,
123 PIPE_BUFFER_USAGE_CPU_WRITE
);
125 /* note acc_strb->format might not equal acc_ps->format */
126 switch (acc_strb
->format
) {
127 case PIPE_FORMAT_R16G16B16A16_SNORM
:
129 GLshort r
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
130 GLshort g
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
131 GLshort b
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
132 GLshort a
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
134 for (i
= 0; i
< height
; i
++) {
135 GLshort
*dst
= (GLshort
*) (map
+ (ypos
+ i
) * acc_ps
->stride
+ xpos
* 8);
136 for (j
= 0; j
< width
; j
++) {
147 _mesa_problem(ctx
, "unexpected format in st_clear_accum_buffer()");
150 screen
->surface_unmap(screen
, acc_ps
);
151 pipe_surface_reference(&acc_ps
, NULL
);
157 accum_mad(GLcontext
*ctx
, GLfloat scale
, GLfloat bias
,
158 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
159 struct st_renderbuffer
*acc_strb
)
161 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
162 struct pipe_surface
*acc_ps
= acc_strb
->surface
;
165 map
= screen
->surface_map(screen
, acc_ps
,
166 PIPE_BUFFER_USAGE_CPU_WRITE
);
168 /* note acc_strb->format might not equal acc_ps->format */
169 switch (acc_strb
->format
) {
170 case PIPE_FORMAT_R16G16B16A16_SNORM
:
173 for (i
= 0; i
< height
; i
++) {
174 GLshort
*acc
= (GLshort
*) (map
+ (ypos
+ i
) * acc_ps
->stride
+ xpos
* 8);
175 for (j
= 0; j
< width
* 4; j
++) {
176 float val
= SHORT_TO_FLOAT(acc
[j
]) * scale
+ bias
;
177 acc
[j
] = FLOAT_TO_SHORT(val
);
183 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
186 screen
->surface_unmap(screen
, acc_ps
);
191 accum_accum(struct pipe_context
*pipe
, GLfloat value
,
192 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
193 struct st_renderbuffer
*acc_strb
,
194 struct st_renderbuffer
*color_strb
)
196 struct pipe_screen
*screen
= pipe
->screen
;
197 struct pipe_surface
*acc_surf
, *color_surf
;
198 GLfloat
*colorBuf
, *accBuf
;
201 acc_surf
= screen
->get_tex_surface(screen
, acc_strb
->texture
, 0, 0, 0,
202 (PIPE_BUFFER_USAGE_CPU_WRITE
|
203 PIPE_BUFFER_USAGE_CPU_READ
));
205 color_surf
= screen
->get_tex_surface(screen
, color_strb
->texture
, 0, 0, 0,
206 PIPE_BUFFER_USAGE_CPU_READ
);
208 colorBuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
209 accBuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
211 pipe_get_tile_rgba(color_surf
, xpos
, ypos
, width
, height
, colorBuf
);
212 acc_get_tile_rgba(pipe
, acc_surf
, xpos
, ypos
, width
, height
, accBuf
);
214 for (i
= 0; i
< 4 * width
* height
; i
++) {
215 accBuf
[i
] = accBuf
[i
] + colorBuf
[i
] * value
;
218 acc_put_tile_rgba(pipe
, acc_surf
, xpos
, ypos
, width
, height
, accBuf
);
222 pipe_surface_reference(&acc_surf
, NULL
);
223 pipe_surface_reference(&color_surf
, NULL
);
228 accum_load(struct pipe_context
*pipe
, GLfloat value
,
229 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
230 struct st_renderbuffer
*acc_strb
,
231 struct st_renderbuffer
*color_strb
)
233 struct pipe_screen
*screen
= pipe
->screen
;
234 struct pipe_surface
*acc_surf
, *color_surf
;
238 acc_surf
= screen
->get_tex_surface(screen
, acc_strb
->texture
, 0, 0, 0,
239 PIPE_BUFFER_USAGE_CPU_WRITE
);
241 color_surf
= screen
->get_tex_surface(screen
, color_strb
->texture
, 0, 0, 0,
242 PIPE_BUFFER_USAGE_CPU_READ
);
244 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
246 pipe_get_tile_rgba(color_surf
, xpos
, ypos
, width
, height
, buf
);
248 for (i
= 0; i
< 4 * width
* height
; i
++) {
249 buf
[i
] = buf
[i
] * value
;
252 acc_put_tile_rgba(pipe
, acc_surf
, xpos
, ypos
, width
, height
, buf
);
255 pipe_surface_reference(&acc_surf
, NULL
);
256 pipe_surface_reference(&color_surf
, NULL
);
261 accum_return(GLcontext
*ctx
, GLfloat value
,
262 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
263 struct st_renderbuffer
*acc_strb
,
264 struct st_renderbuffer
*color_strb
)
266 struct pipe_context
*pipe
= ctx
->st
->pipe
;
267 struct pipe_screen
*screen
= pipe
->screen
;
268 const GLubyte
*colormask
= ctx
->Color
.ColorMask
;
269 struct pipe_surface
*acc_surf
, *color_surf
;
270 GLfloat
*abuf
, *cbuf
= NULL
;
273 abuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
275 acc_surf
= screen
->get_tex_surface(screen
, acc_strb
->texture
, 0, 0, 0,
276 PIPE_BUFFER_USAGE_CPU_READ
);
278 color_surf
= screen
->get_tex_surface(screen
, color_strb
->texture
, 0, 0, 0,
279 (PIPE_BUFFER_USAGE_CPU_READ
|
280 PIPE_BUFFER_USAGE_CPU_WRITE
));
282 acc_get_tile_rgba(pipe
, acc_surf
, xpos
, ypos
, width
, height
, abuf
);
284 if (!colormask
[0] || !colormask
[1] || !colormask
[2] || !colormask
[3]) {
285 cbuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
286 pipe_get_tile_rgba(color_surf
, xpos
, ypos
, width
, height
, cbuf
);
289 for (i
= 0; i
< width
* height
; i
++) {
290 for (ch
= 0; ch
< 4; ch
++) {
292 GLfloat val
= abuf
[i
* 4 + ch
] * value
;
293 abuf
[i
* 4 + ch
] = CLAMP(val
, 0.0f
, 1.0f
);
296 abuf
[i
* 4 + ch
] = cbuf
[i
* 4 + ch
];
301 pipe_put_tile_rgba(color_surf
, xpos
, ypos
, width
, height
, abuf
);
306 pipe_surface_reference(&acc_surf
, NULL
);
307 pipe_surface_reference(&color_surf
, NULL
);
312 st_Accum(GLcontext
*ctx
, GLenum op
, GLfloat value
)
314 struct st_context
*st
= ctx
->st
;
315 struct pipe_context
*pipe
= st
->pipe
;
316 struct st_renderbuffer
*acc_strb
317 = st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
318 struct st_renderbuffer
*color_strb
319 = st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
321 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
322 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
323 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
324 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
326 /* make sure color bufs aren't cached */
327 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
332 accum_mad(ctx
, 1.0, value
, xpos
, ypos
, width
, height
, acc_strb
);
337 accum_mad(ctx
, value
, 0.0, xpos
, ypos
, width
, height
, acc_strb
);
342 accum_accum(pipe
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
346 accum_load(pipe
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
349 accum_return(ctx
, value
, xpos
, ypos
, width
, height
, acc_strb
, color_strb
);
358 void st_init_accum_functions(struct dd_function_table
*functions
)
360 functions
->Accum
= st_Accum
;