2090e51d40c50c60d91692b7ccb54211d05466fc
[mesa.git] / src / mesa / drivers / dri / intel / intel_span.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright 2011 Intel Corporation
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Authors:
28 * Chad Versace <chad@chad-versace.us>
29 *
30 **************************************************************************/
31
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include "main/glheader.h"
35 #include "main/macros.h"
36 #include "main/mtypes.h"
37 #include "main/colormac.h"
38 #include "main/renderbuffer.h"
39
40 #include "intel_buffers.h"
41 #include "intel_fbo.h"
42 #include "intel_mipmap_tree.h"
43 #include "intel_screen.h"
44 #include "intel_span.h"
45 #include "intel_regions.h"
46 #include "intel_tex.h"
47
48 #include "swrast/swrast.h"
49 #include "swrast/s_renderbuffer.h"
50
51 /**
52 * \brief Get pointer offset into stencil buffer.
53 *
54 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
55 * must decode the tile's layout in software.
56 *
57 * See
58 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
59 * Format.
60 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
61 *
62 * Even though the returned offset is always positive, the return type is
63 * signed due to
64 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
65 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
66 */
67 intptr_t
68 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y)
69 {
70 uint32_t tile_size = 4096;
71 uint32_t tile_width = 64;
72 uint32_t tile_height = 64;
73 uint32_t row_size = 64 * stride;
74
75 uint32_t tile_x = x / tile_width;
76 uint32_t tile_y = y / tile_height;
77
78 /* The byte's address relative to the tile's base addres. */
79 uint32_t byte_x = x % tile_width;
80 uint32_t byte_y = y % tile_height;
81
82 uintptr_t u = tile_y * row_size
83 + tile_x * tile_size
84 + 512 * (byte_x / 8)
85 + 64 * (byte_y / 8)
86 + 32 * ((byte_y / 4) % 2)
87 + 16 * ((byte_x / 4) % 2)
88 + 8 * ((byte_y / 2) % 2)
89 + 4 * ((byte_x / 2) % 2)
90 + 2 * (byte_y % 2)
91 + 1 * (byte_x % 2);
92
93 /*
94 * Errata for Gen5:
95 *
96 * An additional offset is needed which is not documented in the PRM.
97 *
98 * if ((byte_x / 8) % 2 == 1) {
99 * if ((byte_y / 8) % 2) == 0) {
100 * u += 64;
101 * } else {
102 * u -= 64;
103 * }
104 * }
105 *
106 * The offset is expressed more tersely as
107 * u += ((int) x & 0x8) * (8 - (((int) y & 0x8) << 1));
108 */
109
110 return u;
111 }
112
113 static void
114 intel_renderbuffer_map(struct intel_context *intel, struct gl_renderbuffer *rb)
115 {
116 struct gl_context *ctx = &intel->ctx;
117 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
118 GLubyte *map;
119 int stride;
120
121 if (!irb)
122 return;
123
124 if (irb->Base.Map) {
125 /* Renderbuffer is already mapped. This usually happens when a single
126 * buffer is attached to the framebuffer's depth and stencil attachment
127 * points.
128 */
129 return;
130 }
131
132 ctx->Driver.MapRenderbuffer(ctx, rb, 0, 0, rb->Width, rb->Height,
133 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
134 &map, &stride);
135 irb->Base.Map = map;
136 irb->Base.RowStride = stride;
137 }
138
139 static void
140 intel_renderbuffer_unmap(struct intel_context *intel,
141 struct gl_renderbuffer *rb)
142 {
143 struct gl_context *ctx = &intel->ctx;
144 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
145
146 if (!irb)
147 return;
148
149 if (!irb->Base.Map) {
150 /* Renderbuffer is already unmapped. This usually happens when a single
151 * buffer is attached to the framebuffer's depth and stencil attachment
152 * points.
153 */
154 return;
155 }
156
157 ctx->Driver.UnmapRenderbuffer(ctx, rb);
158
159 irb->Base.Map = NULL;
160 irb->Base.RowStride = 0;
161 }
162
163 static void
164 intel_framebuffer_map(struct intel_context *intel, struct gl_framebuffer *fb)
165 {
166 int i;
167
168 for (i = 0; i < BUFFER_COUNT; i++) {
169 intel_renderbuffer_map(intel, fb->Attachment[i].Renderbuffer);
170 }
171
172 intel_check_front_buffer_rendering(intel);
173 }
174
175 static void
176 intel_framebuffer_unmap(struct intel_context *intel, struct gl_framebuffer *fb)
177 {
178 int i;
179
180 for (i = 0; i < BUFFER_COUNT; i++) {
181 intel_renderbuffer_unmap(intel, fb->Attachment[i].Renderbuffer);
182 }
183 }
184
185 /**
186 * Resolve all buffers that will be mapped by intelSpanRenderStart().
187 *
188 * Resolve the depth buffer of each enabled texture and of the read and draw
189 * buffers.
190 *
191 * (Note: In the future this will also perform MSAA resolves.)
192 */
193 static void
194 intel_span_resolve_buffers(struct intel_context *intel)
195 {
196 struct gl_context *ctx = &intel->ctx;
197 struct intel_renderbuffer *draw_irb;
198 struct intel_renderbuffer *read_irb;
199 struct intel_texture_object *tex_obj;
200
201 /* Resolve depth buffer of each enabled texture. */
202 for (int i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
203 if (!ctx->Texture.Unit[i]._ReallyEnabled)
204 continue;
205 tex_obj = intel_texture_object(ctx->Texture.Unit[i]._Current);
206 intel_finalize_mipmap_tree(intel, i);
207 if (!tex_obj || !tex_obj->mt)
208 continue;
209 intel_miptree_all_slices_resolve_depth(intel, tex_obj->mt);
210 }
211
212 /* Resolve each attached depth buffer. */
213 draw_irb = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
214 read_irb = intel_get_renderbuffer(ctx->ReadBuffer, BUFFER_DEPTH);
215 if (draw_irb)
216 intel_renderbuffer_resolve_depth(intel, draw_irb);
217 if (read_irb != draw_irb && read_irb)
218 intel_renderbuffer_resolve_depth(intel, read_irb);
219 }
220
221 /**
222 * Map the regions needed by intelSpanRenderStart().
223 */
224 static void
225 intel_span_map_buffers(struct intel_context *intel)
226 {
227 struct gl_context *ctx = &intel->ctx;
228 struct intel_texture_object *tex_obj;
229
230 for (int i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
231 if (!ctx->Texture.Unit[i]._ReallyEnabled)
232 continue;
233 tex_obj = intel_texture_object(ctx->Texture.Unit[i]._Current);
234 intel_finalize_mipmap_tree(intel, i);
235 intel_tex_map_images(intel, tex_obj,
236 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT);
237 }
238
239 intel_framebuffer_map(intel, ctx->DrawBuffer);
240 if (ctx->ReadBuffer != ctx->DrawBuffer) {
241 intel_framebuffer_map(intel, ctx->ReadBuffer);
242 }
243 }
244
245 /**
246 * Prepare for software rendering. Map current read/draw framebuffers'
247 * renderbuffes and all currently bound texture objects.
248 *
249 * Old note: Moved locking out to get reasonable span performance.
250 */
251 void
252 intelSpanRenderStart(struct gl_context * ctx)
253 {
254 struct intel_context *intel = intel_context(ctx);
255
256 intel_flush(ctx);
257 intel_prepare_render(intel);
258 intel_span_resolve_buffers(intel);
259 intel_flush(ctx);
260 intel_span_map_buffers(intel);
261 }
262
263 /**
264 * Called when done software rendering. Unmap the buffers we mapped in
265 * the above function.
266 */
267 void
268 intelSpanRenderFinish(struct gl_context * ctx)
269 {
270 struct intel_context *intel = intel_context(ctx);
271 GLuint i;
272
273 _swrast_flush(ctx);
274
275 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
276 if (ctx->Texture.Unit[i]._ReallyEnabled) {
277 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
278 intel_tex_unmap_images(intel, intel_texture_object(texObj));
279 }
280 }
281
282 intel_framebuffer_unmap(intel, ctx->DrawBuffer);
283 if (ctx->ReadBuffer != ctx->DrawBuffer) {
284 intel_framebuffer_unmap(intel, ctx->ReadBuffer);
285 }
286 }
287
288
289 void
290 intelInitSpanFuncs(struct gl_context * ctx)
291 {
292 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
293 swdd->SpanRenderStart = intelSpanRenderStart;
294 swdd->SpanRenderFinish = intelSpanRenderFinish;
295 }
296
297 void
298 intel_map_vertex_shader_textures(struct gl_context *ctx)
299 {
300 struct intel_context *intel = intel_context(ctx);
301 int i;
302
303 if (ctx->VertexProgram._Current == NULL)
304 return;
305
306 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
307 if (ctx->Texture.Unit[i]._ReallyEnabled &&
308 ctx->VertexProgram._Current->Base.TexturesUsed[i] != 0) {
309 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
310
311 intel_tex_map_images(intel, intel_texture_object(texObj),
312 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT);
313 }
314 }
315 }
316
317 void
318 intel_unmap_vertex_shader_textures(struct gl_context *ctx)
319 {
320 struct intel_context *intel = intel_context(ctx);
321 int i;
322
323 if (ctx->VertexProgram._Current == NULL)
324 return;
325
326 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
327 if (ctx->Texture.Unit[i]._ReallyEnabled &&
328 ctx->VertexProgram._Current->Base.TexturesUsed[i] != 0) {
329 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
330
331 intel_tex_unmap_images(intel, intel_texture_object(texObj));
332 }
333 }
334 }