2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 LunarG Inc.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
25 * Chia-I Wu <olv@lunarg.com>
28 #include "util/u_memory.h"
29 #include "util/u_inlines.h"
30 #include "state_tracker/st_gl_api.h" /* for st_gl_api_create */
33 #include "stw_device.h"
34 #include "stw_framebuffer.h"
35 #include "stw_pixelformat.h"
37 struct stw_st_framebuffer
{
38 struct st_framebuffer_iface base
;
40 struct stw_framebuffer
*fb
;
41 struct st_visual stvis
;
43 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
44 unsigned texture_width
, texture_height
;
45 unsigned texture_mask
;
48 static INLINE
struct stw_st_framebuffer
*
49 stw_st_framebuffer(struct st_framebuffer_iface
*stfb
)
51 return (struct stw_st_framebuffer
*) stfb
;
55 * Remove outdated textures and create the requested ones.
58 stw_st_framebuffer_validate_locked(struct st_framebuffer_iface
*stfb
,
59 unsigned width
, unsigned height
,
62 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
63 struct pipe_resource templ
;
66 /* remove outdated textures */
67 if (stwfb
->texture_width
!= width
|| stwfb
->texture_height
!= height
) {
68 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
69 pipe_resource_reference(&stwfb
->textures
[i
], NULL
);
72 memset(&templ
, 0, sizeof(templ
));
73 templ
.target
= PIPE_TEXTURE_2D
;
75 templ
.height0
= height
;
80 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
81 enum pipe_format format
;
84 /* the texture already exists or not requested */
85 if (stwfb
->textures
[i
] || !(mask
& (1 << i
))) {
86 /* remember the texture */
87 if (stwfb
->textures
[i
])
93 case ST_ATTACHMENT_FRONT_LEFT
:
94 case ST_ATTACHMENT_BACK_LEFT
:
95 format
= stwfb
->stvis
.color_format
;
96 bind
= PIPE_BIND_DISPLAY_TARGET
|
97 PIPE_BIND_RENDER_TARGET
;
99 case ST_ATTACHMENT_DEPTH_STENCIL
:
100 format
= stwfb
->stvis
.depth_stencil_format
;
101 bind
= PIPE_BIND_DEPTH_STENCIL
;
104 format
= PIPE_FORMAT_NONE
;
108 if (format
!= PIPE_FORMAT_NONE
) {
109 templ
.format
= format
;
113 stw_dev
->screen
->resource_create(stw_dev
->screen
, &templ
);
117 stwfb
->texture_width
= width
;
118 stwfb
->texture_height
= height
;
119 stwfb
->texture_mask
= mask
;
123 stw_st_framebuffer_validate(struct st_framebuffer_iface
*stfb
,
124 const enum st_attachment_type
*statts
,
126 struct pipe_resource
**out
)
128 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
129 unsigned statt_mask
, i
;
132 for (i
= 0; i
< count
; i
++)
133 statt_mask
|= 1 << statts
[i
];
135 pipe_mutex_lock(stwfb
->fb
->mutex
);
137 if (stwfb
->fb
->must_resize
|| (statt_mask
& ~stwfb
->texture_mask
)) {
138 stw_st_framebuffer_validate_locked(&stwfb
->base
,
139 stwfb
->fb
->width
, stwfb
->fb
->height
, statt_mask
);
140 stwfb
->fb
->must_resize
= FALSE
;
143 for (i
= 0; i
< count
; i
++) {
145 pipe_resource_reference(&out
[i
], stwfb
->textures
[statts
[i
]]);
148 stw_framebuffer_release(stwfb
->fb
);
154 * Present an attachment of the framebuffer.
157 stw_st_framebuffer_present_locked(struct st_framebuffer_iface
*stfb
,
158 enum st_attachment_type statt
)
160 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
161 struct pipe_resource
*resource
;
163 resource
= stwfb
->textures
[statt
];
165 stw_framebuffer_present_locked(stwfb
->fb
->hDC
, stwfb
->fb
, resource
);
172 stw_st_framebuffer_flush_front(struct st_framebuffer_iface
*stfb
,
173 enum st_attachment_type statt
)
175 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
177 pipe_mutex_lock(stwfb
->fb
->mutex
);
179 return stw_st_framebuffer_present_locked(&stwfb
->base
, statt
);
183 * Create a framebuffer interface.
185 struct st_framebuffer_iface
*
186 stw_st_create_framebuffer(struct stw_framebuffer
*fb
)
188 struct stw_st_framebuffer
*stwfb
;
190 stwfb
= CALLOC_STRUCT(stw_st_framebuffer
);
195 stwfb
->stvis
= fb
->pfi
->stvis
;
197 stwfb
->base
.visual
= &stwfb
->stvis
;
198 stwfb
->base
.flush_front
= stw_st_framebuffer_flush_front
;
199 stwfb
->base
.validate
= stw_st_framebuffer_validate
;
205 * Destroy a framebuffer interface.
208 stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface
*stfb
)
210 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
213 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
214 pipe_resource_reference(&stwfb
->textures
[i
], NULL
);
220 * Swap the buffers of the given framebuffer.
223 stw_st_swap_framebuffer_locked(struct st_framebuffer_iface
*stfb
)
225 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
226 unsigned front
= ST_ATTACHMENT_FRONT_LEFT
, back
= ST_ATTACHMENT_BACK_LEFT
;
227 struct pipe_resource
*ptex
;
230 /* swap the textures */
231 ptex
= stwfb
->textures
[front
];
232 stwfb
->textures
[front
] = stwfb
->textures
[back
];
233 stwfb
->textures
[back
] = ptex
;
235 /* convert to mask */
239 /* swap the bits in mask */
240 mask
= stwfb
->texture_mask
& ~(front
| back
);
241 if (stwfb
->texture_mask
& front
)
243 if (stwfb
->texture_mask
& back
)
245 stwfb
->texture_mask
= mask
;
247 front
= ST_ATTACHMENT_FRONT_LEFT
;
248 return stw_st_framebuffer_present_locked(&stwfb
->base
, front
);
252 * Create an st_api of the state tracker.
255 stw_st_create_api(void)
257 return st_gl_api_create();