Merge branch 'draw-instanced'
[mesa.git] / src / mesa / drivers / dri / intel / intel_span.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include <stdbool.h>
29 #include "main/glheader.h"
30 #include "main/macros.h"
31 #include "main/mtypes.h"
32 #include "main/colormac.h"
33
34 #include "intel_buffers.h"
35 #include "intel_fbo.h"
36 #include "intel_screen.h"
37 #include "intel_span.h"
38 #include "intel_regions.h"
39 #include "intel_tex.h"
40
41 #include "swrast/swrast.h"
42
43 static void
44 intel_set_span_functions(struct intel_context *intel,
45 struct gl_renderbuffer *rb);
46
47 #undef DBG
48 #define DBG 0
49
50 #define LOCAL_VARS \
51 struct intel_renderbuffer *irb = intel_renderbuffer(rb); \
52 const GLint yScale = rb->Name ? 1 : -1; \
53 const GLint yBias = rb->Name ? 0 : rb->Height - 1; \
54 int minx = 0, miny = 0; \
55 int maxx = rb->Width; \
56 int maxy = rb->Height; \
57 int pitch = irb->region->pitch * irb->region->cpp; \
58 void *buf = irb->region->buffer->virtual; \
59 GLuint p; \
60 (void) p; \
61 (void)buf; (void)pitch; /* unused for non-gttmap. */ \
62
63 #define HW_CLIPLOOP()
64 #define HW_ENDCLIPLOOP()
65
66 #define Y_FLIP(_y) ((_y) * yScale + yBias)
67
68 #define HW_LOCK()
69
70 #define HW_UNLOCK()
71
72 /* Convenience macros to avoid typing the address argument over and over */
73 #define NO_TILE(_X, _Y) (((_Y) * irb->region->pitch + (_X)) * irb->region->cpp)
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 #define SPANTMP_MESA_FMT MESA_FORMAT_R8
118 #define TAG(x) intel_##x##_R8
119 #define TAG2(x,y) intel_##x##y##_R8
120 #include "spantmp2.h"
121
122 #define SPANTMP_MESA_FMT MESA_FORMAT_RG88
123 #define TAG(x) intel_##x##_RG88
124 #define TAG2(x,y) intel_##x##y##_RG88
125 #include "spantmp2.h"
126
127 #define SPANTMP_MESA_FMT MESA_FORMAT_R16
128 #define TAG(x) intel_##x##_R16
129 #define TAG2(x,y) intel_##x##y##_R16
130 #include "spantmp2.h"
131
132 #define SPANTMP_MESA_FMT MESA_FORMAT_RG1616
133 #define TAG(x) intel_##x##_RG1616
134 #define TAG2(x,y) intel_##x##y##_RG1616
135 #include "spantmp2.h"
136
137 #define LOCAL_DEPTH_VARS \
138 struct intel_renderbuffer *irb = intel_renderbuffer(rb); \
139 const GLint yScale = rb->Name ? 1 : -1; \
140 const GLint yBias = rb->Name ? 0 : rb->Height - 1; \
141 int minx = 0, miny = 0; \
142 int maxx = rb->Width; \
143 int maxy = rb->Height; \
144 int pitch = irb->region->pitch * irb->region->cpp; \
145 void *buf = irb->region->buffer->virtual; \
146 (void)buf; (void)pitch; /* unused for non-gttmap. */ \
147
148 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
149
150 /* z16 depthbuffer functions. */
151 #define VALUE_TYPE GLushort
152 #define WRITE_DEPTH(_x, _y, d) \
153 (*(uint16_t *)(irb->region->buffer->virtual + NO_TILE(_x, _y)) = d)
154 #define READ_DEPTH(d, _x, _y) \
155 d = *(uint16_t *)(irb->region->buffer->virtual + NO_TILE(_x, _y))
156 #define TAG(x) intel_##x##_z16
157 #include "depthtmp.h"
158
159 /* z24_s8 and z24_x8 depthbuffer functions. */
160 #define VALUE_TYPE GLuint
161 #define WRITE_DEPTH(_x, _y, d) \
162 (*(uint32_t *)(irb->region->buffer->virtual + NO_TILE(_x, _y)) = d)
163 #define READ_DEPTH(d, _x, _y) \
164 d = *(uint32_t *)(irb->region->buffer->virtual + NO_TILE(_x, _y))
165 #define TAG(x) intel_##x##_z24_s8
166 #include "depthtmp.h"
167
168 void
169 intel_renderbuffer_map(struct intel_context *intel, struct gl_renderbuffer *rb)
170 {
171 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
172
173 if (irb == NULL || irb->region == NULL)
174 return;
175
176 drm_intel_gem_bo_map_gtt(irb->region->buffer);
177
178 intel_set_span_functions(intel, rb);
179 }
180
181 void
182 intel_renderbuffer_unmap(struct intel_context *intel,
183 struct gl_renderbuffer *rb)
184 {
185 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
186
187 if (irb == NULL || irb->region == NULL)
188 return;
189
190 drm_intel_gem_bo_unmap_gtt(irb->region->buffer);
191
192 rb->GetRow = NULL;
193 rb->PutRow = NULL;
194 }
195
196 /**
197 * Map or unmap all the renderbuffers which we may need during
198 * software rendering.
199 * XXX in the future, we could probably convey extra information to
200 * reduce the number of mappings needed. I.e. if doing a glReadPixels
201 * from the depth buffer, we really only need one mapping.
202 *
203 * XXX Rewrite this function someday.
204 * We can probably just loop over all the renderbuffer attachments,
205 * map/unmap all of them, and not worry about the _ColorDrawBuffers
206 * _ColorReadBuffer, _DepthBuffer or _StencilBuffer fields.
207 */
208 static void
209 intel_map_unmap_framebuffer(struct intel_context *intel,
210 struct gl_framebuffer *fb,
211 GLboolean map)
212 {
213 GLuint i;
214
215 /* color draw buffers */
216 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
217 if (map)
218 intel_renderbuffer_map(intel, fb->_ColorDrawBuffers[i]);
219 else
220 intel_renderbuffer_unmap(intel, fb->_ColorDrawBuffers[i]);
221 }
222
223 /* color read buffer */
224 if (map)
225 intel_renderbuffer_map(intel, fb->_ColorReadBuffer);
226 else
227 intel_renderbuffer_unmap(intel, fb->_ColorReadBuffer);
228
229 /* check for render to textures */
230 for (i = 0; i < BUFFER_COUNT; i++) {
231 struct gl_renderbuffer_attachment *att =
232 fb->Attachment + i;
233 struct gl_texture_object *tex = att->Texture;
234 if (tex) {
235 /* render to texture */
236 ASSERT(att->Renderbuffer);
237 if (map)
238 intel_tex_map_images(intel, intel_texture_object(tex));
239 else
240 intel_tex_unmap_images(intel, intel_texture_object(tex));
241 }
242 }
243
244 /* depth buffer (Note wrapper!) */
245 if (fb->_DepthBuffer) {
246 if (map)
247 intel_renderbuffer_map(intel, fb->_DepthBuffer->Wrapped);
248 else
249 intel_renderbuffer_unmap(intel, fb->_DepthBuffer->Wrapped);
250 }
251
252 /* stencil buffer (Note wrapper!) */
253 if (fb->_StencilBuffer) {
254 if (map)
255 intel_renderbuffer_map(intel, fb->_StencilBuffer->Wrapped);
256 else
257 intel_renderbuffer_unmap(intel, fb->_StencilBuffer->Wrapped);
258 }
259
260 intel_check_front_buffer_rendering(intel);
261 }
262
263 /**
264 * Prepare for software rendering. Map current read/draw framebuffers'
265 * renderbuffes and all currently bound texture objects.
266 *
267 * Old note: Moved locking out to get reasonable span performance.
268 */
269 void
270 intelSpanRenderStart(struct gl_context * ctx)
271 {
272 struct intel_context *intel = intel_context(ctx);
273 GLuint i;
274
275 intel_flush(&intel->ctx);
276 intel_prepare_render(intel);
277
278 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
279 if (ctx->Texture.Unit[i]._ReallyEnabled) {
280 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
281
282 intel_finalize_mipmap_tree(intel, i);
283 intel_tex_map_images(intel, intel_texture_object(texObj));
284 }
285 }
286
287 intel_map_unmap_framebuffer(intel, ctx->DrawBuffer, GL_TRUE);
288 if (ctx->ReadBuffer != ctx->DrawBuffer)
289 intel_map_unmap_framebuffer(intel, ctx->ReadBuffer, GL_TRUE);
290 }
291
292 /**
293 * Called when done software rendering. Unmap the buffers we mapped in
294 * the above function.
295 */
296 void
297 intelSpanRenderFinish(struct gl_context * ctx)
298 {
299 struct intel_context *intel = intel_context(ctx);
300 GLuint i;
301
302 _swrast_flush(ctx);
303
304 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
305 if (ctx->Texture.Unit[i]._ReallyEnabled) {
306 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
307 intel_tex_unmap_images(intel, intel_texture_object(texObj));
308 }
309 }
310
311 intel_map_unmap_framebuffer(intel, ctx->DrawBuffer, GL_FALSE);
312 if (ctx->ReadBuffer != ctx->DrawBuffer)
313 intel_map_unmap_framebuffer(intel, ctx->ReadBuffer, GL_FALSE);
314 }
315
316
317 void
318 intelInitSpanFuncs(struct gl_context * ctx)
319 {
320 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
321 swdd->SpanRenderStart = intelSpanRenderStart;
322 swdd->SpanRenderFinish = intelSpanRenderFinish;
323 }
324
325 void
326 intel_map_vertex_shader_textures(struct gl_context *ctx)
327 {
328 struct intel_context *intel = intel_context(ctx);
329 int i;
330
331 if (ctx->VertexProgram._Current == NULL)
332 return;
333
334 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
335 if (ctx->Texture.Unit[i]._ReallyEnabled &&
336 ctx->VertexProgram._Current->Base.TexturesUsed[i] != 0) {
337 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
338
339 intel_tex_map_images(intel, intel_texture_object(texObj));
340 }
341 }
342 }
343
344 void
345 intel_unmap_vertex_shader_textures(struct gl_context *ctx)
346 {
347 struct intel_context *intel = intel_context(ctx);
348 int i;
349
350 if (ctx->VertexProgram._Current == NULL)
351 return;
352
353 for (i = 0; i < ctx->Const.MaxTextureImageUnits; i++) {
354 if (ctx->Texture.Unit[i]._ReallyEnabled &&
355 ctx->VertexProgram._Current->Base.TexturesUsed[i] != 0) {
356 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
357
358 intel_tex_unmap_images(intel, intel_texture_object(texObj));
359 }
360 }
361 }
362
363 typedef void (*span_init_func)(struct gl_renderbuffer *rb);
364
365 static span_init_func intel_span_init_funcs[MESA_FORMAT_COUNT] =
366 {
367 [MESA_FORMAT_A8] = intel_InitPointers_A8,
368 [MESA_FORMAT_RGB565] = intel_InitPointers_RGB565,
369 [MESA_FORMAT_ARGB4444] = intel_InitPointers_ARGB4444,
370 [MESA_FORMAT_ARGB1555] = intel_InitPointers_ARGB1555,
371 [MESA_FORMAT_XRGB8888] = intel_InitPointers_xRGB8888,
372 [MESA_FORMAT_ARGB8888] = intel_InitPointers_ARGB8888,
373 [MESA_FORMAT_SARGB8] = intel_InitPointers_ARGB8888,
374 [MESA_FORMAT_Z16] = intel_InitDepthPointers_z16,
375 [MESA_FORMAT_X8_Z24] = intel_InitDepthPointers_z24_s8,
376 [MESA_FORMAT_S8_Z24] = intel_InitDepthPointers_z24_s8,
377 [MESA_FORMAT_R8] = intel_InitPointers_R8,
378 [MESA_FORMAT_RG88] = intel_InitPointers_RG88,
379 [MESA_FORMAT_R16] = intel_InitPointers_R16,
380 [MESA_FORMAT_RG1616] = intel_InitPointers_RG1616,
381 };
382
383 bool
384 intel_span_supports_format(gl_format format)
385 {
386 return intel_span_init_funcs[format] != NULL;
387 }
388
389 /**
390 * Plug in appropriate span read/write functions for the given renderbuffer.
391 * These are used for the software fallbacks.
392 */
393 static void
394 intel_set_span_functions(struct intel_context *intel,
395 struct gl_renderbuffer *rb)
396 {
397 struct intel_renderbuffer *irb = (struct intel_renderbuffer *) rb;
398
399 assert(intel_span_init_funcs[irb->Base.Format]);
400 intel_span_init_funcs[irb->Base.Format](rb);
401 }