Tizen 2.0 Release
[profile/ivi/osmesa.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                                                                         \
135    /*                                                                   \
136     * Here we ignore rb->Data and rb->RowStride as set by               \
137     * intelSpanRenderStart. Since intel_offset_S8 decodes the W tile    \
138     * manually, the region's *real* base address and stride is          \
139     * required.                                                         \
140     */                                                                  \
141    struct intel_renderbuffer *irb = intel_renderbuffer(rb);             \
142    uint8_t *buf = irb->region->buffer->virtual;                         \
143    unsigned stride = irb->region->pitch;                                \
144    bool flip = rb->Name == 0;                                           \
145    int y_scale = flip ? -1 : 1;                                         \
146    int y_bias = flip ? (rb->Height - 1) : 0;                            \
147
148 #undef Y_FLIP
149 #define Y_FLIP(y) (y_scale * (y) + y_bias)
150
151 /**
152  * \brief Get pointer offset into stencil buffer.
153  *
154  * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
155  * must decode the tile's layout in software.
156  *
157  * See
158  *   - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
159  *     Format.
160  *   - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
161  *
162  * Even though the returned offset is always positive, the return type is
163  * signed due to
164  *    commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
165  *    mesa: Fix return type of  _mesa_get_format_bytes() (#37351)
166  */
167 static inline intptr_t
168 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y)
169 {
170    uint32_t tile_size = 4096;
171    uint32_t tile_width = 64;
172    uint32_t tile_height = 64;
173    uint32_t row_size = 64 * stride;
174
175    uint32_t tile_x = x / tile_width;
176    uint32_t tile_y = y / tile_height;
177
178    /* The byte's address relative to the tile's base addres. */
179    uint32_t byte_x = x % tile_width;
180    uint32_t byte_y = y % tile_height;
181
182    uintptr_t u = tile_y * row_size
183                + tile_x * tile_size
184                + 512 * (byte_x / 8)
185                +  64 * (byte_y / 8)
186                +  32 * ((byte_y / 4) % 2)
187                +  16 * ((byte_x / 4) % 2)
188                +   8 * ((byte_y / 2) % 2)
189                +   4 * ((byte_x / 2) % 2)
190                +   2 * (byte_y % 2)
191                +   1 * (byte_x % 2);
192
193    /*
194     * Errata for Gen5:
195     *
196     * An additional offset is needed which is not documented in the PRM.
197     *
198     * if ((byte_x / 8) % 2 == 1) {
199     *    if ((byte_y / 8) % 2) == 0) {
200     *       u += 64;
201     *    } else {
202     *       u -= 64;
203     *    }
204     * }
205     *
206     * The offset is expressed more tersely as
207     * u += ((int) x & 0x8) * (8 - (((int) y & 0x8) << 1));
208     */
209
210    return u;
211 }
212
213 #define WRITE_STENCIL(x, y, src)  buf[intel_offset_S8(stride, x, y)] = src;
214 #define READ_STENCIL(dest, x, y) dest = buf[intel_offset_S8(stride, x, y)]
215 #define TAG(x) intel_##x##_S8
216 #include "stenciltmp.h"
217
218 /* ------------------------------------------------------------------------- */
219
220 void
221 intel_renderbuffer_map(struct intel_context *intel, struct gl_renderbuffer *rb)
222 {
223    struct intel_renderbuffer *irb = intel_renderbuffer(rb);
224
225    if (!irb)
226       return;
227
228    if (irb->wrapped_depth)
229       intel_renderbuffer_map(intel, irb->wrapped_depth);
230    if (irb->wrapped_stencil)
231       intel_renderbuffer_map(intel, irb->wrapped_stencil);
232
233    if (!irb->region)
234       return;
235
236    drm_intel_gem_bo_map_gtt(irb->region->buffer);
237
238    rb->Data = irb->region->buffer->virtual;
239    rb->RowStride = irb->region->pitch;
240
241    if (!rb->Name) {
242       /* Flip orientation of the window system buffer */
243       rb->Data += rb->RowStride * (irb->region->height - 1) * irb->region->cpp;
244       rb->RowStride = -rb->RowStride;
245    } else {
246       /* Adjust the base pointer of a texture image drawbuffer to the image
247        * within the miptree region (all else has draw_x/y = 0).
248        */
249       rb->Data += irb->draw_x * irb->region->cpp;
250       rb->Data += irb->draw_y * rb->RowStride * irb->region->cpp;
251    }
252
253    intel_set_span_functions(intel, rb);
254 }
255
256 void
257 intel_renderbuffer_unmap(struct intel_context *intel,
258                          struct gl_renderbuffer *rb)
259 {
260    struct intel_renderbuffer *irb = intel_renderbuffer(rb);
261
262    if (!irb)
263       return;
264
265    if (irb->wrapped_depth)
266       intel_renderbuffer_unmap(intel, irb->wrapped_depth);
267    if (irb->wrapped_stencil)
268       intel_renderbuffer_unmap(intel, irb->wrapped_stencil);
269
270    if (!irb->region)
271       return;
272
273    drm_intel_gem_bo_unmap_gtt(irb->region->buffer);
274
275    rb->GetRow = NULL;
276    rb->PutRow = NULL;
277    rb->Data = NULL;
278    rb->RowStride = 0;
279 }
280
281 static void
282 intel_framebuffer_map(struct intel_context *intel, struct gl_framebuffer *fb)
283 {
284    int i;
285
286    for (i = 0; i < BUFFER_COUNT; i++) {
287       intel_renderbuffer_map(intel, fb->Attachment[i].Renderbuffer);
288    }
289
290    intel_check_front_buffer_rendering(intel);
291 }
292
293 static void
294 intel_framebuffer_unmap(struct intel_context *intel, struct gl_framebuffer *fb)
295 {
296    int i;
297
298    for (i = 0; i < BUFFER_COUNT; i++) {
299       intel_renderbuffer_unmap(intel, fb->Attachment[i].Renderbuffer);
300    }
301 }
302
303 /**
304  * Prepare for software rendering.  Map current read/draw framebuffers'
305  * renderbuffes and all currently bound texture objects.
306  *
307  * Old note: Moved locking out to get reasonable span performance.
308  */
309 void
310 intelSpanRenderStart(struct gl_context * ctx)
311 {
312    struct intel_context *intel = intel_context(ctx);
313    GLuint i;
314
315    intel_flush(&intel->ctx);
316    intel_prepare_render(intel);
317
318    for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
319       if (ctx->Texture.Unit[i]._ReallyEnabled) {
320          struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
321
322          intel_finalize_mipmap_tree(intel, i);
323          intel_tex_map_images(intel, intel_texture_object(texObj));
324       }
325    }
326
327    intel_framebuffer_map(intel, ctx->DrawBuffer);
328    if (ctx->ReadBuffer != ctx->DrawBuffer) {
329       intel_framebuffer_map(intel, ctx->ReadBuffer);
330    }
331 }
332
333 /**
334  * Called when done software rendering.  Unmap the buffers we mapped in
335  * the above function.
336  */
337 void
338 intelSpanRenderFinish(struct gl_context * ctx)
339 {
340    struct intel_context *intel = intel_context(ctx);
341    GLuint i;
342
343    _swrast_flush(ctx);
344
345    for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
346       if (ctx->Texture.Unit[i]._ReallyEnabled) {
347          struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
348          intel_tex_unmap_images(intel, intel_texture_object(texObj));
349       }
350    }
351
352    intel_framebuffer_unmap(intel, ctx->DrawBuffer);
353    if (ctx->ReadBuffer != ctx->DrawBuffer) {
354       intel_framebuffer_unmap(intel, ctx->ReadBuffer);
355    }
356 }
357
358
359 void
360 intelInitSpanFuncs(struct gl_context * ctx)
361 {
362    struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
363    swdd->SpanRenderStart = intelSpanRenderStart;
364    swdd->SpanRenderFinish = intelSpanRenderFinish;
365 }
366
367 void
368 intel_map_vertex_shader_textures(struct gl_context *ctx)
369 {
370    struct intel_context *intel = intel_context(ctx);
371    int i;
372
373    if (ctx->VertexProgram._Current == NULL)
374       return;
375
376    for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
377       if (ctx->Texture.Unit[i]._ReallyEnabled &&
378           ctx->VertexProgram._Current->Base.TexturesUsed[i] != 0) {
379          struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
380
381          intel_tex_map_images(intel, intel_texture_object(texObj));
382       }
383    }
384 }
385
386 void
387 intel_unmap_vertex_shader_textures(struct gl_context *ctx)
388 {
389    struct intel_context *intel = intel_context(ctx);
390    int i;
391
392    if (ctx->VertexProgram._Current == NULL)
393       return;
394
395    for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
396       if (ctx->Texture.Unit[i]._ReallyEnabled &&
397           ctx->VertexProgram._Current->Base.TexturesUsed[i] != 0) {
398          struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
399
400          intel_tex_unmap_images(intel, intel_texture_object(texObj));
401       }
402    }
403 }
404
405 typedef void (*span_init_func)(struct gl_renderbuffer *rb);
406
407 static span_init_func intel_span_init_funcs[MESA_FORMAT_COUNT] =
408 {
409    [MESA_FORMAT_A8] = intel_InitPointers_A8,
410    [MESA_FORMAT_RGB565] = intel_InitPointers_RGB565,
411    [MESA_FORMAT_ARGB4444] = intel_InitPointers_ARGB4444,
412    [MESA_FORMAT_ARGB1555] = intel_InitPointers_ARGB1555,
413    [MESA_FORMAT_XRGB8888] = intel_InitPointers_xRGB8888,
414    [MESA_FORMAT_ARGB8888] = intel_InitPointers_ARGB8888,
415    [MESA_FORMAT_SARGB8] = intel_InitPointers_ARGB8888,
416    [MESA_FORMAT_Z16] = _mesa_set_renderbuffer_accessors,
417    [MESA_FORMAT_X8_Z24] = _mesa_set_renderbuffer_accessors,
418    [MESA_FORMAT_S8_Z24] = _mesa_set_renderbuffer_accessors,
419    [MESA_FORMAT_S8] = intel_InitStencilPointers_S8,
420    [MESA_FORMAT_R8] = _mesa_set_renderbuffer_accessors,
421    [MESA_FORMAT_RG88] = _mesa_set_renderbuffer_accessors,
422    [MESA_FORMAT_R16] = _mesa_set_renderbuffer_accessors,
423    [MESA_FORMAT_RG1616] = _mesa_set_renderbuffer_accessors,
424    [MESA_FORMAT_RGBA_FLOAT32] = _mesa_set_renderbuffer_accessors,
425    [MESA_FORMAT_RG_FLOAT32] = _mesa_set_renderbuffer_accessors,
426    [MESA_FORMAT_R_FLOAT32] = _mesa_set_renderbuffer_accessors,
427    [MESA_FORMAT_INTENSITY_FLOAT32] = _mesa_set_renderbuffer_accessors,
428    [MESA_FORMAT_LUMINANCE_FLOAT32] = _mesa_set_renderbuffer_accessors,
429 };
430
431 bool
432 intel_span_supports_format(gl_format format)
433 {
434    return intel_span_init_funcs[format] != NULL;
435 }
436
437 /**
438  * Plug in appropriate span read/write functions for the given renderbuffer.
439  * These are used for the software fallbacks.
440  */
441 static void
442 intel_set_span_functions(struct intel_context *intel,
443                          struct gl_renderbuffer *rb)
444 {
445    struct intel_renderbuffer *irb = (struct intel_renderbuffer *) rb;
446
447    assert(intel_span_init_funcs[irb->Base.Format]);
448    intel_span_init_funcs[irb->Base.Format](rb);
449 }