invert Y axis
[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 "state_tracker/st_context.h"
49
50
51 static INLINE struct xmesa_surface *
52 xmesa_surf(struct softpipe_surface *sps)
53 {
54 return (struct xmesa_surface *) sps;
55 }
56
57
58 static INLINE struct xmesa_renderbuffer *
59 xmesa_rb(struct softpipe_surface *sps)
60 {
61 struct xmesa_surface *xms = xmesa_surf(sps);
62 return xms->xrb;
63 }
64
65
66 #define FLIP(Y) Y = xrb->St.Base.Height - (Y) - 1;
67
68
69 /**
70 * quad reading/writing
71 * These functions are just wrappers around the existing renderbuffer
72 * functions.
73 * Note that Y=0 is the top of the surface.
74 */
75
76 static void
77 read_quad_f(struct softpipe_surface *sps, GLint x, GLint y,
78 GLfloat (*rgba)[NUM_CHANNELS])
79 {
80 struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
81 GLubyte temp[16];
82 GLfloat *dst = (GLfloat *) rgba;
83 GLuint i;
84 GET_CURRENT_CONTEXT(ctx);
85 FLIP(y);
86 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y, temp);
87 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y - 1, temp + 8);
88 for (i = 0; i < 16; i++) {
89 dst[i] = UBYTE_TO_FLOAT(temp[i]);
90 }
91 }
92
93 static void
94 read_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
95 GLfloat (*rrrr)[QUAD_SIZE])
96 {
97 struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
98 GLubyte temp[16];
99 GLfloat *dst = (GLfloat *) rrrr;
100 GLuint i, j;
101 GET_CURRENT_CONTEXT(ctx);
102 FLIP(y);
103 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y, temp);
104 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y - 1, temp + 8);
105 for (i = 0; i < 4; i++) {
106 for (j = 0; j < 4; j++) {
107 dst[j * 4 + i] = UBYTE_TO_FLOAT(temp[i * 4 + j]);
108 }
109 }
110 }
111
112 static void
113 write_quad_f(struct softpipe_surface *sps, GLint x, GLint y,
114 GLfloat (*rgba)[NUM_CHANNELS])
115 {
116 struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
117 GLubyte temp[16];
118 const GLfloat *src = (const GLfloat *) rgba;
119 GLuint i;
120 GET_CURRENT_CONTEXT(ctx);
121 FLIP(y);
122 for (i = 0; i < 16; i++) {
123 UNCLAMPED_FLOAT_TO_UBYTE(temp[i], src[i]);
124 }
125 xrb->St.Base.PutRow(ctx, &xrb->St.Base, 2, x, y, temp, NULL);
126 xrb->St.Base.PutRow(ctx, &xrb->St.Base, 2, x, y - 1, temp + 8, NULL);
127 }
128
129 static void
130 write_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
131 GLfloat (*rrrr)[QUAD_SIZE])
132 {
133 struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
134 GLubyte temp[16];
135 const GLfloat *src = (const GLfloat *) rrrr;
136 GLuint i, j;
137 GET_CURRENT_CONTEXT(ctx);
138 FLIP(y);
139 for (i = 0; i < 4; i++) {
140 for (j = 0; j < 4; j++) {
141 UNCLAMPED_FLOAT_TO_UBYTE(temp[j * 4 + i], src[i * 4 + j]);
142 }
143 }
144 xrb->St.Base.PutRow(ctx, &xrb->St.Base, 2, x, y, temp, NULL);
145 xrb->St.Base.PutRow(ctx, &xrb->St.Base, 2, x, y - 1, temp + 8, NULL);
146 }
147
148 static void
149 read_quad_ub(struct softpipe_surface *sps, GLint x, GLint y,
150 GLubyte (*rgba)[NUM_CHANNELS])
151 {
152 struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
153 GET_CURRENT_CONTEXT(ctx);
154 FLIP(y);
155 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y, rgba);
156 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y - 1, rgba + 2);
157 }
158
159 static void
160 write_quad_ub(struct softpipe_surface *sps, GLint x, GLint y,
161 GLubyte (*rgba)[NUM_CHANNELS])
162 {
163 struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
164 GET_CURRENT_CONTEXT(ctx);
165 FLIP(y);
166 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y, rgba);
167 xrb->St.Base.GetRow(ctx, &xrb->St.Base, 2, x, y - 1, rgba + 2);
168 }
169
170
171 /**
172 * Called to create a pipe_surface for each X renderbuffer.
173 * Note: this is being used instead of pipe->surface_alloc() since we
174 * have special/unique quad read/write functions for X.
175 */
176 struct pipe_surface *
177 xmesa_new_surface(GLcontext *ctx, struct xmesa_renderbuffer *xrb)
178 {
179 struct pipe_context *pipe = ctx->st->pipe;
180 struct softpipe_surface *sps;
181
182 sps = CALLOC_STRUCT(softpipe_surface);
183 if (!sps)
184 return NULL;
185
186 #if 0
187 sps->surface.rb = xrb; /* XXX only needed for quad funcs above */
188 #endif
189 sps->surface.width = xrb->St.Base.Width;
190 sps->surface.height = xrb->St.Base.Height;
191
192 sps->read_quad_f = read_quad_f;
193 sps->read_quad_f_swz = read_quad_f_swz;
194 sps->read_quad_ub = read_quad_ub;
195 sps->write_quad_f = write_quad_f;
196 sps->write_quad_f_swz = write_quad_f_swz;
197 sps->write_quad_ub = write_quad_ub;
198
199 /* Note, the region we allocate doesn't actually have any storage
200 * since we're drawing into an XImage or Pixmap.
201 * The region's size will get set in the xmesa_alloc_front/back_storage()
202 * functions.
203 */
204 sps->surface.region = pipe->region_alloc(pipe, 0, 0, 0);
205
206 return &sps->surface;
207 }
208
209
210 struct pipe_surface *
211 xmesa_surface_alloc(struct pipe_context *pipe, GLuint pipeFormat)
212 {
213 struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);
214
215 assert(pipeFormat);
216
217 xms->surface.surface.format = pipeFormat;
218
219 switch (pipeFormat) {
220 case PIPE_FORMAT_U_A8_R8_G8_B8:
221 xms->surface.read_quad_f_swz = read_quad_f;
222 xms->surface.read_quad_f = read_quad_f;
223 xms->surface.read_quad_f_swz = read_quad_f_swz;
224 xms->surface.read_quad_ub = read_quad_ub;
225 xms->surface.write_quad_f = write_quad_f;
226 xms->surface.write_quad_f_swz = write_quad_f_swz;
227 xms->surface.write_quad_ub = write_quad_ub;
228 break;
229 case PIPE_FORMAT_S8_Z24:
230 softpipe_init_surface_funcs(&xms->surface);
231 /*
232 xms->surface.read_quad_z = 1;
233 xms->surface.write_quad_z = 1;
234 */
235 break;
236 default:
237 abort();
238 }
239
240 return &xms->surface.surface;
241 }
242
243
244 const GLuint *
245 xmesa_supported_formats(struct pipe_context *pipe, GLuint *numFormats)
246 {
247 static const GLuint formats[] = {
248 PIPE_FORMAT_U_A8_R8_G8_B8,
249 PIPE_FORMAT_S8_Z24
250 };
251
252 *numFormats = 2;
253
254 return formats;
255 }
256