1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "st_context.h"
36 #include "st_cb_fbo.h"
37 #include "st_public.h"
38 #include "st_texture.h"
39 #include "pipe/p_context.h"
40 #include "cso_cache/cso_context.h"
41 #include "util/u_rect.h"
42 #include "util/u_math.h"
47 * When doing GL render to texture, we have to be sure that finalize_texture()
48 * didn't yank out the pipe_texture that we earlier created a surface for.
49 * Check for that here and create a new surface if needed.
52 update_renderbuffer_surface(struct st_context
*st
,
53 struct st_renderbuffer
*strb
)
55 struct pipe_screen
*screen
= st
->pipe
->screen
;
56 struct pipe_texture
*texture
= strb
->rtt
->pt
;
57 int rtt_width
= strb
->Base
.Width
;
58 int rtt_height
= strb
->Base
.Height
;
61 strb
->surface
->texture
!= texture
||
62 strb
->surface
->width
!= rtt_width
||
63 strb
->surface
->height
!= rtt_height
) {
65 /* find matching mipmap level size */
66 for (level
= 0; level
<= texture
->last_level
; level
++) {
67 if (u_minify(texture
->width0
, level
) == rtt_width
&&
68 u_minify(texture
->height0
, level
) == rtt_height
) {
70 pipe_surface_reference(&strb
->surface
, NULL
);
72 strb
->surface
= screen
->get_tex_surface(screen
,
77 PIPE_BUFFER_USAGE_GPU_READ
|
78 PIPE_BUFFER_USAGE_GPU_WRITE
);
80 printf("-- alloc new surface %d x %d into tex %p\n",
81 strb
->surface
->width
, strb
->surface
->height
,
92 * Update framebuffer state (color, depth, stencil, etc. buffers)
95 update_framebuffer_state( struct st_context
*st
)
97 struct pipe_framebuffer_state
*framebuffer
= &st
->state
.framebuffer
;
98 struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
99 struct st_renderbuffer
*strb
;
102 framebuffer
->width
= fb
->Width
;
103 framebuffer
->height
= fb
->Height
;
105 /*printf("------ fb size %d x %d\n", fb->Width, fb->Height);*/
107 /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
108 * to determine which surfaces to draw to
110 framebuffer
->nr_cbufs
= 0;
111 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
112 strb
= st_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
115 /*printf("--------- framebuffer surface rtt %p\n", strb->rtt);*/
117 /* rendering to a GL texture, may have to update surface */
118 update_renderbuffer_surface(st
, strb
);
122 pipe_surface_reference(&framebuffer
->cbufs
[framebuffer
->nr_cbufs
],
124 framebuffer
->nr_cbufs
++;
126 strb
->defined
= GL_TRUE
; /* we'll be drawing something */
129 for (i
= framebuffer
->nr_cbufs
; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
130 pipe_surface_reference(&framebuffer
->cbufs
[i
], NULL
);
134 * Depth/Stencil renderbuffer/surface.
136 strb
= st_renderbuffer(fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
138 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
140 /* rendering to a GL texture, may have to update surface */
141 update_renderbuffer_surface(st
, strb
);
143 pipe_surface_reference(&framebuffer
->zsbuf
, strb
->surface
);
146 strb
= st_renderbuffer(fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
148 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
149 assert(strb
->surface
);
150 pipe_surface_reference(&framebuffer
->zsbuf
, strb
->surface
);
153 pipe_surface_reference(&framebuffer
->zsbuf
, NULL
);
156 cso_set_framebuffer(st
->cso_context
, framebuffer
);
158 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
) {
159 if (st
->frontbuffer_status
== FRONT_STATUS_COPY_OF_BACK
) {
160 /* copy back color buffer to front color buffer */
161 struct st_framebuffer
*stfb
= (struct st_framebuffer
*) fb
;
162 struct pipe_surface
*surf_front
, *surf_back
;
163 (void) st_get_framebuffer_surface(stfb
, ST_SURFACE_FRONT_LEFT
, &surf_front
);
164 (void) st_get_framebuffer_surface(stfb
, ST_SURFACE_BACK_LEFT
, &surf_back
);
166 if (st
->pipe
->surface_copy
) {
167 st
->pipe
->surface_copy(st
->pipe
,
168 surf_front
, 0, 0, /* dest */
169 surf_back
, 0, 0, /* src */
170 fb
->Width
, fb
->Height
);
172 util_surface_copy(st
->pipe
, FALSE
,
175 fb
->Width
, fb
->Height
);
178 /* we're assuming we'll really draw to the front buffer */
179 st
->frontbuffer_status
= FRONT_STATUS_DIRTY
;
184 const struct st_tracked_state st_update_framebuffer
= {
185 "st_update_framebuffer", /* name */
187 _NEW_BUFFERS
, /* mesa */
188 ST_NEW_FRAMEBUFFER
, /* st */
190 update_framebuffer_state
/* update */