gallium: include scissor.h
[mesa.git] / src / mesa / state_tracker / st_framebuffer.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 #include "main/imports.h"
30 #include "main/buffers.h"
31 #include "main/context.h"
32 #include "main/framebuffer.h"
33 #include "main/matrix.h"
34 #include "main/renderbuffer.h"
35 #include "main/scissor.h"
36 #include "st_public.h"
37 #include "st_context.h"
38 #include "st_cb_fbo.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_context.h"
41
42
43 struct st_framebuffer *
44 st_create_framebuffer( const __GLcontextModes *visual,
45 enum pipe_format colorFormat,
46 enum pipe_format depthFormat,
47 enum pipe_format stencilFormat,
48 uint width, uint height,
49 void *private)
50 {
51 struct st_framebuffer *stfb = CALLOC_STRUCT(st_framebuffer);
52 if (stfb) {
53 _mesa_initialize_framebuffer(&stfb->Base, visual);
54
55 {
56 /* fake frontbuffer */
57 /* XXX allocation should only happen in the unusual case
58 it's actually needed */
59 struct gl_renderbuffer *rb
60 = st_new_renderbuffer_fb(colorFormat);
61 _mesa_add_renderbuffer(&stfb->Base, BUFFER_FRONT_LEFT, rb);
62 }
63
64 if (visual->doubleBufferMode) {
65 struct gl_renderbuffer *rb
66 = st_new_renderbuffer_fb(colorFormat);
67 _mesa_add_renderbuffer(&stfb->Base, BUFFER_BACK_LEFT, rb);
68 }
69
70 if (visual->depthBits == 24 && visual->stencilBits == 8) {
71 /* combined depth/stencil buffer */
72 struct gl_renderbuffer *depthStencilRb
73 = st_new_renderbuffer_fb(depthFormat);
74 /* note: bind RB to two attachment points */
75 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthStencilRb);
76 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, depthStencilRb);
77 }
78 else {
79 /* separate depth and/or stencil */
80
81 if (visual->depthBits == 32) {
82 /* 32-bit depth buffer */
83 struct gl_renderbuffer *depthRb
84 = st_new_renderbuffer_fb(depthFormat);
85 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
86 }
87 else if (visual->depthBits == 24) {
88 /* 24-bit depth buffer, ignore stencil bits */
89 struct gl_renderbuffer *depthRb
90 = st_new_renderbuffer_fb(depthFormat);
91 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
92 }
93 else if (visual->depthBits > 0) {
94 /* 16-bit depth buffer */
95 struct gl_renderbuffer *depthRb
96 = st_new_renderbuffer_fb(depthFormat);
97 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
98 }
99
100 if (visual->stencilBits > 0) {
101 /* 8-bit stencil */
102 struct gl_renderbuffer *stencilRb
103 = st_new_renderbuffer_fb(stencilFormat);
104 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, stencilRb);
105 }
106 }
107
108 if (visual->accumRedBits > 0) {
109 /* 16-bit/channel accum */
110 struct gl_renderbuffer *accumRb
111 = st_new_renderbuffer_fb(DEFAULT_ACCUM_PIPE_FORMAT);
112 _mesa_add_renderbuffer(&stfb->Base, BUFFER_ACCUM, accumRb);
113 }
114
115 stfb->Base.Initialized = GL_TRUE;
116 stfb->InitWidth = width;
117 stfb->InitHeight = height;
118 stfb->Private = private;
119 }
120 return stfb;
121 }
122
123
124 void st_resize_framebuffer( struct st_framebuffer *stfb,
125 uint width, uint height )
126 {
127 if (stfb->Base.Width != width || stfb->Base.Height != height) {
128 GET_CURRENT_CONTEXT(ctx);
129 if (ctx) {
130 if (stfb->InitWidth == 0 && stfb->InitHeight == 0) {
131 /* didn't have a valid size until now */
132 stfb->InitWidth = width;
133 stfb->InitHeight = height;
134 if (ctx->Viewport.Width <= 1) {
135 /* set context's initial viewport/scissor size */
136 _mesa_set_viewport(ctx, 0, 0, width, height);
137 _mesa_set_scissor(ctx, 0, 0, width, height);
138 }
139 }
140
141 _mesa_resize_framebuffer(ctx, &stfb->Base, width, height);
142
143 assert(stfb->Base.Width == width);
144 assert(stfb->Base.Height == height);
145 }
146 }
147 }
148
149
150 void st_unreference_framebuffer( struct st_framebuffer **stfb )
151 {
152 _mesa_unreference_framebuffer((struct gl_framebuffer **) stfb);
153 }
154
155
156
157 /**
158 * Return the pipe_surface for the given renderbuffer.
159 */
160 struct pipe_surface *
161 st_get_framebuffer_surface(struct st_framebuffer *stfb, uint surfIndex)
162 {
163 struct st_renderbuffer *strb;
164
165 assert(surfIndex <= ST_SURFACE_DEPTH);
166
167 /* sanity checks, ST tokens should match Mesa tokens */
168 assert(ST_SURFACE_FRONT_LEFT == BUFFER_FRONT_LEFT);
169 assert(ST_SURFACE_BACK_RIGHT == BUFFER_BACK_RIGHT);
170
171 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
172 if (strb)
173 return strb->surface;
174 return NULL;
175 }
176
177
178 /**
179 * This function is to be called prior to SwapBuffers on the given
180 * framebuffer. It checks if the current context is bound to the framebuffer
181 * and flushes rendering if needed.
182 */
183 void
184 st_notify_swapbuffers(struct st_framebuffer *stfb)
185 {
186 GET_CURRENT_CONTEXT(ctx);
187
188 if (ctx && ctx->DrawBuffer == &stfb->Base) {
189 st_flush( ctx->st,
190 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_SWAPBUFFERS,
191 NULL );
192 ctx->st->frontbuffer_status = FRONT_STATUS_COPY_OF_BACK;
193 }
194 }
195
196
197 /**
198 * Quick hack - allows the winsys to inform the driver that surface
199 * states are now undefined after a glXSwapBuffers or similar.
200 */
201 void
202 st_notify_swapbuffers_complete(struct st_framebuffer *stfb)
203 {
204 GET_CURRENT_CONTEXT(ctx);
205
206 if (ctx && ctx->DrawBuffer == &stfb->Base) {
207 struct st_renderbuffer *strb;
208 int i;
209
210 for (i = 0; i < BUFFER_COUNT; i++) {
211 if (stfb->Base.Attachment[i].Renderbuffer) {
212 strb = st_renderbuffer(stfb->Base.Attachment[i].Renderbuffer);
213 strb->surface->status = PIPE_SURFACE_STATUS_UNDEFINED;
214 }
215 }
216 }
217 }
218
219
220 void *st_framebuffer_private( struct st_framebuffer *stfb )
221 {
222 return stfb->Private;
223 }
224