intel: Add fields to intel_renderbuffer for unwrapping packed depth/stencil buffers
[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_screen.h"
43 #include "intel_span.h"
44 #include "intel_regions.h"
45 #include "intel_tex.h"
46
47 #include "swrast/swrast.h"
48
49 static void
50 intel_set_span_functions(struct intel_context *intel,
51 struct gl_renderbuffer *rb);
52
53 #undef DBG
54 #define DBG 0
55
56 #define LOCAL_VARS \
57 struct intel_renderbuffer *irb = intel_renderbuffer(rb); \
58 int minx = 0, miny = 0; \
59 int maxx = rb->Width; \
60 int maxy = rb->Height; \
61 int pitch = rb->RowStride * irb->region->cpp; \
62 void *buf = rb->Data; \
63 GLuint p; \
64 (void) p;
65
66 #define HW_CLIPLOOP()
67 #define HW_ENDCLIPLOOP()
68
69 #define Y_FLIP(_y) (_y)
70
71 #define HW_LOCK()
72
73 #define HW_UNLOCK()
74
75 /* r5g6b5 color span and pixel functions */
76 #define SPANTMP_PIXEL_FMT GL_RGB
77 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_5_6_5
78 #define TAG(x) intel_##x##_RGB565
79 #define TAG2(x,y) intel_##x##y_RGB565
80 #include "spantmp2.h"
81
82 /* a4r4g4b4 color span and pixel functions */
83 #define SPANTMP_PIXEL_FMT GL_BGRA
84 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_4_4_4_4_REV
85 #define TAG(x) intel_##x##_ARGB4444
86 #define TAG2(x,y) intel_##x##y_ARGB4444
87 #include "spantmp2.h"
88
89 /* a1r5g5b5 color span and pixel functions */
90 #define SPANTMP_PIXEL_FMT GL_BGRA
91 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_1_5_5_5_REV
92 #define TAG(x) intel_##x##_ARGB1555
93 #define TAG2(x,y) intel_##x##y##_ARGB1555
94 #include "spantmp2.h"
95
96 /* a8r8g8b8 color span and pixel functions */
97 #define SPANTMP_PIXEL_FMT GL_BGRA
98 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
99 #define TAG(x) intel_##x##_ARGB8888
100 #define TAG2(x,y) intel_##x##y##_ARGB8888
101 #include "spantmp2.h"
102
103 /* x8r8g8b8 color span and pixel functions */
104 #define SPANTMP_PIXEL_FMT GL_BGR
105 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
106 #define TAG(x) intel_##x##_xRGB8888
107 #define TAG2(x,y) intel_##x##y##_xRGB8888
108 #include "spantmp2.h"
109
110 /* a8 color span and pixel functions */
111 #define SPANTMP_PIXEL_FMT GL_ALPHA
112 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_BYTE
113 #define TAG(x) intel_##x##_A8
114 #define TAG2(x,y) intel_##x##y##_A8
115 #include "spantmp2.h"
116
117 /* ------------------------------------------------------------------------- */
118 /* s8 stencil span and pixel functions */
119 /* ------------------------------------------------------------------------- */
120
121 /*
122 * HAVE_HW_STENCIL_SPANS determines if stencil buffer read/writes are done with
123 * memcpy or for loops. Since the stencil buffer is interleaved, memcpy won't
124 * work.
125 */
126 #define HAVE_HW_STENCIL_SPANS 0
127
128 #define LOCAL_STENCIL_VARS \
129 (void) ctx; \
130 int minx = 0; \
131 int miny = 0; \
132 int maxx = rb->Width; \
133 int maxy = rb->Height; \
134 int stride = rb->RowStride; \
135 uint8_t *buf = rb->Data; \
136
137 /* Don't flip y. */
138 #undef Y_FLIP
139 #define Y_FLIP(y) y
140
141 /**
142 * \brief Get pointer offset into stencil buffer.
143 *
144 * The stencil buffer interleaves two rows into one. Yay for crazy hardware.
145 * The table below demonstrates how the pointer arithmetic behaves for a buffer
146 * with positive stride (s=stride).
147 *
148 * x | y | byte offset
149 * --------------------------
150 * 0 | 0 | 0
151 * 0 | 1 | 1
152 * 1 | 0 | 2
153 * 1 | 1 | 3
154 * ... | ... | ...
155 * 0 | 2 | s
156 * 0 | 3 | s + 1
157 * 1 | 2 | s + 2
158 * 1 | 3 | s + 3
159 *
160 *
161 */
162 static inline intptr_t
163 intel_offset_S8(int stride, GLint x, GLint y)
164 {
165 return 2 * ((y / 2) * stride + x) + y % 2;
166 }
167
168 #define WRITE_STENCIL(x, y, src) buf[intel_offset_S8(stride, x, y)] = src;
169 #define READ_STENCIL(dest, x, y) dest = buf[intel_offset_S8(stride, x, y)]
170 #define TAG(x) intel_##x##_S8
171 #include "stenciltmp.h"
172
173 /* ------------------------------------------------------------------------- */
174
175 void
176 intel_renderbuffer_map(struct intel_context *intel, struct gl_renderbuffer *rb)
177 {
178 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
179
180 if (!irb)
181 return;
182
183 if (irb->wrapped_depth)
184 intel_renderbuffer_map(intel, irb->wrapped_depth);
185 if (irb->wrapped_stencil)
186 intel_renderbuffer_map(intel, irb->wrapped_stencil);
187
188 if (!irb->region)
189 return;
190
191 drm_intel_gem_bo_map_gtt(irb->region->buffer);
192
193 rb->Data = irb->region->buffer->virtual;
194 rb->RowStride = irb->region->pitch;
195
196 if (!rb->Name) {
197 /* Flip orientation of the window system buffer */
198 rb->Data += rb->RowStride * (irb->region->height - 1) * irb->region->cpp;
199 rb->RowStride = -rb->RowStride;
200 } else {
201 /* Adjust the base pointer of a texture image drawbuffer to the image
202 * within the miptree region (all else has draw_x/y = 0).
203 */
204 rb->Data += irb->draw_x * irb->region->cpp;
205 rb->Data += irb->draw_y * rb->RowStride * irb->region->cpp;
206 }
207
208 intel_set_span_functions(intel, rb);
209 }
210
211 void
212 intel_renderbuffer_unmap(struct intel_context *intel,
213 struct gl_renderbuffer *rb)
214 {
215 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
216
217 if (!irb)
218 return;
219
220 if (irb->wrapped_depth)
221 intel_renderbuffer_unmap(intel, irb->wrapped_depth);
222 if (irb->wrapped_stencil)
223 intel_renderbuffer_unmap(intel, irb->wrapped_stencil);
224
225 if (!irb->region)
226 return;
227
228 drm_intel_gem_bo_unmap_gtt(irb->region->buffer);
229
230 rb->GetRow = NULL;
231 rb->PutRow = NULL;
232 rb->Data = NULL;
233 rb->RowStride = 0;
234 }
235
236 static void
237 intel_framebuffer_map(struct intel_context *intel, struct gl_framebuffer *fb)
238 {
239 int i;
240
241 for (i = 0; i < BUFFER_COUNT; i++) {
242 intel_renderbuffer_map(intel, fb->Attachment[i].Renderbuffer);
243 }
244
245 intel_check_front_buffer_rendering(intel);
246 }
247
248 static void
249 intel_framebuffer_unmap(struct intel_context *intel, struct gl_framebuffer *fb)
250 {
251 int i;
252
253 for (i = 0; i < BUFFER_COUNT; i++) {
254 intel_renderbuffer_unmap(intel, fb->Attachment[i].Renderbuffer);
255 }
256 }
257
258 /**
259 * Prepare for software rendering. Map current read/draw framebuffers'
260 * renderbuffes and all currently bound texture objects.
261 *
262 * Old note: Moved locking out to get reasonable span performance.
263 */
264 void
265 intelSpanRenderStart(struct gl_context * ctx)
266 {
267 struct intel_context *intel = intel_context(ctx);
268 GLuint i;
269
270 intel_flush(&intel->ctx);
271 intel_prepare_render(intel);
272
273 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
274 if (ctx->Texture.Unit[i]._ReallyEnabled) {
275 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
276
277 intel_finalize_mipmap_tree(intel, i);
278 intel_tex_map_images(intel, intel_texture_object(texObj));
279 }
280 }
281
282 intel_framebuffer_map(intel, ctx->DrawBuffer);
283 if (ctx->ReadBuffer != ctx->DrawBuffer) {
284 intel_framebuffer_map(intel, ctx->ReadBuffer);
285 }
286 }
287
288 /**
289 * Called when done software rendering. Unmap the buffers we mapped in
290 * the above function.
291 */
292 void
293 intelSpanRenderFinish(struct gl_context * ctx)
294 {
295 struct intel_context *intel = intel_context(ctx);
296 GLuint i;
297
298 _swrast_flush(ctx);
299
300 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
301 if (ctx->Texture.Unit[i]._ReallyEnabled) {
302 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
303 intel_tex_unmap_images(intel, intel_texture_object(texObj));
304 }
305 }
306
307 intel_framebuffer_unmap(intel, ctx->DrawBuffer);
308 if (ctx->ReadBuffer != ctx->DrawBuffer) {
309 intel_framebuffer_unmap(intel, ctx->ReadBuffer);
310 }
311 }
312
313
314 void
315 intelInitSpanFuncs(struct gl_context * ctx)
316 {
317 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
318 swdd->SpanRenderStart = intelSpanRenderStart;
319 swdd->SpanRenderFinish = intelSpanRenderFinish;
320 }
321
322 void
323 intel_map_vertex_shader_textures(struct gl_context *ctx)
324 {
325 struct intel_context *intel = intel_context(ctx);
326 int i;
327
328 if (ctx->VertexProgram._Current == NULL)
329 return;
330
331 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
332 if (ctx->Texture.Unit[i]._ReallyEnabled &&
333 ctx->VertexProgram._Current->Base.TexturesUsed[i] != 0) {
334 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
335
336 intel_tex_map_images(intel, intel_texture_object(texObj));
337 }
338 }
339 }
340
341 void
342 intel_unmap_vertex_shader_textures(struct gl_context *ctx)
343 {
344 struct intel_context *intel = intel_context(ctx);
345 int i;
346
347 if (ctx->VertexProgram._Current == NULL)
348 return;
349
350 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
351 if (ctx->Texture.Unit[i]._ReallyEnabled &&
352 ctx->VertexProgram._Current->Base.TexturesUsed[i] != 0) {
353 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
354
355 intel_tex_unmap_images(intel, intel_texture_object(texObj));
356 }
357 }
358 }
359
360 typedef void (*span_init_func)(struct gl_renderbuffer *rb);
361
362 static span_init_func intel_span_init_funcs[MESA_FORMAT_COUNT] =
363 {
364 [MESA_FORMAT_A8] = intel_InitPointers_A8,
365 [MESA_FORMAT_RGB565] = intel_InitPointers_RGB565,
366 [MESA_FORMAT_ARGB4444] = intel_InitPointers_ARGB4444,
367 [MESA_FORMAT_ARGB1555] = intel_InitPointers_ARGB1555,
368 [MESA_FORMAT_XRGB8888] = intel_InitPointers_xRGB8888,
369 [MESA_FORMAT_ARGB8888] = intel_InitPointers_ARGB8888,
370 [MESA_FORMAT_SARGB8] = intel_InitPointers_ARGB8888,
371 [MESA_FORMAT_Z16] = _mesa_set_renderbuffer_accessors,
372 [MESA_FORMAT_X8_Z24] = _mesa_set_renderbuffer_accessors,
373 [MESA_FORMAT_S8_Z24] = _mesa_set_renderbuffer_accessors,
374 [MESA_FORMAT_S8] = intel_InitStencilPointers_S8,
375 [MESA_FORMAT_R8] = _mesa_set_renderbuffer_accessors,
376 [MESA_FORMAT_RG88] = _mesa_set_renderbuffer_accessors,
377 [MESA_FORMAT_R16] = _mesa_set_renderbuffer_accessors,
378 [MESA_FORMAT_RG1616] = _mesa_set_renderbuffer_accessors,
379 [MESA_FORMAT_RGBA_FLOAT32] = _mesa_set_renderbuffer_accessors,
380 [MESA_FORMAT_RG_FLOAT32] = _mesa_set_renderbuffer_accessors,
381 [MESA_FORMAT_R_FLOAT32] = _mesa_set_renderbuffer_accessors,
382 [MESA_FORMAT_INTENSITY_FLOAT32] = _mesa_set_renderbuffer_accessors,
383 [MESA_FORMAT_LUMINANCE_FLOAT32] = _mesa_set_renderbuffer_accessors,
384 };
385
386 bool
387 intel_span_supports_format(gl_format format)
388 {
389 return intel_span_init_funcs[format] != NULL;
390 }
391
392 /**
393 * Plug in appropriate span read/write functions for the given renderbuffer.
394 * These are used for the software fallbacks.
395 */
396 static void
397 intel_set_span_functions(struct intel_context *intel,
398 struct gl_renderbuffer *rb)
399 {
400 struct intel_renderbuffer *irb = (struct intel_renderbuffer *) rb;
401
402 assert(intel_span_init_funcs[irb->Base.Format]);
403 intel_span_init_funcs[irb->Base.Format](rb);
404 }