1e2494286b87b0344c8143db34d90c3cb1d13566
[mesa.git] / src / mesa / drivers / dri / i965 / intel_fbo.h
1 /*
2 * Copyright 2006 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * 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
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #ifndef INTEL_FBO_H
27 #define INTEL_FBO_H
28
29 #include <stdbool.h>
30 #include <assert.h>
31 #include "main/formats.h"
32 #include "main/macros.h"
33 #include "brw_context.h"
34 #include "intel_mipmap_tree.h"
35 #include "intel_screen.h"
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 struct intel_mipmap_tree;
42
43 /**
44 * Intel renderbuffer, derived from gl_renderbuffer.
45 */
46 struct intel_renderbuffer
47 {
48 struct swrast_renderbuffer Base;
49 /**
50 * The real renderbuffer storage.
51 *
52 * This is multisampled if NumSamples is > 1.
53 */
54 struct intel_mipmap_tree *mt;
55
56 /**
57 * Downsampled contents for window-system MSAA renderbuffers.
58 *
59 * For window system MSAA color buffers, the singlesample_mt is shared with
60 * other processes in DRI2 (and in DRI3, it's the image buffer managed by
61 * glx_dri3.c), while mt is private to our process. To do a swapbuffers,
62 * we have to downsample out of mt into singlesample_mt. For depth and
63 * stencil buffers, the singlesample_mt is also private, and since we don't
64 * expect to need to do resolves (except if someone does a glReadPixels()
65 * or glCopyTexImage()), we just temporarily allocate singlesample_mt when
66 * asked to map the renderbuffer.
67 */
68 struct intel_mipmap_tree *singlesample_mt;
69
70 /* Gen < 6 doesn't have layer specifier for render targets or depth. Driver
71 * needs to manually offset surfaces to correct level/layer. There are,
72 * however, alignment restrictions to respect as well and in come cases
73 * the only option is to use temporary single slice surface which driver
74 * copies after rendering to the full miptree.
75 *
76 * See intel_renderbuffer_move_to_temp().
77 */
78 struct intel_mipmap_tree *align_wa_mt;
79
80 /**
81 * \name Miptree view
82 * \{
83 *
84 * Multiple renderbuffers may simultaneously wrap a single texture and each
85 * provide a different view into that texture. The fields below indicate
86 * which miptree slice is wrapped by this renderbuffer. The fields' values
87 * are consistent with the 'level' and 'layer' parameters of
88 * glFramebufferTextureLayer().
89 *
90 * For renderbuffers not created with glFramebufferTexture*(), mt_level and
91 * mt_layer are 0.
92 */
93 unsigned int mt_level;
94 unsigned int mt_layer;
95
96 /* The number of attached logical layers. */
97 unsigned int layer_count;
98 /** \} */
99
100 GLuint draw_x, draw_y; /**< Offset of drawing within the region */
101
102 /**
103 * Set to true at every draw call, to indicate if a window-system
104 * renderbuffer needs to be downsampled before using singlesample_mt.
105 */
106 bool need_downsample;
107
108 /**
109 * Set to true when doing an intel_renderbuffer_map()/unmap() that requires
110 * an upsample at the end.
111 */
112 bool need_map_upsample;
113
114 /**
115 * Set to true if singlesample_mt is temporary storage that persists only
116 * for the duration of a mapping.
117 */
118 bool singlesample_mt_is_tmp;
119 };
120
121
122 /**
123 * gl_renderbuffer is a base class which we subclass. The Class field
124 * is used for simple run-time type checking.
125 */
126 #define INTEL_RB_CLASS 0x12345678
127
128
129 /**
130 * Return a gl_renderbuffer ptr casted to intel_renderbuffer.
131 * NULL will be returned if the rb isn't really an intel_renderbuffer.
132 * This is determined by checking the ClassID.
133 */
134 static inline struct intel_renderbuffer *
135 intel_renderbuffer(struct gl_renderbuffer *rb)
136 {
137 struct intel_renderbuffer *irb = (struct intel_renderbuffer *) rb;
138 if (irb && irb->Base.Base.ClassID == INTEL_RB_CLASS)
139 return irb;
140 else
141 return NULL;
142 }
143
144 static inline struct intel_mipmap_tree *
145 intel_renderbuffer_get_mt(struct intel_renderbuffer *irb)
146 {
147 if (!irb)
148 return NULL;
149
150 return (irb->align_wa_mt) ? irb->align_wa_mt : irb->mt;
151 }
152
153 /**
154 * \brief Return the framebuffer attachment specified by attIndex.
155 *
156 * If the framebuffer lacks the specified attachment, then return null.
157 *
158 * If the attached renderbuffer is a wrapper, then return wrapped
159 * renderbuffer.
160 */
161 static inline struct intel_renderbuffer *
162 intel_get_renderbuffer(struct gl_framebuffer *fb, gl_buffer_index attIndex)
163 {
164 struct gl_renderbuffer *rb;
165
166 assert((unsigned)attIndex < ARRAY_SIZE(fb->Attachment));
167
168 rb = fb->Attachment[attIndex].Renderbuffer;
169 if (!rb)
170 return NULL;
171
172 return intel_renderbuffer(rb);
173 }
174
175
176 static inline mesa_format
177 intel_rb_format(const struct intel_renderbuffer *rb)
178 {
179 return rb->Base.Base.Format;
180 }
181
182 extern struct intel_renderbuffer *
183 intel_create_winsys_renderbuffer(struct intel_screen *screen,
184 mesa_format format, unsigned num_samples);
185
186 struct intel_renderbuffer *
187 intel_create_private_renderbuffer(struct intel_screen *screen,
188 mesa_format format, unsigned num_samples);
189
190 struct gl_renderbuffer*
191 intel_create_wrapped_renderbuffer(struct gl_context * ctx,
192 int width, int height,
193 mesa_format format);
194
195 extern void
196 intel_fbo_init(struct brw_context *brw);
197
198 void
199 intel_renderbuffer_set_draw_offset(struct intel_renderbuffer *irb);
200
201 static inline uint32_t
202 intel_renderbuffer_get_tile_offsets(struct intel_renderbuffer *irb,
203 uint32_t *tile_x,
204 uint32_t *tile_y)
205 {
206 if (irb->align_wa_mt) {
207 *tile_x = 0;
208 *tile_y = 0;
209 return 0;
210 }
211
212 return intel_miptree_get_tile_offsets(irb->mt, irb->mt_level, irb->mt_layer,
213 tile_x, tile_y);
214 }
215
216 bool
217 intel_renderbuffer_has_hiz(struct intel_renderbuffer *irb);
218
219
220 void intel_renderbuffer_move_to_temp(struct brw_context *brw,
221 struct intel_renderbuffer *irb,
222 bool invalidate);
223
224 void
225 intel_renderbuffer_downsample(struct brw_context *brw,
226 struct intel_renderbuffer *irb);
227
228 void
229 intel_renderbuffer_upsample(struct brw_context *brw,
230 struct intel_renderbuffer *irb);
231
232 void brw_render_cache_set_clear(struct brw_context *brw);
233 void brw_render_cache_set_add_bo(struct brw_context *brw, struct brw_bo *bo);
234 void brw_render_cache_set_check_flush(struct brw_context *brw, struct brw_bo *bo);
235
236 unsigned
237 intel_quantize_num_samples(struct intel_screen *intel, unsigned num_samples);
238
239 #ifdef __cplusplus
240 }
241 #endif
242
243 #endif /* INTEL_FBO_H */