intel: Resolve buffers in intel_map_texture_image()
[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
50 static void
51 intel_set_span_functions(struct intel_context *intel,
52 struct gl_renderbuffer *rb);
53
54 #undef DBG
55 #define DBG 0
56
57 #define LOCAL_VARS \
58 struct intel_renderbuffer *irb = intel_renderbuffer(rb); \
59 int minx = 0, miny = 0; \
60 int maxx = rb->Width; \
61 int maxy = rb->Height; \
62 int pitch = rb->RowStride * irb->mt->region->cpp; \
63 void *buf = rb->Data; \
64 GLuint p; \
65 (void) p;
66
67 #define HW_CLIPLOOP()
68 #define HW_ENDCLIPLOOP()
69
70 #define Y_FLIP(_y) (_y)
71
72 #define HW_LOCK()
73
74 #define HW_UNLOCK()
75
76 /* r5g6b5 color span and pixel functions */
77 #define SPANTMP_PIXEL_FMT GL_RGB
78 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_5_6_5
79 #define TAG(x) intel_##x##_RGB565
80 #define TAG2(x,y) intel_##x##y_RGB565
81 #include "spantmp2.h"
82
83 /* a4r4g4b4 color span and pixel functions */
84 #define SPANTMP_PIXEL_FMT GL_BGRA
85 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_4_4_4_4_REV
86 #define TAG(x) intel_##x##_ARGB4444
87 #define TAG2(x,y) intel_##x##y_ARGB4444
88 #include "spantmp2.h"
89
90 /* a1r5g5b5 color span and pixel functions */
91 #define SPANTMP_PIXEL_FMT GL_BGRA
92 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_1_5_5_5_REV
93 #define TAG(x) intel_##x##_ARGB1555
94 #define TAG2(x,y) intel_##x##y##_ARGB1555
95 #include "spantmp2.h"
96
97 /* a8r8g8b8 color span and pixel functions */
98 #define SPANTMP_PIXEL_FMT GL_BGRA
99 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
100 #define TAG(x) intel_##x##_ARGB8888
101 #define TAG2(x,y) intel_##x##y##_ARGB8888
102 #include "spantmp2.h"
103
104 /* x8r8g8b8 color span and pixel functions */
105 #define SPANTMP_PIXEL_FMT GL_BGR
106 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
107 #define TAG(x) intel_##x##_xRGB8888
108 #define TAG2(x,y) intel_##x##y##_xRGB8888
109 #include "spantmp2.h"
110
111 /* a8 color span and pixel functions */
112 #define SPANTMP_PIXEL_FMT GL_ALPHA
113 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_BYTE
114 #define TAG(x) intel_##x##_A8
115 #define TAG2(x,y) intel_##x##y##_A8
116 #include "spantmp2.h"
117
118 /**
119 * \brief Get pointer offset into stencil buffer.
120 *
121 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
122 * must decode the tile's layout in software.
123 *
124 * See
125 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
126 * Format.
127 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
128 *
129 * Even though the returned offset is always positive, the return type is
130 * signed due to
131 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
132 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
133 */
134 intptr_t
135 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y)
136 {
137 uint32_t tile_size = 4096;
138 uint32_t tile_width = 64;
139 uint32_t tile_height = 64;
140 uint32_t row_size = 64 * stride;
141
142 uint32_t tile_x = x / tile_width;
143 uint32_t tile_y = y / tile_height;
144
145 /* The byte's address relative to the tile's base addres. */
146 uint32_t byte_x = x % tile_width;
147 uint32_t byte_y = y % tile_height;
148
149 uintptr_t u = tile_y * row_size
150 + tile_x * tile_size
151 + 512 * (byte_x / 8)
152 + 64 * (byte_y / 8)
153 + 32 * ((byte_y / 4) % 2)
154 + 16 * ((byte_x / 4) % 2)
155 + 8 * ((byte_y / 2) % 2)
156 + 4 * ((byte_x / 2) % 2)
157 + 2 * (byte_y % 2)
158 + 1 * (byte_x % 2);
159
160 /*
161 * Errata for Gen5:
162 *
163 * An additional offset is needed which is not documented in the PRM.
164 *
165 * if ((byte_x / 8) % 2 == 1) {
166 * if ((byte_y / 8) % 2) == 0) {
167 * u += 64;
168 * } else {
169 * u -= 64;
170 * }
171 * }
172 *
173 * The offset is expressed more tersely as
174 * u += ((int) x & 0x8) * (8 - (((int) y & 0x8) << 1));
175 */
176
177 return u;
178 }
179
180 void
181 intel_renderbuffer_map(struct intel_context *intel, struct gl_renderbuffer *rb)
182 {
183 struct gl_context *ctx = &intel->ctx;
184 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
185 GLubyte *map;
186 int stride;
187
188 if (!irb)
189 return;
190
191 if (rb->Data) {
192 /* Renderbuffer is already mapped. This usually happens when a single
193 * buffer is attached to the framebuffer's depth and stencil attachment
194 * points.
195 */
196 return;
197 }
198
199 ctx->Driver.MapRenderbuffer(ctx, rb, 0, 0, rb->Width, rb->Height,
200 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
201 &map, &stride);
202 rb->Data = map;
203 rb->RowStride = stride / _mesa_get_format_bytes(rb->Format);
204
205 intel_set_span_functions(intel, rb);
206 }
207
208 void
209 intel_renderbuffer_unmap(struct intel_context *intel,
210 struct gl_renderbuffer *rb)
211 {
212 struct gl_context *ctx = &intel->ctx;
213 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
214
215 if (!irb)
216 return;
217
218 if (!rb->Data) {
219 /* Renderbuffer is already unmapped. This usually happens when a single
220 * buffer is attached to the framebuffer's depth and stencil attachment
221 * points.
222 */
223 return;
224 }
225
226 ctx->Driver.UnmapRenderbuffer(ctx, rb);
227
228 rb->GetRow = NULL;
229 rb->PutRow = NULL;
230 rb->Data = NULL;
231 rb->RowStride = 0;
232 }
233
234 static void
235 intel_framebuffer_map(struct intel_context *intel, struct gl_framebuffer *fb)
236 {
237 int i;
238
239 for (i = 0; i < BUFFER_COUNT; i++) {
240 intel_renderbuffer_map(intel, fb->Attachment[i].Renderbuffer);
241 }
242
243 intel_check_front_buffer_rendering(intel);
244 }
245
246 static void
247 intel_framebuffer_unmap(struct intel_context *intel, struct gl_framebuffer *fb)
248 {
249 int i;
250
251 for (i = 0; i < BUFFER_COUNT; i++) {
252 intel_renderbuffer_unmap(intel, fb->Attachment[i].Renderbuffer);
253 }
254 }
255
256 /**
257 * Resolve all buffers that will be mapped by intelSpanRenderStart().
258 *
259 * Resolve the depth buffer of each enabled texture and of the read and draw
260 * buffers.
261 *
262 * (Note: In the future this will also perform MSAA resolves.)
263 */
264 static void
265 intel_span_resolve_buffers(struct intel_context *intel)
266 {
267 struct gl_context *ctx = &intel->ctx;
268 struct intel_renderbuffer *draw_irb;
269 struct intel_renderbuffer *read_irb;
270 struct intel_texture_object *tex_obj;
271
272 /* Resolve depth buffer of each enabled texture. */
273 for (int i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
274 if (!ctx->Texture.Unit[i]._ReallyEnabled)
275 continue;
276 tex_obj = intel_texture_object(ctx->Texture.Unit[i]._Current);
277 intel_finalize_mipmap_tree(intel, i);
278 if (!tex_obj || !tex_obj->mt)
279 continue;
280 intel_miptree_all_slices_resolve_depth(intel, tex_obj->mt);
281 }
282
283 /* Resolve each attached depth buffer. */
284 draw_irb = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
285 read_irb = intel_get_renderbuffer(ctx->ReadBuffer, BUFFER_DEPTH);
286 if (draw_irb)
287 intel_renderbuffer_resolve_depth(intel, draw_irb);
288 if (read_irb != draw_irb && read_irb)
289 intel_renderbuffer_resolve_depth(intel, read_irb);
290 }
291
292 /**
293 * Map the regions needed by intelSpanRenderStart().
294 */
295 static void
296 intel_span_map_buffers(struct intel_context *intel)
297 {
298 struct gl_context *ctx = &intel->ctx;
299 struct intel_texture_object *tex_obj;
300
301 for (int i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
302 if (!ctx->Texture.Unit[i]._ReallyEnabled)
303 continue;
304 tex_obj = intel_texture_object(ctx->Texture.Unit[i]._Current);
305 intel_finalize_mipmap_tree(intel, i);
306 intel_tex_map_images(intel, tex_obj,
307 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT);
308 }
309
310 intel_framebuffer_map(intel, ctx->DrawBuffer);
311 if (ctx->ReadBuffer != ctx->DrawBuffer) {
312 intel_framebuffer_map(intel, ctx->ReadBuffer);
313 }
314 }
315
316 /**
317 * Prepare for software rendering. Map current read/draw framebuffers'
318 * renderbuffes and all currently bound texture objects.
319 *
320 * Old note: Moved locking out to get reasonable span performance.
321 */
322 void
323 intelSpanRenderStart(struct gl_context * ctx)
324 {
325 struct intel_context *intel = intel_context(ctx);
326
327 intel_flush(ctx);
328 intel_prepare_render(intel);
329 intel_span_resolve_buffers(intel);
330 intel_flush(ctx);
331 intel_span_map_buffers(intel);
332 }
333
334 /**
335 * Called when done software rendering. Unmap the buffers we mapped in
336 * the above function.
337 */
338 void
339 intelSpanRenderFinish(struct gl_context * ctx)
340 {
341 struct intel_context *intel = intel_context(ctx);
342 GLuint i;
343
344 _swrast_flush(ctx);
345
346 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
347 if (ctx->Texture.Unit[i]._ReallyEnabled) {
348 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
349 intel_tex_unmap_images(intel, intel_texture_object(texObj));
350 }
351 }
352
353 intel_framebuffer_unmap(intel, ctx->DrawBuffer);
354 if (ctx->ReadBuffer != ctx->DrawBuffer) {
355 intel_framebuffer_unmap(intel, ctx->ReadBuffer);
356 }
357 }
358
359
360 void
361 intelInitSpanFuncs(struct gl_context * ctx)
362 {
363 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
364 swdd->SpanRenderStart = intelSpanRenderStart;
365 swdd->SpanRenderFinish = intelSpanRenderFinish;
366 }
367
368 void
369 intel_map_vertex_shader_textures(struct gl_context *ctx)
370 {
371 struct intel_context *intel = intel_context(ctx);
372 int i;
373
374 if (ctx->VertexProgram._Current == NULL)
375 return;
376
377 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
378 if (ctx->Texture.Unit[i]._ReallyEnabled &&
379 ctx->VertexProgram._Current->Base.TexturesUsed[i] != 0) {
380 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
381
382 intel_tex_map_images(intel, intel_texture_object(texObj),
383 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT);
384 }
385 }
386 }
387
388 void
389 intel_unmap_vertex_shader_textures(struct gl_context *ctx)
390 {
391 struct intel_context *intel = intel_context(ctx);
392 int i;
393
394 if (ctx->VertexProgram._Current == NULL)
395 return;
396
397 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
398 if (ctx->Texture.Unit[i]._ReallyEnabled &&
399 ctx->VertexProgram._Current->Base.TexturesUsed[i] != 0) {
400 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
401
402 intel_tex_unmap_images(intel, intel_texture_object(texObj));
403 }
404 }
405 }
406
407 typedef void (*span_init_func)(struct gl_renderbuffer *rb);
408
409 static span_init_func intel_span_init_funcs[MESA_FORMAT_COUNT] =
410 {
411 [MESA_FORMAT_A8] = intel_InitPointers_A8,
412 [MESA_FORMAT_RGB565] = intel_InitPointers_RGB565,
413 [MESA_FORMAT_ARGB4444] = intel_InitPointers_ARGB4444,
414 [MESA_FORMAT_ARGB1555] = intel_InitPointers_ARGB1555,
415 [MESA_FORMAT_XRGB8888] = intel_InitPointers_xRGB8888,
416 [MESA_FORMAT_ARGB8888] = intel_InitPointers_ARGB8888,
417 [MESA_FORMAT_SARGB8] = intel_InitPointers_ARGB8888,
418 [MESA_FORMAT_Z16] = _mesa_set_renderbuffer_accessors,
419 [MESA_FORMAT_X8_Z24] = _mesa_set_renderbuffer_accessors,
420 [MESA_FORMAT_S8_Z24] = _mesa_set_renderbuffer_accessors,
421 [MESA_FORMAT_S8] = _mesa_set_renderbuffer_accessors,
422 [MESA_FORMAT_R8] = _mesa_set_renderbuffer_accessors,
423 [MESA_FORMAT_RG88] = _mesa_set_renderbuffer_accessors,
424 [MESA_FORMAT_R16] = _mesa_set_renderbuffer_accessors,
425 [MESA_FORMAT_RG1616] = _mesa_set_renderbuffer_accessors,
426 [MESA_FORMAT_RGBA_FLOAT32] = _mesa_set_renderbuffer_accessors,
427 [MESA_FORMAT_RG_FLOAT32] = _mesa_set_renderbuffer_accessors,
428 [MESA_FORMAT_R_FLOAT32] = _mesa_set_renderbuffer_accessors,
429 [MESA_FORMAT_INTENSITY_FLOAT32] = _mesa_set_renderbuffer_accessors,
430 [MESA_FORMAT_LUMINANCE_FLOAT32] = _mesa_set_renderbuffer_accessors,
431 };
432
433 bool
434 intel_span_supports_format(gl_format format)
435 {
436 /* Rendering to/from integer textures will be done using MapRenderbuffer,
437 * rather than coding up new paths through GetRow/PutRow(), so claim support
438 * for those formats in here for now.
439 */
440 return (intel_span_init_funcs[format] != NULL ||
441 _mesa_is_format_integer_color(format));
442 }
443
444 /**
445 * Plug in appropriate span read/write functions for the given renderbuffer.
446 * These are used for the software fallbacks.
447 */
448 static void
449 intel_set_span_functions(struct intel_context *intel,
450 struct gl_renderbuffer *rb)
451 {
452 struct intel_renderbuffer *irb = (struct intel_renderbuffer *) rb;
453
454 assert(intel_span_init_funcs[irb->Base.Format]);
455 intel_span_init_funcs[irb->Base.Format](rb);
456
457 if (rb->DataType == GL_NONE) {
458 _mesa_problem(NULL,
459 "renderbuffer format %s is missing "
460 "intel_mesa_format_to_rb_datatype() support.",
461 _mesa_get_format_name(rb->Format));
462 }
463 }