2 * Mesa 3-D graphics library
4 * Copyright (C) 2010 LunarG Inc.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
25 * Chia-I Wu <olv@lunarg.com>
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 */
34 #include "stw_device.h"
35 #include "stw_framebuffer.h"
36 #include "stw_pixelformat.h"
38 struct stw_st_framebuffer
{
39 struct st_framebuffer_iface base
;
41 struct stw_framebuffer
*fb
;
42 struct st_visual stvis
;
44 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
45 unsigned texture_width
, texture_height
;
46 unsigned texture_mask
;
49 static inline struct stw_st_framebuffer
*
50 stw_st_framebuffer(struct st_framebuffer_iface
*stfb
)
52 return (struct stw_st_framebuffer
*) stfb
;
56 * Remove outdated textures and create the requested ones.
59 stw_st_framebuffer_validate_locked(struct st_framebuffer_iface
*stfb
,
60 unsigned width
, unsigned height
,
63 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
64 struct pipe_resource templ
;
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
);
73 memset(&templ
, 0, sizeof(templ
));
74 templ
.target
= PIPE_TEXTURE_2D
;
76 templ
.height0
= height
;
80 templ
.nr_samples
= stwfb
->stvis
.samples
;
82 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
83 enum pipe_format format
;
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
])
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
;
102 case ST_ATTACHMENT_DEPTH_STENCIL
:
103 format
= stwfb
->stvis
.depth_stencil_format
;
104 bind
= PIPE_BIND_DEPTH_STENCIL
;
107 format
= PIPE_FORMAT_NONE
;
111 if (format
!= PIPE_FORMAT_NONE
) {
112 templ
.format
= format
;
116 stw_dev
->screen
->resource_create(stw_dev
->screen
, &templ
);
120 stwfb
->texture_width
= width
;
121 stwfb
->texture_height
= height
;
122 stwfb
->texture_mask
= mask
;
126 stw_st_framebuffer_validate(struct st_context_iface
*stctx
,
127 struct st_framebuffer_iface
*stfb
,
128 const enum st_attachment_type
*statts
,
130 struct pipe_resource
**out
)
132 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
133 unsigned statt_mask
, i
;
136 for (i
= 0; i
< count
; i
++)
137 statt_mask
|= 1 << statts
[i
];
139 pipe_mutex_lock(stwfb
->fb
->mutex
);
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
;
147 for (i
= 0; i
< count
; i
++) {
149 pipe_resource_reference(&out
[i
], stwfb
->textures
[statts
[i
]]);
152 stw_framebuffer_release(stwfb
->fb
);
158 * Present an attachment of the framebuffer.
161 stw_st_framebuffer_present_locked(HDC hdc
,
162 struct st_framebuffer_iface
*stfb
,
163 enum st_attachment_type statt
)
165 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
166 struct pipe_resource
*resource
;
168 resource
= stwfb
->textures
[statt
];
170 stw_framebuffer_present_locked(hdc
, stwfb
->fb
, resource
);
177 stw_st_framebuffer_flush_front(struct st_context_iface
*stctx
,
178 struct st_framebuffer_iface
*stfb
,
179 enum st_attachment_type statt
)
181 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
185 pipe_mutex_lock(stwfb
->fb
->mutex
);
187 /* We must not cache HDCs anywhere, as they can be invalidated by the
188 * application, or screen resolution changes. */
190 hDC
= GetDC(stwfb
->fb
->hWnd
);
192 ret
= stw_st_framebuffer_present_locked(hDC
, &stwfb
->base
, statt
);
194 ReleaseDC(stwfb
->fb
->hWnd
, hDC
);
200 * Create a framebuffer interface.
202 struct st_framebuffer_iface
*
203 stw_st_create_framebuffer(struct stw_framebuffer
*fb
)
205 struct stw_st_framebuffer
*stwfb
;
207 stwfb
= CALLOC_STRUCT(stw_st_framebuffer
);
212 stwfb
->stvis
= fb
->pfi
->stvis
;
214 stwfb
->base
.visual
= &stwfb
->stvis
;
215 p_atomic_set(&stwfb
->base
.stamp
, 1);
216 stwfb
->base
.flush_front
= stw_st_framebuffer_flush_front
;
217 stwfb
->base
.validate
= stw_st_framebuffer_validate
;
223 * Destroy a framebuffer interface.
226 stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface
*stfb
)
228 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
231 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
232 pipe_resource_reference(&stwfb
->textures
[i
], NULL
);
238 * Swap the buffers of the given framebuffer.
241 stw_st_swap_framebuffer_locked(HDC hdc
, struct st_framebuffer_iface
*stfb
)
243 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
244 unsigned front
= ST_ATTACHMENT_FRONT_LEFT
, back
= ST_ATTACHMENT_BACK_LEFT
;
245 struct pipe_resource
*ptex
;
248 /* swap the textures */
249 ptex
= stwfb
->textures
[front
];
250 stwfb
->textures
[front
] = stwfb
->textures
[back
];
251 stwfb
->textures
[back
] = ptex
;
253 /* convert to mask */
257 /* swap the bits in mask */
258 mask
= stwfb
->texture_mask
& ~(front
| back
);
259 if (stwfb
->texture_mask
& front
)
261 if (stwfb
->texture_mask
& back
)
263 stwfb
->texture_mask
= mask
;
265 front
= ST_ATTACHMENT_FRONT_LEFT
;
266 return stw_st_framebuffer_present_locked(hdc
, &stwfb
->base
, front
);
271 * Return the pipe_resource that correspond to given buffer.
273 struct pipe_resource
*
274 stw_get_framebuffer_resource(struct st_framebuffer_iface
*stfb
,
275 enum st_attachment_type att
)
277 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
278 return stwfb
->textures
[att
];
283 * Create an st_api of the state tracker.
286 stw_st_create_api(void)
288 return st_gl_api_create();