1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright 2011 Intel Corporation
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
28 * Chad Versace <chad@chad-versace.us>
30 **************************************************************************/
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"
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"
47 #include "swrast/swrast.h"
50 intel_set_span_functions(struct intel_context
*intel
,
51 struct gl_renderbuffer
*rb
);
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; \
67 #define HW_ENDCLIPLOOP()
69 #define Y_FLIP(_y) (_y)
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
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
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
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"
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"
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"
117 /* ------------------------------------------------------------------------- */
118 /* s8 stencil span and pixel functions */
119 /* ------------------------------------------------------------------------- */
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
126 #define HAVE_HW_STENCIL_SPANS 0
128 #define LOCAL_STENCIL_VARS \
132 int maxx = rb->Width; \
133 int maxy = rb->Height; \
134 int stride = rb->RowStride; \
135 uint8_t *buf = rb->Data; \
142 * \brief Get pointer offset into stencil buffer.
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).
148 * x | y | byte offset
149 * --------------------------
162 static inline intptr_t
163 intel_offset_S8(int stride
, GLint x
, GLint y
)
165 return 2 * ((y
/ 2) * stride
+ x
) + y
% 2;
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"
173 /* ------------------------------------------------------------------------- */
176 intel_renderbuffer_map(struct intel_context
*intel
, struct gl_renderbuffer
*rb
)
178 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
180 if (irb
== NULL
|| irb
->region
== NULL
)
183 drm_intel_gem_bo_map_gtt(irb
->region
->buffer
);
185 rb
->Data
= irb
->region
->buffer
->virtual;
186 rb
->RowStride
= irb
->region
->pitch
;
188 /* Flip orientation if it's the window system buffer */
190 rb
->Data
+= rb
->RowStride
* (irb
->region
->height
- 1) * irb
->region
->cpp
;
191 rb
->RowStride
= -rb
->RowStride
;
194 intel_set_span_functions(intel
, rb
);
198 intel_renderbuffer_unmap(struct intel_context
*intel
,
199 struct gl_renderbuffer
*rb
)
201 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
203 if (irb
== NULL
|| irb
->region
== NULL
)
206 drm_intel_gem_bo_unmap_gtt(irb
->region
->buffer
);
215 * Map or unmap all the renderbuffers which we may need during
216 * software rendering.
217 * XXX in the future, we could probably convey extra information to
218 * reduce the number of mappings needed. I.e. if doing a glReadPixels
219 * from the depth buffer, we really only need one mapping.
221 * XXX Rewrite this function someday.
222 * We can probably just loop over all the renderbuffer attachments,
223 * map/unmap all of them, and not worry about the _ColorDrawBuffers
224 * _ColorReadBuffer, _DepthBuffer or _StencilBuffer fields.
227 intel_map_unmap_framebuffer(struct intel_context
*intel
,
228 struct gl_framebuffer
*fb
,
233 /* color draw buffers */
234 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
236 intel_renderbuffer_map(intel
, fb
->_ColorDrawBuffers
[i
]);
238 intel_renderbuffer_unmap(intel
, fb
->_ColorDrawBuffers
[i
]);
241 /* color read buffer */
243 intel_renderbuffer_map(intel
, fb
->_ColorReadBuffer
);
245 intel_renderbuffer_unmap(intel
, fb
->_ColorReadBuffer
);
247 /* check for render to textures */
248 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
249 struct gl_renderbuffer_attachment
*att
=
251 struct gl_texture_object
*tex
= att
->Texture
;
253 /* render to texture */
254 ASSERT(att
->Renderbuffer
);
256 intel_tex_map_images(intel
, intel_texture_object(tex
));
258 intel_tex_unmap_images(intel
, intel_texture_object(tex
));
262 /* depth buffer (Note wrapper!) */
263 if (fb
->_DepthBuffer
) {
265 intel_renderbuffer_map(intel
, fb
->_DepthBuffer
->Wrapped
);
267 intel_renderbuffer_unmap(intel
, fb
->_DepthBuffer
->Wrapped
);
270 /* stencil buffer (Note wrapper!) */
271 if (fb
->_StencilBuffer
) {
273 intel_renderbuffer_map(intel
, fb
->_StencilBuffer
->Wrapped
);
275 intel_renderbuffer_unmap(intel
, fb
->_StencilBuffer
->Wrapped
);
278 intel_check_front_buffer_rendering(intel
);
282 * Prepare for software rendering. Map current read/draw framebuffers'
283 * renderbuffes and all currently bound texture objects.
285 * Old note: Moved locking out to get reasonable span performance.
288 intelSpanRenderStart(struct gl_context
* ctx
)
290 struct intel_context
*intel
= intel_context(ctx
);
293 intel_flush(&intel
->ctx
);
294 intel_prepare_render(intel
);
296 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
297 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
298 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
300 intel_finalize_mipmap_tree(intel
, i
);
301 intel_tex_map_images(intel
, intel_texture_object(texObj
));
305 intel_map_unmap_framebuffer(intel
, ctx
->DrawBuffer
, GL_TRUE
);
306 if (ctx
->ReadBuffer
!= ctx
->DrawBuffer
)
307 intel_map_unmap_framebuffer(intel
, ctx
->ReadBuffer
, GL_TRUE
);
311 * Called when done software rendering. Unmap the buffers we mapped in
312 * the above function.
315 intelSpanRenderFinish(struct gl_context
* ctx
)
317 struct intel_context
*intel
= intel_context(ctx
);
322 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
323 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
324 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
325 intel_tex_unmap_images(intel
, intel_texture_object(texObj
));
329 intel_map_unmap_framebuffer(intel
, ctx
->DrawBuffer
, GL_FALSE
);
330 if (ctx
->ReadBuffer
!= ctx
->DrawBuffer
)
331 intel_map_unmap_framebuffer(intel
, ctx
->ReadBuffer
, GL_FALSE
);
336 intelInitSpanFuncs(struct gl_context
* ctx
)
338 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference(ctx
);
339 swdd
->SpanRenderStart
= intelSpanRenderStart
;
340 swdd
->SpanRenderFinish
= intelSpanRenderFinish
;
344 intel_map_vertex_shader_textures(struct gl_context
*ctx
)
346 struct intel_context
*intel
= intel_context(ctx
);
349 if (ctx
->VertexProgram
._Current
== NULL
)
352 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
353 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
&&
354 ctx
->VertexProgram
._Current
->Base
.TexturesUsed
[i
] != 0) {
355 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
357 intel_tex_map_images(intel
, intel_texture_object(texObj
));
363 intel_unmap_vertex_shader_textures(struct gl_context
*ctx
)
365 struct intel_context
*intel
= intel_context(ctx
);
368 if (ctx
->VertexProgram
._Current
== NULL
)
371 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
372 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
&&
373 ctx
->VertexProgram
._Current
->Base
.TexturesUsed
[i
] != 0) {
374 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
376 intel_tex_unmap_images(intel
, intel_texture_object(texObj
));
381 typedef void (*span_init_func
)(struct gl_renderbuffer
*rb
);
383 static span_init_func intel_span_init_funcs
[MESA_FORMAT_COUNT
] =
385 [MESA_FORMAT_A8
] = intel_InitPointers_A8
,
386 [MESA_FORMAT_RGB565
] = intel_InitPointers_RGB565
,
387 [MESA_FORMAT_ARGB4444
] = intel_InitPointers_ARGB4444
,
388 [MESA_FORMAT_ARGB1555
] = intel_InitPointers_ARGB1555
,
389 [MESA_FORMAT_XRGB8888
] = intel_InitPointers_xRGB8888
,
390 [MESA_FORMAT_ARGB8888
] = intel_InitPointers_ARGB8888
,
391 [MESA_FORMAT_SARGB8
] = intel_InitPointers_ARGB8888
,
392 [MESA_FORMAT_Z16
] = _mesa_set_renderbuffer_accessors
,
393 [MESA_FORMAT_X8_Z24
] = _mesa_set_renderbuffer_accessors
,
394 [MESA_FORMAT_S8_Z24
] = _mesa_set_renderbuffer_accessors
,
395 [MESA_FORMAT_S8
] = intel_InitStencilPointers_S8
,
396 [MESA_FORMAT_R8
] = _mesa_set_renderbuffer_accessors
,
397 [MESA_FORMAT_RG88
] = _mesa_set_renderbuffer_accessors
,
398 [MESA_FORMAT_R16
] = _mesa_set_renderbuffer_accessors
,
399 [MESA_FORMAT_RG1616
] = _mesa_set_renderbuffer_accessors
,
400 [MESA_FORMAT_RGBA_FLOAT32
] = _mesa_set_renderbuffer_accessors
,
401 [MESA_FORMAT_RG_FLOAT32
] = _mesa_set_renderbuffer_accessors
,
402 [MESA_FORMAT_R_FLOAT32
] = _mesa_set_renderbuffer_accessors
,
403 [MESA_FORMAT_INTENSITY_FLOAT32
] = _mesa_set_renderbuffer_accessors
,
404 [MESA_FORMAT_LUMINANCE_FLOAT32
] = _mesa_set_renderbuffer_accessors
,
408 intel_span_supports_format(gl_format format
)
410 return intel_span_init_funcs
[format
] != NULL
;
414 * Plug in appropriate span read/write functions for the given renderbuffer.
415 * These are used for the software fallbacks.
418 intel_set_span_functions(struct intel_context
*intel
,
419 struct gl_renderbuffer
*rb
)
421 struct intel_renderbuffer
*irb
= (struct intel_renderbuffer
*) rb
;
423 assert(intel_span_init_funcs
[irb
->Base
.Format
]);
424 intel_span_init_funcs
[irb
->Base
.Format
](rb
);