clamp/clip in put_tile(), move softpipe_init_surface_funcs() call
[mesa.git] / src / mesa / drivers / x11 / xm_surface.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file xm_surface.c
28 * Code to allow the softpipe code to write to X windows/buffers.
29 * This is a bit of a hack for now. We've basically got two different
30 * abstractions for color buffers: gl_renderbuffer and softpipe_surface.
31 * They'll need to get merged someday...
32 * For now, they're separate things that point to each other.
33 */
34
35
36 #include "glxheader.h"
37 #include "GL/xmesa.h"
38 #include "xmesaP.h"
39 #include "context.h"
40 #include "imports.h"
41 #include "macros.h"
42 #include "framebuffer.h"
43 #include "renderbuffer.h"
44
45 #include "pipe/p_context.h"
46 #include "pipe/p_defines.h"
47 #include "pipe/softpipe/sp_context.h"
48 #include "pipe/softpipe/sp_clear.h"
49 #include "state_tracker/st_context.h"
50
51
52 #define CLIP_TILE \
53 do { \
54 if (x + w > ps->width) \
55 w = ps->width - x; \
56 if (y + h > ps->height) \
57 h = ps->height -y; \
58 } while(0)
59
60
61 static INLINE struct xmesa_surface *
62 xmesa_surf(struct softpipe_surface *sps)
63 {
64 return (struct xmesa_surface *) sps;
65 }
66
67
68 static INLINE struct xmesa_renderbuffer *
69 xmesa_rb(struct softpipe_surface *sps)
70 {
71 struct xmesa_surface *xms = xmesa_surf(sps);
72 return xms->xrb;
73 }
74
75
76 #define FLIP(Y) Y = xrb->St.Base.Height - (Y) - 1;
77
78
79 /**
80 * quad reading/writing
81 * These functions are just wrappers around the existing renderbuffer
82 * functions.
83 * Note that Y=0 is the top of the surface.
84 */
85
86 static void
87 read_quad_f(struct softpipe_surface *sps, GLint x, GLint y,
88 GLfloat (*rgba)[NUM_CHANNELS])
89 {
90 struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
91 GLubyte temp[16];
92 GLfloat *dst = (GLfloat *) rgba;
93 GLuint i;
94 GET_CURRENT_CONTEXT(ctx);
95 FLIP(y);
96 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y, temp);
97 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y - 1, temp + 8);
98 for (i = 0; i < 16; i++) {
99 dst[i] = UBYTE_TO_FLOAT(temp[i]);
100 }
101 }
102
103 static void
104 read_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
105 GLfloat (*rrrr)[QUAD_SIZE])
106 {
107 struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
108 GLubyte temp[16];
109 GLfloat *dst = (GLfloat *) rrrr;
110 GLuint i, j;
111 GET_CURRENT_CONTEXT(ctx);
112 FLIP(y);
113 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y, temp);
114 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y - 1, temp + 8);
115 for (i = 0; i < 4; i++) {
116 for (j = 0; j < 4; j++) {
117 dst[j * 4 + i] = UBYTE_TO_FLOAT(temp[i * 4 + j]);
118 }
119 }
120 }
121
122 static void
123 write_quad_f(struct softpipe_surface *sps, GLint x, GLint y,
124 GLfloat (*rgba)[NUM_CHANNELS])
125 {
126 struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
127 GLubyte temp[16];
128 const GLfloat *src = (const GLfloat *) rgba;
129 GLuint i;
130 GET_CURRENT_CONTEXT(ctx);
131 FLIP(y);
132 for (i = 0; i < 16; i++) {
133 UNCLAMPED_FLOAT_TO_UBYTE(temp[i], src[i]);
134 }
135 xrb->St.Base.PutRow(ctx, &xrb->St.Base, 2, x, y, temp, NULL);
136 xrb->St.Base.PutRow(ctx, &xrb->St.Base, 2, x, y - 1, temp + 8, NULL);
137 }
138
139 static void
140 write_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
141 GLfloat (*rrrr)[QUAD_SIZE])
142 {
143 struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
144 GLubyte temp[16];
145 const GLfloat *src = (const GLfloat *) rrrr;
146 GLuint i, j;
147 GET_CURRENT_CONTEXT(ctx);
148 FLIP(y);
149 for (i = 0; i < 4; i++) {
150 for (j = 0; j < 4; j++) {
151 UNCLAMPED_FLOAT_TO_UBYTE(temp[j * 4 + i], src[i * 4 + j]);
152 }
153 }
154 xrb->St.Base.PutRow(ctx, &xrb->St.Base, 2, x, y, temp, NULL);
155 xrb->St.Base.PutRow(ctx, &xrb->St.Base, 2, x, y - 1, temp + 8, NULL);
156 }
157
158 static void
159 read_quad_ub(struct softpipe_surface *sps, GLint x, GLint y,
160 GLubyte (*rgba)[NUM_CHANNELS])
161 {
162 struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
163 GET_CURRENT_CONTEXT(ctx);
164 FLIP(y);
165 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y, rgba);
166 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y - 1, rgba + 2);
167 }
168
169 static void
170 write_quad_ub(struct softpipe_surface *sps, GLint x, GLint y,
171 GLubyte (*rgba)[NUM_CHANNELS])
172 {
173 struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
174 GET_CURRENT_CONTEXT(ctx);
175 FLIP(y);
176 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y, rgba);
177 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y - 1, rgba + 2);
178 }
179
180
181 static void
182 get_tile(struct pipe_surface *ps,
183 GLuint x, GLuint y, GLuint w, GLuint h, GLfloat *p)
184 {
185 struct xmesa_renderbuffer *xrb = xmesa_rb((struct softpipe_surface *) ps);
186 GLubyte tmp[MAX_WIDTH * 4];
187 GLuint i, j;
188 unsigned w0 = w;
189 GET_CURRENT_CONTEXT(ctx);
190
191 CLIP_TILE;
192
193 FLIP(y);
194 for (i = 0; i < h; i++) {
195 xrb->St.Base.GetRow(ctx, &xrb->St.Base, w, x, y - i, tmp);
196 for (j = 0; j < w * 4; j++) {
197 p[j] = UBYTE_TO_FLOAT(tmp[j]);
198 }
199 p += w0 * 4;
200 }
201 }
202
203
204 static void
205 put_tile(struct pipe_surface *ps,
206 GLuint x, GLuint y, GLuint w, GLuint h, const GLfloat *p)
207 {
208 struct xmesa_renderbuffer *xrb = xmesa_rb((struct softpipe_surface *) ps);
209 GLubyte tmp[MAX_WIDTH * 4];
210 GLuint i, j;
211 unsigned w0 = w;
212 GET_CURRENT_CONTEXT(ctx);
213 CLIP_TILE;
214 FLIP(y);
215 for (i = 0; i < h; i++) {
216 for (j = 0; j < w * 4; j++) {
217 UNCLAMPED_FLOAT_TO_UBYTE(tmp[j], p[j]);
218 }
219 xrb->St.Base.PutRow(ctx, &xrb->St.Base, w, x, y - i, tmp, NULL);
220 p += w0 * 4;
221 }
222 }
223
224
225
226 /**
227 * Called to create a pipe_surface for each X renderbuffer.
228 * Note: this is being used instead of pipe->surface_alloc() since we
229 * have special/unique quad read/write functions for X.
230 */
231 struct pipe_surface *
232 xmesa_new_color_surface(struct pipe_context *pipe, GLuint pipeFormat)
233 {
234 struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);
235
236 assert(pipeFormat);
237
238 xms->surface.surface.format = pipeFormat;
239 xms->surface.surface.refcount = 1;
240
241 /* some of the functions plugged in by this call will get overridden */
242 softpipe_init_surface_funcs(&xms->surface);
243
244 switch (pipeFormat) {
245 case PIPE_FORMAT_U_A8_R8_G8_B8:
246 xms->surface.read_quad_f_swz = read_quad_f;
247 xms->surface.read_quad_f = read_quad_f;
248 xms->surface.read_quad_f_swz = read_quad_f_swz;
249 xms->surface.read_quad_ub = read_quad_ub;
250 xms->surface.write_quad_f = write_quad_f;
251 xms->surface.write_quad_f_swz = write_quad_f_swz;
252 xms->surface.write_quad_ub = write_quad_ub;
253 xms->surface.surface.get_tile = get_tile;
254 xms->surface.surface.put_tile = put_tile;
255 break;
256 case PIPE_FORMAT_S8_Z24:
257 /*
258 xms->surface.read_quad_z = 1;
259 xms->surface.write_quad_z = 1;
260 */
261 break;
262 default:
263 abort();
264 }
265
266 /* Note, the region we allocate doesn't actually have any storage
267 * since we're drawing into an XImage or Pixmap.
268 * The region's size will get set in the xmesa_alloc_front/back_storage()
269 * functions.
270 */
271 if (pipe)
272 xms->surface.surface.region = pipe->region_alloc(pipe, 1, 0, 0, 0x0);
273
274 return &xms->surface.surface;
275 }
276
277
278 /**
279 * Called via pipe->surface_alloc() to create new surfaces (textures,
280 * renderbuffers, etc.
281 */
282 struct pipe_surface *
283 xmesa_surface_alloc(struct pipe_context *pipe, GLuint pipeFormat)
284 {
285 struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);
286
287 assert(pipeFormat);
288
289 xms->surface.surface.format = pipeFormat;
290 xms->surface.surface.refcount = 1;
291 /*
292 * This is really just a softpipe surface, not an XImage/Pixmap surface.
293 */
294 softpipe_init_surface_funcs(&xms->surface);
295
296 assert(pipe);
297 xms->surface.surface.region = pipe->region_alloc(pipe, 1, 1, 1, 0x0);
298
299 return &xms->surface.surface;
300 }
301
302
303 const GLuint *
304 xmesa_supported_formats(struct pipe_context *pipe, GLuint *numFormats)
305 {
306 static const GLuint formats[] = {
307 PIPE_FORMAT_U_A8_R8_G8_B8,
308 PIPE_FORMAT_S_R16_G16_B16_A16,
309 PIPE_FORMAT_S8_Z24
310 };
311
312 *numFormats = sizeof(formats) / sizeof(formats[0]);
313
314 return formats;
315 }
316
317
318 /**
319 * Called via pipe->clear()
320 */
321 void
322 xmesa_clear(struct pipe_context *pipe, struct pipe_surface *ps, GLuint value)
323 {
324 struct xmesa_renderbuffer *xrb = xmesa_rb((struct softpipe_surface *) ps);
325
326 if (xrb && xrb->ximage) {
327 /* clearing back color buffer */
328 GET_CURRENT_CONTEXT(ctx);
329 xmesa_clear_buffers(ctx, BUFFER_BIT_BACK_LEFT);
330 }
331 else if (xrb && xrb->pixmap) {
332 /* clearing front color buffer */
333 GET_CURRENT_CONTEXT(ctx);
334 xmesa_clear_buffers(ctx, BUFFER_BIT_FRONT_LEFT);
335 }
336 else {
337 /* clearing other buffer */
338 softpipe_clear(pipe, ps, value);
339 }
340 }
341