intel_span.c revision af35a3523df7f555427de4835ae092d1836c3c95
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
49static void
50intel_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 * \brief Get pointer offset into stencil buffer.
119 *
120 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
121 * must decode the tile's layout in software.
122 *
123 * See
124 *   - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
125 *     Format.
126 *   - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
127 *
128 * Even though the returned offset is always positive, the return type is
129 * signed due to
130 *    commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
131 *    mesa: Fix return type of  _mesa_get_format_bytes() (#37351)
132 */
133intptr_t
134intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y)
135{
136   uint32_t tile_size = 4096;
137   uint32_t tile_width = 64;
138   uint32_t tile_height = 64;
139   uint32_t row_size = 64 * stride;
140
141   uint32_t tile_x = x / tile_width;
142   uint32_t tile_y = y / tile_height;
143
144   /* The byte's address relative to the tile's base addres. */
145   uint32_t byte_x = x % tile_width;
146   uint32_t byte_y = y % tile_height;
147
148   uintptr_t u = tile_y * row_size
149               + tile_x * tile_size
150               + 512 * (byte_x / 8)
151               +  64 * (byte_y / 8)
152               +  32 * ((byte_y / 4) % 2)
153               +  16 * ((byte_x / 4) % 2)
154               +   8 * ((byte_y / 2) % 2)
155               +   4 * ((byte_x / 2) % 2)
156               +   2 * (byte_y % 2)
157               +   1 * (byte_x % 2);
158
159   /*
160    * Errata for Gen5:
161    *
162    * An additional offset is needed which is not documented in the PRM.
163    *
164    * if ((byte_x / 8) % 2 == 1) {
165    *    if ((byte_y / 8) % 2) == 0) {
166    *       u += 64;
167    *    } else {
168    *       u -= 64;
169    *    }
170    * }
171    *
172    * The offset is expressed more tersely as
173    * u += ((int) x & 0x8) * (8 - (((int) y & 0x8) << 1));
174    */
175
176   return u;
177}
178
179void
180intel_renderbuffer_map(struct intel_context *intel, struct gl_renderbuffer *rb)
181{
182   struct gl_context *ctx = &intel->ctx;
183   struct intel_renderbuffer *irb = intel_renderbuffer(rb);
184   GLubyte *map;
185   int stride;
186
187   if (!irb)
188      return;
189
190   if (rb->Data) {
191      /* Renderbuffer is already mapped. This usually happens when a single
192       * buffer is attached to the framebuffer's depth and stencil attachment
193       * points.
194       */
195      return;
196   }
197
198   ctx->Driver.MapRenderbuffer(ctx, rb, 0, 0, rb->Width, rb->Height,
199			       GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
200			       &map, &stride);
201   rb->Data = map;
202   rb->RowStride = stride / _mesa_get_format_bytes(rb->Format);
203
204   intel_set_span_functions(intel, rb);
205}
206
207void
208intel_renderbuffer_unmap(struct intel_context *intel,
209			 struct gl_renderbuffer *rb)
210{
211   struct gl_context *ctx = &intel->ctx;
212   struct intel_renderbuffer *irb = intel_renderbuffer(rb);
213
214   if (!irb)
215      return;
216
217   if (!rb->Data) {
218      /* Renderbuffer is already unmapped. This usually happens when a single
219       * buffer is attached to the framebuffer's depth and stencil attachment
220       * points.
221       */
222      return;
223   }
224
225   ctx->Driver.UnmapRenderbuffer(ctx, rb);
226
227   rb->GetRow = NULL;
228   rb->PutRow = NULL;
229   rb->Data = NULL;
230   rb->RowStride = 0;
231}
232
233static void
234intel_framebuffer_map(struct intel_context *intel, struct gl_framebuffer *fb)
235{
236   int i;
237
238   for (i = 0; i < BUFFER_COUNT; i++) {
239      intel_renderbuffer_map(intel, fb->Attachment[i].Renderbuffer);
240   }
241
242   intel_check_front_buffer_rendering(intel);
243}
244
245static void
246intel_framebuffer_unmap(struct intel_context *intel, struct gl_framebuffer *fb)
247{
248   int i;
249
250   for (i = 0; i < BUFFER_COUNT; i++) {
251      intel_renderbuffer_unmap(intel, fb->Attachment[i].Renderbuffer);
252   }
253}
254
255/**
256 * Prepare for software rendering.  Map current read/draw framebuffers'
257 * renderbuffes and all currently bound texture objects.
258 *
259 * Old note: Moved locking out to get reasonable span performance.
260 */
261void
262intelSpanRenderStart(struct gl_context * ctx)
263{
264   struct intel_context *intel = intel_context(ctx);
265   GLuint i;
266
267   intel_flush(&intel->ctx);
268   intel_prepare_render(intel);
269
270   for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
271      if (ctx->Texture.Unit[i]._ReallyEnabled) {
272         struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
273
274         intel_finalize_mipmap_tree(intel, i);
275         intel_tex_map_images(intel, intel_texture_object(texObj),
276                              GL_MAP_READ_BIT | GL_MAP_WRITE_BIT);
277      }
278   }
279
280   intel_framebuffer_map(intel, ctx->DrawBuffer);
281   if (ctx->ReadBuffer != ctx->DrawBuffer) {
282      intel_framebuffer_map(intel, ctx->ReadBuffer);
283   }
284}
285
286/**
287 * Called when done software rendering.  Unmap the buffers we mapped in
288 * the above function.
289 */
290void
291intelSpanRenderFinish(struct gl_context * ctx)
292{
293   struct intel_context *intel = intel_context(ctx);
294   GLuint i;
295
296   _swrast_flush(ctx);
297
298   for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
299      if (ctx->Texture.Unit[i]._ReallyEnabled) {
300         struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
301         intel_tex_unmap_images(intel, intel_texture_object(texObj));
302      }
303   }
304
305   intel_framebuffer_unmap(intel, ctx->DrawBuffer);
306   if (ctx->ReadBuffer != ctx->DrawBuffer) {
307      intel_framebuffer_unmap(intel, ctx->ReadBuffer);
308   }
309}
310
311
312void
313intelInitSpanFuncs(struct gl_context * ctx)
314{
315   struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
316   swdd->SpanRenderStart = intelSpanRenderStart;
317   swdd->SpanRenderFinish = intelSpanRenderFinish;
318}
319
320void
321intel_map_vertex_shader_textures(struct gl_context *ctx)
322{
323   struct intel_context *intel = intel_context(ctx);
324   int i;
325
326   if (ctx->VertexProgram._Current == NULL)
327      return;
328
329   for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
330      if (ctx->Texture.Unit[i]._ReallyEnabled &&
331	  ctx->VertexProgram._Current->Base.TexturesUsed[i] != 0) {
332         struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
333
334         intel_tex_map_images(intel, intel_texture_object(texObj),
335                              GL_MAP_READ_BIT | GL_MAP_WRITE_BIT);
336      }
337   }
338}
339
340void
341intel_unmap_vertex_shader_textures(struct gl_context *ctx)
342{
343   struct intel_context *intel = intel_context(ctx);
344   int i;
345
346   if (ctx->VertexProgram._Current == NULL)
347      return;
348
349   for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
350      if (ctx->Texture.Unit[i]._ReallyEnabled &&
351	  ctx->VertexProgram._Current->Base.TexturesUsed[i] != 0) {
352         struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
353
354         intel_tex_unmap_images(intel, intel_texture_object(texObj));
355      }
356   }
357}
358
359typedef void (*span_init_func)(struct gl_renderbuffer *rb);
360
361static span_init_func intel_span_init_funcs[MESA_FORMAT_COUNT] =
362{
363   [MESA_FORMAT_A8] = intel_InitPointers_A8,
364   [MESA_FORMAT_RGB565] = intel_InitPointers_RGB565,
365   [MESA_FORMAT_ARGB4444] = intel_InitPointers_ARGB4444,
366   [MESA_FORMAT_ARGB1555] = intel_InitPointers_ARGB1555,
367   [MESA_FORMAT_XRGB8888] = intel_InitPointers_xRGB8888,
368   [MESA_FORMAT_ARGB8888] = intel_InitPointers_ARGB8888,
369   [MESA_FORMAT_SARGB8] = intel_InitPointers_ARGB8888,
370   [MESA_FORMAT_Z16] = _mesa_set_renderbuffer_accessors,
371   [MESA_FORMAT_X8_Z24] = _mesa_set_renderbuffer_accessors,
372   [MESA_FORMAT_S8_Z24] = _mesa_set_renderbuffer_accessors,
373   [MESA_FORMAT_S8] = _mesa_set_renderbuffer_accessors,
374   [MESA_FORMAT_R8] = _mesa_set_renderbuffer_accessors,
375   [MESA_FORMAT_RG88] = _mesa_set_renderbuffer_accessors,
376   [MESA_FORMAT_R16] = _mesa_set_renderbuffer_accessors,
377   [MESA_FORMAT_RG1616] = _mesa_set_renderbuffer_accessors,
378   [MESA_FORMAT_RGBA_FLOAT32] = _mesa_set_renderbuffer_accessors,
379   [MESA_FORMAT_RG_FLOAT32] = _mesa_set_renderbuffer_accessors,
380   [MESA_FORMAT_R_FLOAT32] = _mesa_set_renderbuffer_accessors,
381   [MESA_FORMAT_INTENSITY_FLOAT32] = _mesa_set_renderbuffer_accessors,
382   [MESA_FORMAT_LUMINANCE_FLOAT32] = _mesa_set_renderbuffer_accessors,
383};
384
385bool
386intel_span_supports_format(gl_format format)
387{
388   /* Rendering to/from integer textures will be done using MapRenderbuffer,
389    * rather than coding up new paths through GetRow/PutRow(), so claim support
390    * for those formats in here for now.
391    */
392   return (intel_span_init_funcs[format] != NULL ||
393	   _mesa_is_format_integer_color(format));
394}
395
396/**
397 * Plug in appropriate span read/write functions for the given renderbuffer.
398 * These are used for the software fallbacks.
399 */
400static void
401intel_set_span_functions(struct intel_context *intel,
402			 struct gl_renderbuffer *rb)
403{
404   struct intel_renderbuffer *irb = (struct intel_renderbuffer *) rb;
405
406   assert(intel_span_init_funcs[irb->Base.Format]);
407   intel_span_init_funcs[irb->Base.Format](rb);
408
409   if (rb->DataType == GL_NONE) {
410      _mesa_problem(NULL,
411		    "renderbuffer format %s is missing "
412		    "intel_mesa_format_to_rb_datatype() support.",
413		    _mesa_get_format_name(rb->Format));
414   }
415}
416