ecf4cfcc8437c7b6b677f85545fa852e2d48f5c6
[mesa.git] / src / gallium / state_trackers / wgl / stw_st.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2010 LunarG Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "util/u_memory.h"
29 #include "util/u_inlines.h"
30 #include "util/u_atomic.h"
31 #include "state_tracker/st_gl_api.h" /* for st_gl_api_create */
32
33 #include "stw_st.h"
34 #include "stw_device.h"
35 #include "stw_framebuffer.h"
36 #include "stw_pixelformat.h"
37
38 struct stw_st_framebuffer {
39 struct st_framebuffer_iface base;
40
41 struct stw_framebuffer *fb;
42 struct st_visual stvis;
43
44 struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
45 unsigned texture_width, texture_height;
46 unsigned texture_mask;
47 };
48
49 static inline struct stw_st_framebuffer *
50 stw_st_framebuffer(struct st_framebuffer_iface *stfb)
51 {
52 return (struct stw_st_framebuffer *) stfb;
53 }
54
55 /**
56 * Remove outdated textures and create the requested ones.
57 */
58 static void
59 stw_st_framebuffer_validate_locked(struct st_framebuffer_iface *stfb,
60 unsigned width, unsigned height,
61 unsigned mask)
62 {
63 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
64 struct pipe_resource templ;
65 unsigned i;
66
67 /* remove outdated textures */
68 if (stwfb->texture_width != width || stwfb->texture_height != height) {
69 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
70 pipe_resource_reference(&stwfb->textures[i], NULL);
71 }
72
73 memset(&templ, 0, sizeof(templ));
74 templ.target = PIPE_TEXTURE_2D;
75 templ.width0 = width;
76 templ.height0 = height;
77 templ.depth0 = 1;
78 templ.array_size = 1;
79 templ.last_level = 0;
80 templ.nr_samples = stwfb->stvis.samples;
81
82 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
83 enum pipe_format format;
84 unsigned bind;
85
86 /* the texture already exists or not requested */
87 if (stwfb->textures[i] || !(mask & (1 << i))) {
88 /* remember the texture */
89 if (stwfb->textures[i])
90 mask |= (1 << i);
91 continue;
92 }
93
94 switch (i) {
95 case ST_ATTACHMENT_FRONT_LEFT:
96 case ST_ATTACHMENT_BACK_LEFT:
97 format = stwfb->stvis.color_format;
98 bind = PIPE_BIND_DISPLAY_TARGET |
99 PIPE_BIND_SAMPLER_VIEW |
100 PIPE_BIND_RENDER_TARGET;
101 break;
102 case ST_ATTACHMENT_DEPTH_STENCIL:
103 format = stwfb->stvis.depth_stencil_format;
104 bind = PIPE_BIND_DEPTH_STENCIL;
105 break;
106 default:
107 format = PIPE_FORMAT_NONE;
108 break;
109 }
110
111 if (format != PIPE_FORMAT_NONE) {
112 templ.format = format;
113 templ.bind = bind;
114
115 stwfb->textures[i] =
116 stw_dev->screen->resource_create(stw_dev->screen, &templ);
117 }
118 }
119
120 stwfb->texture_width = width;
121 stwfb->texture_height = height;
122 stwfb->texture_mask = mask;
123 }
124
125 static boolean
126 stw_st_framebuffer_validate(struct st_context_iface *stctx,
127 struct st_framebuffer_iface *stfb,
128 const enum st_attachment_type *statts,
129 unsigned count,
130 struct pipe_resource **out)
131 {
132 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
133 unsigned statt_mask, i;
134
135 statt_mask = 0x0;
136 for (i = 0; i < count; i++)
137 statt_mask |= 1 << statts[i];
138
139 stw_framebuffer_lock(stwfb->fb);
140
141 if (stwfb->fb->must_resize || (statt_mask & ~stwfb->texture_mask)) {
142 stw_st_framebuffer_validate_locked(&stwfb->base,
143 stwfb->fb->width, stwfb->fb->height, statt_mask);
144 stwfb->fb->must_resize = FALSE;
145 }
146
147 for (i = 0; i < count; i++) {
148 out[i] = NULL;
149 pipe_resource_reference(&out[i], stwfb->textures[statts[i]]);
150 }
151
152 stw_framebuffer_unlock(stwfb->fb);
153
154 return TRUE;
155 }
156
157 /**
158 * Present an attachment of the framebuffer.
159 */
160 static boolean
161 stw_st_framebuffer_present_locked(HDC hdc,
162 struct st_framebuffer_iface *stfb,
163 enum st_attachment_type statt)
164 {
165 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
166 struct pipe_resource *resource;
167
168 resource = stwfb->textures[statt];
169 if (resource) {
170 stw_framebuffer_present_locked(hdc, stwfb->fb, resource);
171 }
172 else {
173 stw_framebuffer_unlock(stwfb->fb);
174 }
175
176 return TRUE;
177 }
178
179 static boolean
180 stw_st_framebuffer_flush_front(struct st_context_iface *stctx,
181 struct st_framebuffer_iface *stfb,
182 enum st_attachment_type statt)
183 {
184 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
185 boolean ret;
186 HDC hDC;
187
188 stw_framebuffer_lock(stwfb->fb);
189
190 /* We must not cache HDCs anywhere, as they can be invalidated by the
191 * application, or screen resolution changes. */
192
193 hDC = GetDC(stwfb->fb->hWnd);
194
195 ret = stw_st_framebuffer_present_locked(hDC, &stwfb->base, statt);
196
197 ReleaseDC(stwfb->fb->hWnd, hDC);
198
199 return ret;
200 }
201
202 /**
203 * Create a framebuffer interface.
204 */
205 struct st_framebuffer_iface *
206 stw_st_create_framebuffer(struct stw_framebuffer *fb)
207 {
208 struct stw_st_framebuffer *stwfb;
209
210 stwfb = CALLOC_STRUCT(stw_st_framebuffer);
211 if (!stwfb)
212 return NULL;
213
214 stwfb->fb = fb;
215 stwfb->stvis = fb->pfi->stvis;
216
217 stwfb->base.visual = &stwfb->stvis;
218 p_atomic_set(&stwfb->base.stamp, 1);
219 stwfb->base.flush_front = stw_st_framebuffer_flush_front;
220 stwfb->base.validate = stw_st_framebuffer_validate;
221
222 return &stwfb->base;
223 }
224
225 /**
226 * Destroy a framebuffer interface.
227 */
228 void
229 stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface *stfb)
230 {
231 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
232 int i;
233
234 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
235 pipe_resource_reference(&stwfb->textures[i], NULL);
236
237 FREE(stwfb);
238 }
239
240 /**
241 * Swap the buffers of the given framebuffer.
242 */
243 boolean
244 stw_st_swap_framebuffer_locked(HDC hdc, struct st_framebuffer_iface *stfb)
245 {
246 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
247 unsigned front = ST_ATTACHMENT_FRONT_LEFT, back = ST_ATTACHMENT_BACK_LEFT;
248 struct pipe_resource *ptex;
249 unsigned mask;
250
251 /* swap the textures */
252 ptex = stwfb->textures[front];
253 stwfb->textures[front] = stwfb->textures[back];
254 stwfb->textures[back] = ptex;
255
256 /* convert to mask */
257 front = 1 << front;
258 back = 1 << back;
259
260 /* swap the bits in mask */
261 mask = stwfb->texture_mask & ~(front | back);
262 if (stwfb->texture_mask & front)
263 mask |= back;
264 if (stwfb->texture_mask & back)
265 mask |= front;
266 stwfb->texture_mask = mask;
267
268 front = ST_ATTACHMENT_FRONT_LEFT;
269 return stw_st_framebuffer_present_locked(hdc, &stwfb->base, front);
270 }
271
272
273 /**
274 * Return the pipe_resource that correspond to given buffer.
275 */
276 struct pipe_resource *
277 stw_get_framebuffer_resource(struct st_framebuffer_iface *stfb,
278 enum st_attachment_type att)
279 {
280 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
281 return stwfb->textures[att];
282 }
283
284
285 /**
286 * Create an st_api of the state tracker.
287 */
288 struct st_api *
289 stw_st_create_api(void)
290 {
291 return st_gl_api_create();
292 }