[OPENGL]
[reactos.git] / reactos / dll / opengl / mesa / src / gallium / state_trackers / wgl / stw_st.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.9
4 *
5 * Copyright (C) 2010 LunarG Inc.
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 * 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
81 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
82 enum pipe_format format;
83 unsigned bind;
84
85 /* the texture already exists or not requested */
86 if (stwfb->textures[i] || !(mask & (1 << i))) {
87 /* remember the texture */
88 if (stwfb->textures[i])
89 mask |= (1 << i);
90 continue;
91 }
92
93 switch (i) {
94 case ST_ATTACHMENT_FRONT_LEFT:
95 case ST_ATTACHMENT_BACK_LEFT:
96 format = stwfb->stvis.color_format;
97 bind = PIPE_BIND_DISPLAY_TARGET |
98 PIPE_BIND_RENDER_TARGET;
99 break;
100 case ST_ATTACHMENT_DEPTH_STENCIL:
101 format = stwfb->stvis.depth_stencil_format;
102 bind = PIPE_BIND_DEPTH_STENCIL;
103 break;
104 default:
105 format = PIPE_FORMAT_NONE;
106 break;
107 }
108
109 if (format != PIPE_FORMAT_NONE) {
110 templ.format = format;
111 templ.bind = bind;
112
113 stwfb->textures[i] =
114 stw_dev->screen->resource_create(stw_dev->screen, &templ);
115 }
116 }
117
118 stwfb->texture_width = width;
119 stwfb->texture_height = height;
120 stwfb->texture_mask = mask;
121 }
122
123 static boolean
124 stw_st_framebuffer_validate(struct st_framebuffer_iface *stfb,
125 const enum st_attachment_type *statts,
126 unsigned count,
127 struct pipe_resource **out)
128 {
129 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
130 unsigned statt_mask, i;
131
132 statt_mask = 0x0;
133 for (i = 0; i < count; i++)
134 statt_mask |= 1 << statts[i];
135
136 pipe_mutex_lock(stwfb->fb->mutex);
137
138 if (stwfb->fb->must_resize || (statt_mask & ~stwfb->texture_mask)) {
139 stw_st_framebuffer_validate_locked(&stwfb->base,
140 stwfb->fb->width, stwfb->fb->height, statt_mask);
141 stwfb->fb->must_resize = FALSE;
142 }
143
144 for (i = 0; i < count; i++) {
145 out[i] = NULL;
146 pipe_resource_reference(&out[i], stwfb->textures[statts[i]]);
147 }
148
149 stw_framebuffer_release(stwfb->fb);
150
151 return TRUE;
152 }
153
154 /**
155 * Present an attachment of the framebuffer.
156 */
157 static boolean
158 stw_st_framebuffer_present_locked(HDC hdc,
159 struct st_framebuffer_iface *stfb,
160 enum st_attachment_type statt)
161 {
162 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
163 struct pipe_resource *resource;
164
165 resource = stwfb->textures[statt];
166 if (resource) {
167 stw_framebuffer_present_locked(hdc, stwfb->fb, resource);
168 }
169
170 return TRUE;
171 }
172
173 static boolean
174 stw_st_framebuffer_flush_front(struct st_framebuffer_iface *stfb,
175 enum st_attachment_type statt)
176 {
177 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
178
179 pipe_mutex_lock(stwfb->fb->mutex);
180
181 return stw_st_framebuffer_present_locked(stwfb->fb->hDC, &stwfb->base, statt);
182 }
183
184 /**
185 * Create a framebuffer interface.
186 */
187 struct st_framebuffer_iface *
188 stw_st_create_framebuffer(struct stw_framebuffer *fb)
189 {
190 struct stw_st_framebuffer *stwfb;
191
192 stwfb = CALLOC_STRUCT(stw_st_framebuffer);
193 if (!stwfb)
194 return NULL;
195
196 stwfb->fb = fb;
197 stwfb->stvis = fb->pfi->stvis;
198
199 stwfb->base.visual = &stwfb->stvis;
200 p_atomic_set(&stwfb->base.stamp, 1);
201 stwfb->base.flush_front = stw_st_framebuffer_flush_front;
202 stwfb->base.validate = stw_st_framebuffer_validate;
203
204 return &stwfb->base;
205 }
206
207 /**
208 * Destroy a framebuffer interface.
209 */
210 void
211 stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface *stfb)
212 {
213 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
214 int i;
215
216 for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
217 pipe_resource_reference(&stwfb->textures[i], NULL);
218
219 FREE(stwfb);
220 }
221
222 /**
223 * Swap the buffers of the given framebuffer.
224 */
225 boolean
226 stw_st_swap_framebuffer_locked(HDC hdc, struct st_framebuffer_iface *stfb)
227 {
228 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
229 unsigned front = ST_ATTACHMENT_FRONT_LEFT, back = ST_ATTACHMENT_BACK_LEFT;
230 struct pipe_resource *ptex;
231 unsigned mask;
232
233 /* swap the textures */
234 ptex = stwfb->textures[front];
235 stwfb->textures[front] = stwfb->textures[back];
236 stwfb->textures[back] = ptex;
237
238 /* convert to mask */
239 front = 1 << front;
240 back = 1 << back;
241
242 /* swap the bits in mask */
243 mask = stwfb->texture_mask & ~(front | back);
244 if (stwfb->texture_mask & front)
245 mask |= back;
246 if (stwfb->texture_mask & back)
247 mask |= front;
248 stwfb->texture_mask = mask;
249
250 front = ST_ATTACHMENT_FRONT_LEFT;
251 return stw_st_framebuffer_present_locked(hdc, &stwfb->base, front);
252 }
253
254 /**
255 * Create an st_api of the state tracker.
256 */
257 struct st_api *
258 stw_st_create_api(void)
259 {
260 return st_gl_api_create();
261 }