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/p_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 int cpp
= acc_ps
->cpp
;
71 acc_ps
->format
= DEFAULT_ACCUM_PIPE_FORMAT
;
74 pipe_get_tile_rgba(pipe
, acc_ps
, x
, y
, w
, h
, p
);
82 * Wrapper for pipe_put_tile_rgba(). Do format/cpp override to make the
83 * tile util function think the surface is 16bit/channel, even if it's not.
84 * See also: st_renderbuffer_alloc_storage()
87 acc_put_tile_rgba(struct pipe_context
*pipe
, struct pipe_surface
*acc_ps
,
88 uint x
, uint y
, uint w
, uint h
, const float *p
)
90 enum pipe_format f
= acc_ps
->format
;
91 const int cpp
= acc_ps
->cpp
;
93 acc_ps
->format
= DEFAULT_ACCUM_PIPE_FORMAT
;
96 pipe_put_tile_rgba(pipe
, acc_ps
, x
, y
, w
, h
, p
);
105 st_clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
107 struct st_renderbuffer
*acc_strb
= st_renderbuffer(rb
);
108 struct pipe_surface
*acc_ps
= acc_strb
->surface
;
109 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
110 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
111 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
112 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
113 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
116 map
= screen
->surface_map(screen
, acc_ps
,
117 PIPE_BUFFER_USAGE_CPU_WRITE
);
119 /* note acc_strb->format might not equal acc_ps->format */
120 switch (acc_strb
->format
) {
121 case PIPE_FORMAT_R16G16B16A16_SNORM
:
123 GLshort r
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[0]);
124 GLshort g
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[1]);
125 GLshort b
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[2]);
126 GLshort a
= FLOAT_TO_SHORT(ctx
->Accum
.ClearColor
[3]);
128 for (i
= 0; i
< height
; i
++) {
129 GLshort
*dst
= ((GLshort
*) map
130 + ((ypos
+ i
) * acc_ps
->pitch
+ xpos
) * 4);
131 for (j
= 0; j
< width
; j
++) {
142 _mesa_problem(ctx
, "unexpected format in st_clear_accum_buffer()");
145 screen
->surface_unmap(screen
, acc_ps
);
151 accum_mad(GLcontext
*ctx
, GLfloat scale
, GLfloat bias
,
152 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
153 struct st_renderbuffer
*acc_strb
)
155 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
156 struct pipe_surface
*acc_ps
= acc_strb
->surface
;
159 map
= screen
->surface_map(screen
, acc_ps
,
160 PIPE_BUFFER_USAGE_CPU_WRITE
);
162 /* note acc_strb->format might not equal acc_ps->format */
163 switch (acc_strb
->format
) {
164 case PIPE_FORMAT_R16G16B16A16_SNORM
:
167 for (i
= 0; i
< height
; i
++) {
168 GLshort
*acc
= ((GLshort
*) map
169 + ((ypos
+ i
) * acc_ps
->pitch
+ xpos
) * 4);
170 for (j
= 0; j
< width
* 4; j
++) {
171 float val
= SHORT_TO_FLOAT(acc
[j
]) * scale
+ bias
;
172 acc
[j
] = FLOAT_TO_SHORT(val
);
178 _mesa_problem(NULL
, "unexpected format in st_clear_accum_buffer()");
181 screen
->surface_unmap(screen
, acc_ps
);
186 accum_accum(struct pipe_context
*pipe
, GLfloat value
,
187 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
188 struct pipe_surface
*acc_ps
,
189 struct pipe_surface
*color_ps
)
191 GLfloat
*colorBuf
, *accBuf
;
194 colorBuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
195 accBuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
197 pipe_get_tile_rgba(pipe
, color_ps
, xpos
, ypos
, width
, height
, colorBuf
);
198 acc_get_tile_rgba(pipe
, acc_ps
, xpos
, ypos
, width
, height
, accBuf
);
200 for (i
= 0; i
< 4 * width
* height
; i
++) {
201 accBuf
[i
] = accBuf
[i
] + colorBuf
[i
] * value
;
204 acc_put_tile_rgba(pipe
, acc_ps
, xpos
, ypos
, width
, height
, accBuf
);
212 accum_load(struct pipe_context
*pipe
, GLfloat value
,
213 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
214 struct pipe_surface
*acc_ps
,
215 struct pipe_surface
*color_ps
)
220 buf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
222 pipe_get_tile_rgba(pipe
, color_ps
, xpos
, ypos
, width
, height
, buf
);
224 for (i
= 0; i
< 4 * width
* height
; i
++) {
225 buf
[i
] = buf
[i
] * value
;
228 acc_put_tile_rgba(pipe
, acc_ps
, xpos
, ypos
, width
, height
, buf
);
235 accum_return(GLcontext
*ctx
, GLfloat value
,
236 GLint xpos
, GLint ypos
, GLint width
, GLint height
,
237 struct pipe_surface
*acc_ps
,
238 struct pipe_surface
*color_ps
)
240 struct pipe_context
*pipe
= ctx
->st
->pipe
;
241 const GLubyte
*colormask
= ctx
->Color
.ColorMask
;
242 GLfloat
*abuf
, *cbuf
= NULL
;
245 abuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
247 acc_get_tile_rgba(pipe
, acc_ps
, xpos
, ypos
, width
, height
, abuf
);
249 if (!colormask
[0] || !colormask
[1] || !colormask
[2] || !colormask
[3]) {
250 cbuf
= (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
251 pipe_get_tile_rgba(pipe
, color_ps
, xpos
, ypos
, width
, height
, cbuf
);
254 for (i
= 0; i
< width
* height
; i
++) {
255 for (ch
= 0; ch
< 4; ch
++) {
257 GLfloat val
= abuf
[i
* 4 + ch
] * value
;
258 abuf
[i
* 4 + ch
] = CLAMP(val
, 0.0, 1.0);
261 abuf
[i
* 4 + ch
] = cbuf
[i
* 4 + ch
];
266 pipe_put_tile_rgba(pipe
, color_ps
, xpos
, ypos
, width
, height
, abuf
);
275 st_Accum(GLcontext
*ctx
, GLenum op
, GLfloat value
)
277 struct st_context
*st
= ctx
->st
;
278 struct pipe_context
*pipe
= st
->pipe
;
279 struct st_renderbuffer
*acc_strb
280 = st_renderbuffer(ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
281 struct st_renderbuffer
*color_strb
282 = st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
283 struct pipe_surface
*acc_ps
= acc_strb
->surface
;
284 struct pipe_surface
*color_ps
= color_strb
->surface
;
286 const GLint xpos
= ctx
->DrawBuffer
->_Xmin
;
287 const GLint ypos
= ctx
->DrawBuffer
->_Ymin
;
288 const GLint width
= ctx
->DrawBuffer
->_Xmax
- xpos
;
289 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ypos
;
291 /* make sure color bufs aren't cached */
292 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
297 accum_mad(ctx
, 1.0, value
, xpos
, ypos
, width
, height
, acc_strb
);
302 accum_mad(ctx
, value
, 0.0, xpos
, ypos
, width
, height
, acc_strb
);
307 accum_accum(pipe
, value
, xpos
, ypos
, width
, height
, acc_ps
, color_ps
);
311 accum_load(pipe
, value
, xpos
, ypos
, width
, height
, acc_ps
, color_ps
);
314 accum_return(ctx
, value
, xpos
, ypos
, width
, height
, acc_ps
, color_ps
);
323 void st_init_accum_functions(struct dd_function_table
*functions
)
325 functions
->Accum
= st_Accum
;