1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "main/glheader.h"
29 #include "main/macros.h"
30 #include "main/mtypes.h"
31 #include "main/colormac.h"
33 #include "intel_buffers.h"
34 #include "intel_fbo.h"
35 #include "intel_screen.h"
36 #include "intel_span.h"
37 #include "intel_regions.h"
38 #include "intel_tex.h"
40 #include "swrast/swrast.h"
43 intel_set_span_functions(struct intel_context
*intel
,
44 struct gl_renderbuffer
*rb
);
46 #define SPAN_CACHE_SIZE 4096
49 get_span_cache(struct intel_renderbuffer
*irb
, uint32_t offset
)
51 if (irb
->span_cache
== NULL
) {
52 irb
->span_cache
= malloc(SPAN_CACHE_SIZE
);
53 irb
->span_cache_offset
= -1;
56 if ((offset
& ~(SPAN_CACHE_SIZE
- 1)) != irb
->span_cache_offset
) {
57 irb
->span_cache_offset
= offset
& ~(SPAN_CACHE_SIZE
- 1);
58 dri_bo_get_subdata(irb
->region
->buffer
, irb
->span_cache_offset
,
59 SPAN_CACHE_SIZE
, irb
->span_cache
);
64 clear_span_cache(struct intel_renderbuffer
*irb
)
66 irb
->span_cache_offset
= -1;
70 pread_32(struct intel_renderbuffer
*irb
, uint32_t offset
)
72 get_span_cache(irb
, offset
);
74 return *(uint32_t *)(irb
->span_cache
+ (offset
& (SPAN_CACHE_SIZE
- 1)));
78 pread_xrgb8888(struct intel_renderbuffer
*irb
, uint32_t offset
)
80 get_span_cache(irb
, offset
);
82 return *(uint32_t *)(irb
->span_cache
+ (offset
& (SPAN_CACHE_SIZE
- 1))) |
87 pread_16(struct intel_renderbuffer
*irb
, uint32_t offset
)
89 get_span_cache(irb
, offset
);
91 return *(uint16_t *)(irb
->span_cache
+ (offset
& (SPAN_CACHE_SIZE
- 1)));
95 pread_8(struct intel_renderbuffer
*irb
, uint32_t offset
)
97 get_span_cache(irb
, offset
);
99 return *(uint8_t *)(irb
->span_cache
+ (offset
& (SPAN_CACHE_SIZE
- 1)));
103 pwrite_32(struct intel_renderbuffer
*irb
, uint32_t offset
, uint32_t val
)
105 clear_span_cache(irb
);
107 dri_bo_subdata(irb
->region
->buffer
, offset
, 4, &val
);
111 pwrite_xrgb8888(struct intel_renderbuffer
*irb
, uint32_t offset
, uint32_t val
)
113 clear_span_cache(irb
);
115 dri_bo_subdata(irb
->region
->buffer
, offset
, 3, &val
);
119 pwrite_16(struct intel_renderbuffer
*irb
, uint32_t offset
, uint16_t val
)
121 clear_span_cache(irb
);
123 dri_bo_subdata(irb
->region
->buffer
, offset
, 2, &val
);
127 pwrite_8(struct intel_renderbuffer
*irb
, uint32_t offset
, uint8_t val
)
129 clear_span_cache(irb
);
131 dri_bo_subdata(irb
->region
->buffer
, offset
, 1, &val
);
134 static uint32_t no_tile_swizzle(struct intel_renderbuffer
*irb
,
137 return (y
* irb
->region
->pitch
+ x
) * irb
->region
->cpp
;
141 * Deal with tiled surfaces
144 static uint32_t x_tile_swizzle(struct intel_renderbuffer
*irb
,
149 int x_tile_off
, y_tile_off
;
150 int x_tile_number
, y_tile_number
;
151 int tile_off
, tile_base
;
153 x
+= irb
->region
->draw_x
;
154 y
+= irb
->region
->draw_y
;
156 tile_stride
= (irb
->region
->pitch
* irb
->region
->cpp
) << 3;
158 xbyte
= x
* irb
->region
->cpp
;
160 x_tile_off
= xbyte
& 0x1ff;
163 x_tile_number
= xbyte
>> 9;
164 y_tile_number
= y
>> 3;
166 tile_off
= (y_tile_off
<< 9) + x_tile_off
;
168 switch (irb
->region
->bit_6_swizzle
) {
169 case I915_BIT_6_SWIZZLE_NONE
:
171 case I915_BIT_6_SWIZZLE_9
:
172 tile_off
^= ((tile_off
>> 3) & 64);
174 case I915_BIT_6_SWIZZLE_9_10
:
175 tile_off
^= ((tile_off
>> 3) & 64) ^ ((tile_off
>> 4) & 64);
177 case I915_BIT_6_SWIZZLE_9_11
:
178 tile_off
^= ((tile_off
>> 3) & 64) ^ ((tile_off
>> 5) & 64);
180 case I915_BIT_6_SWIZZLE_9_10_11
:
181 tile_off
^= ((tile_off
>> 3) & 64) ^ ((tile_off
>> 4) & 64) ^
182 ((tile_off
>> 5) & 64);
185 fprintf(stderr
, "Unknown tile swizzling mode %d\n",
186 irb
->region
->bit_6_swizzle
);
190 tile_base
= (x_tile_number
<< 12) + y_tile_number
* tile_stride
;
193 printf("(%d,%d) -> %d + %d = %d (pitch = %d, tstride = %d)\n",
194 x
, y
, tile_off
, tile_base
,
195 tile_off
+ tile_base
,
196 irb
->region
->pitch
, tile_stride
);
199 return tile_base
+ tile_off
;
202 static uint32_t y_tile_swizzle(struct intel_renderbuffer
*irb
,
207 int x_tile_off
, y_tile_off
;
208 int x_tile_number
, y_tile_number
;
209 int tile_off
, tile_base
;
211 x
+= irb
->region
->draw_x
;
212 y
+= irb
->region
->draw_y
;
214 tile_stride
= (irb
->region
->pitch
* irb
->region
->cpp
) << 5;
216 xbyte
= x
* irb
->region
->cpp
;
218 x_tile_off
= xbyte
& 0x7f;
219 y_tile_off
= y
& 0x1f;
221 x_tile_number
= xbyte
>> 7;
222 y_tile_number
= y
>> 5;
224 tile_off
= ((x_tile_off
& ~0xf) << 5) + (y_tile_off
<< 4) +
227 switch (irb
->region
->bit_6_swizzle
) {
228 case I915_BIT_6_SWIZZLE_NONE
:
230 case I915_BIT_6_SWIZZLE_9
:
231 tile_off
^= ((tile_off
>> 3) & 64);
233 case I915_BIT_6_SWIZZLE_9_10
:
234 tile_off
^= ((tile_off
>> 3) & 64) ^ ((tile_off
>> 4) & 64);
236 case I915_BIT_6_SWIZZLE_9_11
:
237 tile_off
^= ((tile_off
>> 3) & 64) ^ ((tile_off
>> 5) & 64);
239 case I915_BIT_6_SWIZZLE_9_10_11
:
240 tile_off
^= ((tile_off
>> 3) & 64) ^ ((tile_off
>> 4) & 64) ^
241 ((tile_off
>> 5) & 64);
244 fprintf(stderr
, "Unknown tile swizzling mode %d\n",
245 irb
->region
->bit_6_swizzle
);
249 tile_base
= (x_tile_number
<< 12) + y_tile_number
* tile_stride
;
251 return tile_base
+ tile_off
;
255 break intelWriteRGBASpan_ARGB8888
262 struct intel_renderbuffer *irb = intel_renderbuffer(rb); \
263 const GLint yScale = ctx->DrawBuffer->Name ? 1 : -1; \
264 const GLint yBias = ctx->DrawBuffer->Name ? 0 : irb->Base.Height - 1;\
265 int minx = 0, miny = 0; \
266 int maxx = ctx->DrawBuffer->Width; \
267 int maxy = ctx->DrawBuffer->Height; \
268 int pitch = irb->region->pitch * irb->region->cpp; \
269 void *buf = irb->region->buffer->virtual; \
272 (void)buf; (void)pitch; /* unused for non-gttmap. */ \
274 #define HW_CLIPLOOP()
275 #define HW_ENDCLIPLOOP()
277 #define Y_FLIP(_y) ((_y) * yScale + yBias)
283 /* Convenience macros to avoid typing the swizzle argument over and over */
284 #define NO_TILE(_X, _Y) no_tile_swizzle(irb, (_X), (_Y))
285 #define X_TILE(_X, _Y) x_tile_swizzle(irb, (_X), (_Y))
286 #define Y_TILE(_X, _Y) y_tile_swizzle(irb, (_X), (_Y))
288 /* r5g6b5 color span and pixel functions */
289 #define INTEL_PIXEL_FMT GL_RGB
290 #define INTEL_PIXEL_TYPE GL_UNSIGNED_SHORT_5_6_5
291 #define INTEL_READ_VALUE(offset) pread_16(irb, offset)
292 #define INTEL_WRITE_VALUE(offset, v) pwrite_16(irb, offset, v)
293 #define INTEL_TAG(x) x##_RGB565
294 #include "intel_spantmp.h"
296 /* a4r4g4b4 color span and pixel functions */
297 #define INTEL_PIXEL_FMT GL_BGRA
298 #define INTEL_PIXEL_TYPE GL_UNSIGNED_SHORT_4_4_4_4_REV
299 #define INTEL_READ_VALUE(offset) pread_16(irb, offset)
300 #define INTEL_WRITE_VALUE(offset, v) pwrite_16(irb, offset, v)
301 #define INTEL_TAG(x) x##_ARGB4444
302 #include "intel_spantmp.h"
304 /* a1r5g5b5 color span and pixel functions */
305 #define INTEL_PIXEL_FMT GL_BGRA
306 #define INTEL_PIXEL_TYPE GL_UNSIGNED_SHORT_1_5_5_5_REV
307 #define INTEL_READ_VALUE(offset) pread_16(irb, offset)
308 #define INTEL_WRITE_VALUE(offset, v) pwrite_16(irb, offset, v)
309 #define INTEL_TAG(x) x##_ARGB1555
310 #include "intel_spantmp.h"
312 /* a8r8g8b8 color span and pixel functions */
313 #define INTEL_PIXEL_FMT GL_BGRA
314 #define INTEL_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
315 #define INTEL_READ_VALUE(offset) pread_32(irb, offset)
316 #define INTEL_WRITE_VALUE(offset, v) pwrite_32(irb, offset, v)
317 #define INTEL_TAG(x) x##_ARGB8888
318 #include "intel_spantmp.h"
320 /* x8r8g8b8 color span and pixel functions */
321 #define INTEL_PIXEL_FMT GL_BGR
322 #define INTEL_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
323 #define INTEL_READ_VALUE(offset) pread_xrgb8888(irb, offset)
324 #define INTEL_WRITE_VALUE(offset, v) pwrite_xrgb8888(irb, offset, v)
325 #define INTEL_TAG(x) x##_xRGB8888
326 #include "intel_spantmp.h"
328 #define LOCAL_DEPTH_VARS \
329 struct intel_renderbuffer *irb = intel_renderbuffer(rb); \
330 const GLint yScale = ctx->DrawBuffer->Name ? 1 : -1; \
331 const GLint yBias = ctx->DrawBuffer->Name ? 0 : irb->Base.Height - 1;\
332 int minx = 0, miny = 0; \
333 int maxx = ctx->DrawBuffer->Width; \
334 int maxy = ctx->DrawBuffer->Height; \
335 int pitch = irb->region->pitch * irb->region->cpp; \
336 void *buf = irb->region->buffer->virtual; \
337 (void)buf; (void)pitch; /* unused for non-gttmap. */ \
339 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
341 /* z16 depthbuffer functions. */
342 #define INTEL_VALUE_TYPE GLushort
343 #define INTEL_WRITE_DEPTH(offset, d) pwrite_16(irb, offset, d)
344 #define INTEL_READ_DEPTH(offset) pread_16(irb, offset)
345 #define INTEL_TAG(name) name##_z16
346 #include "intel_depthtmp.h"
348 /* z24x8 depthbuffer functions. */
349 #define INTEL_VALUE_TYPE GLuint
350 #define INTEL_WRITE_DEPTH(offset, d) pwrite_32(irb, offset, d)
351 #define INTEL_READ_DEPTH(offset) pread_32(irb, offset)
352 #define INTEL_TAG(name) name##_z24_x8
353 #include "intel_depthtmp.h"
356 intel_renderbuffer_map(struct intel_context
*intel
, struct gl_renderbuffer
*rb
)
358 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
360 if (irb
== NULL
|| irb
->region
== NULL
)
363 if (intel
->intelScreen
->kernel_exec_fencing
)
364 drm_intel_gem_bo_map_gtt(irb
->region
->buffer
);
366 intel_set_span_functions(intel
, rb
);
370 intel_renderbuffer_unmap(struct intel_context
*intel
,
371 struct gl_renderbuffer
*rb
)
373 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
375 if (irb
== NULL
|| irb
->region
== NULL
)
378 if (intel
->intelScreen
->kernel_exec_fencing
)
379 drm_intel_gem_bo_unmap_gtt(irb
->region
->buffer
);
381 clear_span_cache(irb
);
388 * Map or unmap all the renderbuffers which we may need during
389 * software rendering.
390 * XXX in the future, we could probably convey extra information to
391 * reduce the number of mappings needed. I.e. if doing a glReadPixels
392 * from the depth buffer, we really only need one mapping.
394 * XXX Rewrite this function someday.
395 * We can probably just loop over all the renderbuffer attachments,
396 * map/unmap all of them, and not worry about the _ColorDrawBuffers
397 * _ColorReadBuffer, _DepthBuffer or _StencilBuffer fields.
400 intel_map_unmap_framebuffer(struct intel_context
*intel
,
401 struct gl_framebuffer
*fb
,
406 /* color draw buffers */
407 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
409 intel_renderbuffer_map(intel
, fb
->_ColorDrawBuffers
[i
]);
411 intel_renderbuffer_unmap(intel
, fb
->_ColorDrawBuffers
[i
]);
414 /* color read buffer */
416 intel_renderbuffer_map(intel
, fb
->_ColorReadBuffer
);
418 intel_renderbuffer_unmap(intel
, fb
->_ColorReadBuffer
);
420 /* check for render to textures */
421 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
422 struct gl_renderbuffer_attachment
*att
=
424 struct gl_texture_object
*tex
= att
->Texture
;
426 /* render to texture */
427 ASSERT(att
->Renderbuffer
);
429 intel_tex_map_images(intel
, intel_texture_object(tex
));
431 intel_tex_unmap_images(intel
, intel_texture_object(tex
));
435 /* depth buffer (Note wrapper!) */
436 if (fb
->_DepthBuffer
) {
438 intel_renderbuffer_map(intel
, fb
->_DepthBuffer
->Wrapped
);
440 intel_renderbuffer_unmap(intel
, fb
->_DepthBuffer
->Wrapped
);
443 /* stencil buffer (Note wrapper!) */
444 if (fb
->_StencilBuffer
) {
446 intel_renderbuffer_map(intel
, fb
->_StencilBuffer
->Wrapped
);
448 intel_renderbuffer_unmap(intel
, fb
->_StencilBuffer
->Wrapped
);
451 intel_check_front_buffer_rendering(intel
);
455 * Prepare for software rendering. Map current read/draw framebuffers'
456 * renderbuffes and all currently bound texture objects.
458 * Old note: Moved locking out to get reasonable span performance.
461 intelSpanRenderStart(GLcontext
* ctx
)
463 struct intel_context
*intel
= intel_context(ctx
);
466 intelFlush(&intel
->ctx
);
467 intel_prepare_render(intel
);
469 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
470 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
471 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
472 intel_tex_map_images(intel
, intel_texture_object(texObj
));
476 intel_map_unmap_framebuffer(intel
, ctx
->DrawBuffer
, GL_TRUE
);
477 if (ctx
->ReadBuffer
!= ctx
->DrawBuffer
)
478 intel_map_unmap_framebuffer(intel
, ctx
->ReadBuffer
, GL_TRUE
);
482 * Called when done software rendering. Unmap the buffers we mapped in
483 * the above function.
486 intelSpanRenderFinish(GLcontext
* ctx
)
488 struct intel_context
*intel
= intel_context(ctx
);
493 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
494 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
495 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
496 intel_tex_unmap_images(intel
, intel_texture_object(texObj
));
500 intel_map_unmap_framebuffer(intel
, ctx
->DrawBuffer
, GL_FALSE
);
501 if (ctx
->ReadBuffer
!= ctx
->DrawBuffer
)
502 intel_map_unmap_framebuffer(intel
, ctx
->ReadBuffer
, GL_FALSE
);
507 intelInitSpanFuncs(GLcontext
* ctx
)
509 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference(ctx
);
510 swdd
->SpanRenderStart
= intelSpanRenderStart
;
511 swdd
->SpanRenderFinish
= intelSpanRenderFinish
;
515 intel_map_vertex_shader_textures(GLcontext
*ctx
)
517 struct intel_context
*intel
= intel_context(ctx
);
520 if (ctx
->VertexProgram
._Current
== NULL
)
523 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
524 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
&&
525 ctx
->VertexProgram
._Current
->Base
.TexturesUsed
[i
] != 0) {
526 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
528 intel_tex_map_images(intel
, intel_texture_object(texObj
));
534 intel_unmap_vertex_shader_textures(GLcontext
*ctx
)
536 struct intel_context
*intel
= intel_context(ctx
);
539 if (ctx
->VertexProgram
._Current
== NULL
)
542 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
543 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
&&
544 ctx
->VertexProgram
._Current
->Base
.TexturesUsed
[i
] != 0) {
545 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
547 intel_tex_unmap_images(intel
, intel_texture_object(texObj
));
553 * Plug in appropriate span read/write functions for the given renderbuffer.
554 * These are used for the software fallbacks.
557 intel_set_span_functions(struct intel_context
*intel
,
558 struct gl_renderbuffer
*rb
)
560 struct intel_renderbuffer
*irb
= (struct intel_renderbuffer
*) rb
;
561 uint32_t tiling
= irb
->region
->tiling
;
563 if (intel
->intelScreen
->kernel_exec_fencing
) {
564 switch (irb
->Base
.Format
) {
565 case MESA_FORMAT_RGB565
:
566 intel_gttmap_InitPointers_RGB565(rb
);
568 case MESA_FORMAT_ARGB4444
:
569 intel_gttmap_InitPointers_ARGB4444(rb
);
571 case MESA_FORMAT_ARGB1555
:
572 intel_gttmap_InitPointers_ARGB1555(rb
);
574 case MESA_FORMAT_XRGB8888
:
575 intel_gttmap_InitPointers_xRGB8888(rb
);
577 case MESA_FORMAT_ARGB8888
:
578 intel_gttmap_InitPointers_ARGB8888(rb
);
580 case MESA_FORMAT_Z16
:
581 intel_gttmap_InitDepthPointers_z16(rb
);
583 case MESA_FORMAT_X8_Z24
:
584 case MESA_FORMAT_S8_Z24
:
585 intel_gttmap_InitDepthPointers_z24_x8(rb
);
589 "Unexpected MesaFormat %d in intelSetSpanFunctions",
596 /* If in GEM mode, we need to do the tile address swizzling ourselves,
597 * instead of the fence registers handling it.
599 switch (irb
->Base
.Format
) {
600 case MESA_FORMAT_RGB565
:
602 case I915_TILING_NONE
:
604 intelInitPointers_RGB565(rb
);
607 intel_XTile_InitPointers_RGB565(rb
);
610 intel_YTile_InitPointers_RGB565(rb
);
614 case MESA_FORMAT_ARGB4444
:
616 case I915_TILING_NONE
:
618 intelInitPointers_ARGB4444(rb
);
621 intel_XTile_InitPointers_ARGB4444(rb
);
624 intel_YTile_InitPointers_ARGB4444(rb
);
628 case MESA_FORMAT_ARGB1555
:
630 case I915_TILING_NONE
:
632 intelInitPointers_ARGB1555(rb
);
635 intel_XTile_InitPointers_ARGB1555(rb
);
638 intel_YTile_InitPointers_ARGB1555(rb
);
642 case MESA_FORMAT_XRGB8888
:
644 case I915_TILING_NONE
:
646 intelInitPointers_xRGB8888(rb
);
649 intel_XTile_InitPointers_xRGB8888(rb
);
652 intel_YTile_InitPointers_xRGB8888(rb
);
656 case MESA_FORMAT_ARGB8888
:
659 case I915_TILING_NONE
:
661 intelInitPointers_ARGB8888(rb
);
664 intel_XTile_InitPointers_ARGB8888(rb
);
667 intel_YTile_InitPointers_ARGB8888(rb
);
671 case MESA_FORMAT_Z16
:
673 case I915_TILING_NONE
:
675 intelInitDepthPointers_z16(rb
);
678 intel_XTile_InitDepthPointers_z16(rb
);
681 intel_YTile_InitDepthPointers_z16(rb
);
685 case MESA_FORMAT_X8_Z24
:
686 case MESA_FORMAT_S8_Z24
:
687 /* There are a few different ways SW asks us to access the S8Z24 data:
688 * Z24 depth-only depth reads
690 * S8Z24 stencil reads.
692 if (rb
->Format
== MESA_FORMAT_S8_Z24
) {
694 case I915_TILING_NONE
:
696 intelInitDepthPointers_z24_x8(rb
);
699 intel_XTile_InitDepthPointers_z24_x8(rb
);
702 intel_YTile_InitDepthPointers_z24_x8(rb
);
707 "Unexpected ActualFormat in intelSetSpanFunctions");
712 "Unexpected MesaFormat in intelSetSpanFunctions");