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
);
50 struct intel_renderbuffer *irb = intel_renderbuffer(rb); \
51 const GLint yScale = ctx->DrawBuffer->Name ? 1 : -1; \
52 const GLint yBias = ctx->DrawBuffer->Name ? 0 : irb->Base.Height - 1;\
53 int minx = 0, miny = 0; \
54 int maxx = ctx->DrawBuffer->Width; \
55 int maxy = ctx->DrawBuffer->Height; \
56 int pitch = irb->region->pitch * irb->region->cpp; \
57 void *buf = irb->region->buffer->virtual; \
60 (void)buf; (void)pitch; /* unused for non-gttmap. */ \
63 #define HW_ENDCLIPLOOP()
65 #define Y_FLIP(_y) ((_y) * yScale + yBias)
71 /* Convenience macros to avoid typing the address argument over and over */
72 #define NO_TILE(_X, _Y) (((_Y) * irb->region->pitch + (_X)) * irb->region->cpp)
74 /* r5g6b5 color span and pixel functions */
75 #define SPANTMP_PIXEL_FMT GL_RGB
76 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_5_6_5
77 #define TAG(x) intel_##x##_RGB565
78 #define TAG2(x,y) intel_##x##y_RGB565
81 /* a4r4g4b4 color span and pixel functions */
82 #define SPANTMP_PIXEL_FMT GL_BGRA
83 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_4_4_4_4_REV
84 #define TAG(x) intel_##x##_ARGB4444
85 #define TAG2(x,y) intel_##x##y_ARGB4444
88 /* a1r5g5b5 color span and pixel functions */
89 #define SPANTMP_PIXEL_FMT GL_BGRA
90 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_1_5_5_5_REV
91 #define TAG(x) intel_##x##_ARGB1555
92 #define TAG2(x,y) intel_##x##y##_ARGB1555
95 /* a8r8g8b8 color span and pixel functions */
96 #define SPANTMP_PIXEL_FMT GL_BGRA
97 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
98 #define TAG(x) intel_##x##_ARGB8888
99 #define TAG2(x,y) intel_##x##y##_ARGB8888
100 #include "spantmp2.h"
102 /* x8r8g8b8 color span and pixel functions */
103 #define SPANTMP_PIXEL_FMT GL_BGR
104 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
105 #define TAG(x) intel_##x##_xRGB8888
106 #define TAG2(x,y) intel_##x##y##_xRGB8888
107 #include "spantmp2.h"
109 #define LOCAL_DEPTH_VARS \
110 struct intel_renderbuffer *irb = intel_renderbuffer(rb); \
111 const GLint yScale = ctx->DrawBuffer->Name ? 1 : -1; \
112 const GLint yBias = ctx->DrawBuffer->Name ? 0 : irb->Base.Height - 1;\
113 int minx = 0, miny = 0; \
114 int maxx = ctx->DrawBuffer->Width; \
115 int maxy = ctx->DrawBuffer->Height; \
116 int pitch = irb->region->pitch * irb->region->cpp; \
117 void *buf = irb->region->buffer->virtual; \
118 (void)buf; (void)pitch; /* unused for non-gttmap. */ \
120 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
122 /* z16 depthbuffer functions. */
123 #define VALUE_TYPE GLushort
124 #define WRITE_DEPTH(_x, _y, d) \
125 (*(uint16_t *)(irb->region->buffer->virtual + NO_TILE(_x, _y)) = d)
126 #define READ_DEPTH(d, _x, _y) \
127 d = *(uint16_t *)(irb->region->buffer->virtual + NO_TILE(_x, _y))
128 #define TAG(x) intel_##x##_z16
129 #include "depthtmp.h"
131 /* z24_s8 and z24_x8 depthbuffer functions. */
132 #define VALUE_TYPE GLuint
133 #define WRITE_DEPTH(_x, _y, d) \
134 (*(uint32_t *)(irb->region->buffer->virtual + NO_TILE(_x, _y)) = d)
135 #define READ_DEPTH(d, _x, _y) \
136 d = *(uint32_t *)(irb->region->buffer->virtual + NO_TILE(_x, _y))
137 #define TAG(x) intel_##x##_z24_x8
138 #include "depthtmp.h"
141 intel_renderbuffer_map(struct intel_context
*intel
, struct gl_renderbuffer
*rb
)
143 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
145 if (irb
== NULL
|| irb
->region
== NULL
)
148 drm_intel_gem_bo_map_gtt(irb
->region
->buffer
);
150 intel_set_span_functions(intel
, rb
);
154 intel_renderbuffer_unmap(struct intel_context
*intel
,
155 struct gl_renderbuffer
*rb
)
157 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
159 if (irb
== NULL
|| irb
->region
== NULL
)
162 drm_intel_gem_bo_unmap_gtt(irb
->region
->buffer
);
169 * Map or unmap all the renderbuffers which we may need during
170 * software rendering.
171 * XXX in the future, we could probably convey extra information to
172 * reduce the number of mappings needed. I.e. if doing a glReadPixels
173 * from the depth buffer, we really only need one mapping.
175 * XXX Rewrite this function someday.
176 * We can probably just loop over all the renderbuffer attachments,
177 * map/unmap all of them, and not worry about the _ColorDrawBuffers
178 * _ColorReadBuffer, _DepthBuffer or _StencilBuffer fields.
181 intel_map_unmap_framebuffer(struct intel_context
*intel
,
182 struct gl_framebuffer
*fb
,
187 /* color draw buffers */
188 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
190 intel_renderbuffer_map(intel
, fb
->_ColorDrawBuffers
[i
]);
192 intel_renderbuffer_unmap(intel
, fb
->_ColorDrawBuffers
[i
]);
195 /* color read buffer */
197 intel_renderbuffer_map(intel
, fb
->_ColorReadBuffer
);
199 intel_renderbuffer_unmap(intel
, fb
->_ColorReadBuffer
);
201 /* check for render to textures */
202 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
203 struct gl_renderbuffer_attachment
*att
=
205 struct gl_texture_object
*tex
= att
->Texture
;
207 /* render to texture */
208 ASSERT(att
->Renderbuffer
);
210 intel_tex_map_images(intel
, intel_texture_object(tex
));
212 intel_tex_unmap_images(intel
, intel_texture_object(tex
));
216 /* depth buffer (Note wrapper!) */
217 if (fb
->_DepthBuffer
) {
219 intel_renderbuffer_map(intel
, fb
->_DepthBuffer
->Wrapped
);
221 intel_renderbuffer_unmap(intel
, fb
->_DepthBuffer
->Wrapped
);
224 /* stencil buffer (Note wrapper!) */
225 if (fb
->_StencilBuffer
) {
227 intel_renderbuffer_map(intel
, fb
->_StencilBuffer
->Wrapped
);
229 intel_renderbuffer_unmap(intel
, fb
->_StencilBuffer
->Wrapped
);
232 intel_check_front_buffer_rendering(intel
);
236 * Prepare for software rendering. Map current read/draw framebuffers'
237 * renderbuffes and all currently bound texture objects.
239 * Old note: Moved locking out to get reasonable span performance.
242 intelSpanRenderStart(GLcontext
* ctx
)
244 struct intel_context
*intel
= intel_context(ctx
);
247 intelFlush(&intel
->ctx
);
248 intel_prepare_render(intel
);
250 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
251 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
252 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
253 intel_tex_map_images(intel
, intel_texture_object(texObj
));
257 intel_map_unmap_framebuffer(intel
, ctx
->DrawBuffer
, GL_TRUE
);
258 if (ctx
->ReadBuffer
!= ctx
->DrawBuffer
)
259 intel_map_unmap_framebuffer(intel
, ctx
->ReadBuffer
, GL_TRUE
);
263 * Called when done software rendering. Unmap the buffers we mapped in
264 * the above function.
267 intelSpanRenderFinish(GLcontext
* ctx
)
269 struct intel_context
*intel
= intel_context(ctx
);
274 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
275 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
276 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
277 intel_tex_unmap_images(intel
, intel_texture_object(texObj
));
281 intel_map_unmap_framebuffer(intel
, ctx
->DrawBuffer
, GL_FALSE
);
282 if (ctx
->ReadBuffer
!= ctx
->DrawBuffer
)
283 intel_map_unmap_framebuffer(intel
, ctx
->ReadBuffer
, GL_FALSE
);
288 intelInitSpanFuncs(GLcontext
* ctx
)
290 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference(ctx
);
291 swdd
->SpanRenderStart
= intelSpanRenderStart
;
292 swdd
->SpanRenderFinish
= intelSpanRenderFinish
;
296 intel_map_vertex_shader_textures(GLcontext
*ctx
)
298 struct intel_context
*intel
= intel_context(ctx
);
301 if (ctx
->VertexProgram
._Current
== NULL
)
304 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
305 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
&&
306 ctx
->VertexProgram
._Current
->Base
.TexturesUsed
[i
] != 0) {
307 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
309 intel_tex_map_images(intel
, intel_texture_object(texObj
));
315 intel_unmap_vertex_shader_textures(GLcontext
*ctx
)
317 struct intel_context
*intel
= intel_context(ctx
);
320 if (ctx
->VertexProgram
._Current
== NULL
)
323 for (i
= 0; i
< ctx
->Const
.MaxTextureImageUnits
; i
++) {
324 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
&&
325 ctx
->VertexProgram
._Current
->Base
.TexturesUsed
[i
] != 0) {
326 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[i
]._Current
;
328 intel_tex_unmap_images(intel
, intel_texture_object(texObj
));
334 * Plug in appropriate span read/write functions for the given renderbuffer.
335 * These are used for the software fallbacks.
338 intel_set_span_functions(struct intel_context
*intel
,
339 struct gl_renderbuffer
*rb
)
341 struct intel_renderbuffer
*irb
= (struct intel_renderbuffer
*) rb
;
343 switch (irb
->Base
.Format
) {
344 case MESA_FORMAT_RGB565
:
345 intel_InitPointers_RGB565(rb
);
347 case MESA_FORMAT_ARGB4444
:
348 intel_InitPointers_ARGB4444(rb
);
350 case MESA_FORMAT_ARGB1555
:
351 intel_InitPointers_ARGB1555(rb
);
353 case MESA_FORMAT_XRGB8888
:
354 intel_InitPointers_xRGB8888(rb
);
356 case MESA_FORMAT_ARGB8888
:
357 intel_InitPointers_ARGB8888(rb
);
359 case MESA_FORMAT_Z16
:
360 intel_InitDepthPointers_z16(rb
);
362 case MESA_FORMAT_X8_Z24
:
363 case MESA_FORMAT_S8_Z24
:
364 intel_InitDepthPointers_z24_x8(rb
);
368 "Unexpected MesaFormat %d in intelSetSpanFunctions",