Merge branch 'mesa_7_5_branch'
[mesa.git] / src / mesa / drivers / dri / i965 / brw_wm_surface_state.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a 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, sublicense, 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
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33 #include "main/mtypes.h"
34 #include "main/texformat.h"
35 #include "main/texstore.h"
36 #include "shader/prog_parameter.h"
37
38 #include "intel_mipmap_tree.h"
39 #include "intel_batchbuffer.h"
40 #include "intel_tex.h"
41 #include "intel_fbo.h"
42
43 #include "brw_context.h"
44 #include "brw_state.h"
45 #include "brw_defines.h"
46
47
48 static GLuint translate_tex_target( GLenum target )
49 {
50 switch (target) {
51 case GL_TEXTURE_1D:
52 return BRW_SURFACE_1D;
53
54 case GL_TEXTURE_RECTANGLE_NV:
55 return BRW_SURFACE_2D;
56
57 case GL_TEXTURE_2D:
58 return BRW_SURFACE_2D;
59
60 case GL_TEXTURE_3D:
61 return BRW_SURFACE_3D;
62
63 case GL_TEXTURE_CUBE_MAP:
64 return BRW_SURFACE_CUBE;
65
66 default:
67 assert(0);
68 return 0;
69 }
70 }
71
72
73 static GLuint translate_tex_format( GLuint mesa_format, GLenum internal_format,
74 GLenum depth_mode )
75 {
76 switch( mesa_format ) {
77 case MESA_FORMAT_L8:
78 return BRW_SURFACEFORMAT_L8_UNORM;
79
80 case MESA_FORMAT_I8:
81 return BRW_SURFACEFORMAT_I8_UNORM;
82
83 case MESA_FORMAT_A8:
84 return BRW_SURFACEFORMAT_A8_UNORM;
85
86 case MESA_FORMAT_AL88:
87 return BRW_SURFACEFORMAT_L8A8_UNORM;
88
89 case MESA_FORMAT_RGB888:
90 assert(0); /* not supported for sampling */
91 return BRW_SURFACEFORMAT_R8G8B8_UNORM;
92
93 case MESA_FORMAT_ARGB8888:
94 if (internal_format == GL_RGB)
95 return BRW_SURFACEFORMAT_B8G8R8X8_UNORM;
96 else
97 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
98
99 case MESA_FORMAT_RGBA8888_REV:
100 if (internal_format == GL_RGB)
101 return BRW_SURFACEFORMAT_R8G8B8X8_UNORM;
102 else
103 return BRW_SURFACEFORMAT_R8G8B8A8_UNORM;
104
105 case MESA_FORMAT_RGB565:
106 return BRW_SURFACEFORMAT_B5G6R5_UNORM;
107
108 case MESA_FORMAT_ARGB1555:
109 return BRW_SURFACEFORMAT_B5G5R5A1_UNORM;
110
111 case MESA_FORMAT_ARGB4444:
112 return BRW_SURFACEFORMAT_B4G4R4A4_UNORM;
113
114 case MESA_FORMAT_YCBCR_REV:
115 return BRW_SURFACEFORMAT_YCRCB_NORMAL;
116
117 case MESA_FORMAT_YCBCR:
118 return BRW_SURFACEFORMAT_YCRCB_SWAPUVY;
119
120 case MESA_FORMAT_RGB_FXT1:
121 case MESA_FORMAT_RGBA_FXT1:
122 return BRW_SURFACEFORMAT_FXT1;
123
124 case MESA_FORMAT_Z16:
125 if (depth_mode == GL_INTENSITY)
126 return BRW_SURFACEFORMAT_I16_UNORM;
127 else if (depth_mode == GL_ALPHA)
128 return BRW_SURFACEFORMAT_A16_UNORM;
129 else
130 return BRW_SURFACEFORMAT_L16_UNORM;
131
132 case MESA_FORMAT_RGB_DXT1:
133 return BRW_SURFACEFORMAT_DXT1_RGB;
134
135 case MESA_FORMAT_RGBA_DXT1:
136 return BRW_SURFACEFORMAT_BC1_UNORM;
137
138 case MESA_FORMAT_RGBA_DXT3:
139 return BRW_SURFACEFORMAT_BC2_UNORM;
140
141 case MESA_FORMAT_RGBA_DXT5:
142 return BRW_SURFACEFORMAT_BC3_UNORM;
143
144 case MESA_FORMAT_SARGB8:
145 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM_SRGB;
146
147 case MESA_FORMAT_SLA8:
148 return BRW_SURFACEFORMAT_L8A8_UNORM_SRGB;
149
150 case MESA_FORMAT_SL8:
151 return BRW_SURFACEFORMAT_L8_UNORM_SRGB;
152
153 case MESA_FORMAT_SRGB_DXT1:
154 return BRW_SURFACEFORMAT_BC1_UNORM_SRGB;
155
156 case MESA_FORMAT_S8_Z24:
157 /* XXX: these different surface formats don't seem to
158 * make any difference for shadow sampler/compares.
159 */
160 if (depth_mode == GL_INTENSITY)
161 return BRW_SURFACEFORMAT_I24X8_UNORM;
162 else if (depth_mode == GL_ALPHA)
163 return BRW_SURFACEFORMAT_A24X8_UNORM;
164 else
165 return BRW_SURFACEFORMAT_L24X8_UNORM;
166
167 case MESA_FORMAT_DUDV8:
168 return BRW_SURFACEFORMAT_R8G8_SNORM;
169
170 case MESA_FORMAT_SIGNED_RGBA8888_REV:
171 return BRW_SURFACEFORMAT_R8G8B8A8_SNORM;
172
173 default:
174 assert(0);
175 return 0;
176 }
177 }
178
179
180 /**
181 * Use same key for WM and VS surfaces.
182 */
183 struct brw_surface_key {
184 GLenum target, depthmode;
185 dri_bo *bo;
186 GLint format, internal_format;
187 GLint first_level, last_level;
188 GLint width, height, depth;
189 GLint pitch, cpp;
190 uint32_t tiling;
191 GLuint offset;
192 };
193
194
195 static void
196 brw_set_surface_tiling(struct brw_surface_state *surf, uint32_t tiling)
197 {
198 switch (tiling) {
199 case I915_TILING_NONE:
200 surf->ss3.tiled_surface = 0;
201 surf->ss3.tile_walk = 0;
202 break;
203 case I915_TILING_X:
204 surf->ss3.tiled_surface = 1;
205 surf->ss3.tile_walk = BRW_TILEWALK_XMAJOR;
206 break;
207 case I915_TILING_Y:
208 surf->ss3.tiled_surface = 1;
209 surf->ss3.tile_walk = BRW_TILEWALK_YMAJOR;
210 break;
211 }
212 }
213
214 static dri_bo *
215 brw_create_texture_surface( struct brw_context *brw,
216 struct brw_surface_key *key )
217 {
218 struct brw_surface_state surf;
219 dri_bo *bo;
220
221 memset(&surf, 0, sizeof(surf));
222
223 surf.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
224 surf.ss0.surface_type = translate_tex_target(key->target);
225 if (key->bo) {
226 surf.ss0.surface_format = translate_tex_format(key->format,
227 key->internal_format,
228 key->depthmode);
229 }
230 else {
231 switch (key->depth) {
232 case 32:
233 surf.ss0.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
234 break;
235 default:
236 case 24:
237 surf.ss0.surface_format = BRW_SURFACEFORMAT_B8G8R8X8_UNORM;
238 break;
239 case 16:
240 surf.ss0.surface_format = BRW_SURFACEFORMAT_B5G6R5_UNORM;
241 break;
242 }
243 }
244
245 /* This is ok for all textures with channel width 8bit or less:
246 */
247 /* surf.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
248 if (key->bo)
249 surf.ss1.base_addr = key->bo->offset; /* reloc */
250 else
251 surf.ss1.base_addr = key->offset;
252
253 surf.ss2.mip_count = key->last_level - key->first_level;
254 surf.ss2.width = key->width - 1;
255 surf.ss2.height = key->height - 1;
256 brw_set_surface_tiling(&surf, key->tiling);
257 surf.ss3.pitch = (key->pitch * key->cpp) - 1;
258 surf.ss3.depth = key->depth - 1;
259
260 surf.ss4.min_lod = 0;
261
262 if (key->target == GL_TEXTURE_CUBE_MAP) {
263 surf.ss0.cube_pos_x = 1;
264 surf.ss0.cube_pos_y = 1;
265 surf.ss0.cube_pos_z = 1;
266 surf.ss0.cube_neg_x = 1;
267 surf.ss0.cube_neg_y = 1;
268 surf.ss0.cube_neg_z = 1;
269 }
270
271 bo = brw_upload_cache(&brw->surface_cache, BRW_SS_SURFACE,
272 key, sizeof(*key),
273 &key->bo, key->bo ? 1 : 0,
274 &surf, sizeof(surf),
275 NULL, NULL);
276
277 if (key->bo) {
278 /* Emit relocation to surface contents */
279 dri_bo_emit_reloc(bo,
280 I915_GEM_DOMAIN_SAMPLER, 0,
281 0,
282 offsetof(struct brw_surface_state, ss1),
283 key->bo);
284 }
285 return bo;
286 }
287
288 static void
289 brw_update_texture_surface( GLcontext *ctx, GLuint unit )
290 {
291 struct brw_context *brw = brw_context(ctx);
292 struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
293 struct intel_texture_object *intelObj = intel_texture_object(tObj);
294 struct gl_texture_image *firstImage = tObj->Image[0][intelObj->firstLevel];
295 struct brw_surface_key key;
296 const GLuint surf = SURF_INDEX_TEXTURE(unit);
297
298 memset(&key, 0, sizeof(key));
299
300 if (intelObj->imageOverride) {
301 key.pitch = intelObj->pitchOverride / intelObj->mt->cpp;
302 key.depth = intelObj->depthOverride;
303 key.bo = NULL;
304 key.offset = intelObj->textureOffset;
305 } else {
306 key.format = firstImage->TexFormat->MesaFormat;
307 key.internal_format = firstImage->InternalFormat;
308 key.pitch = intelObj->mt->pitch;
309 key.depth = firstImage->Depth;
310 key.bo = intelObj->mt->region->buffer;
311 key.offset = 0;
312 }
313
314 key.target = tObj->Target;
315 key.depthmode = tObj->DepthMode;
316 key.first_level = intelObj->firstLevel;
317 key.last_level = intelObj->lastLevel;
318 key.width = firstImage->Width;
319 key.height = firstImage->Height;
320 key.cpp = intelObj->mt->cpp;
321 key.tiling = intelObj->mt->region->tiling;
322
323 dri_bo_unreference(brw->wm.surf_bo[surf]);
324 brw->wm.surf_bo[surf] = brw_search_cache(&brw->surface_cache,
325 BRW_SS_SURFACE,
326 &key, sizeof(key),
327 &key.bo, key.bo ? 1 : 0,
328 NULL);
329 if (brw->wm.surf_bo[surf] == NULL) {
330 brw->wm.surf_bo[surf] = brw_create_texture_surface(brw, &key);
331 }
332 }
333
334
335
336 /**
337 * Create the constant buffer surface. Vertex/fragment shader constants will be
338 * read from this buffer with Data Port Read instructions/messages.
339 */
340 static dri_bo *
341 brw_create_constant_surface( struct brw_context *brw,
342 struct brw_surface_key *key )
343 {
344 const GLint w = key->width - 1;
345 struct brw_surface_state surf;
346 dri_bo *bo;
347
348 memset(&surf, 0, sizeof(surf));
349
350 surf.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
351 surf.ss0.surface_type = BRW_SURFACE_BUFFER;
352 surf.ss0.surface_format = BRW_SURFACEFORMAT_R32G32B32A32_FLOAT;
353
354 assert(key->bo);
355 if (key->bo)
356 surf.ss1.base_addr = key->bo->offset; /* reloc */
357 else
358 surf.ss1.base_addr = key->offset;
359
360 surf.ss2.width = w & 0x7f; /* bits 6:0 of size or width */
361 surf.ss2.height = (w >> 7) & 0x1fff; /* bits 19:7 of size or width */
362 surf.ss3.depth = (w >> 20) & 0x7f; /* bits 26:20 of size or width */
363 surf.ss3.pitch = (key->pitch * key->cpp) - 1; /* ignored?? */
364 brw_set_surface_tiling(&surf, key->tiling); /* tiling now allowed */
365
366 bo = brw_upload_cache(&brw->surface_cache, BRW_SS_SURFACE,
367 key, sizeof(*key),
368 &key->bo, key->bo ? 1 : 0,
369 &surf, sizeof(surf),
370 NULL, NULL);
371
372 if (key->bo) {
373 /* Emit relocation to surface contents */
374 dri_bo_emit_reloc(bo,
375 I915_GEM_DOMAIN_SAMPLER, 0,
376 0,
377 offsetof(struct brw_surface_state, ss1),
378 key->bo);
379 }
380
381 return bo;
382 }
383
384
385 /**
386 * Update the surface state for a WM constant buffer.
387 * The constant buffer will be (re)allocated here if needed.
388 */
389 static dri_bo *
390 brw_update_wm_constant_surface( GLcontext *ctx,
391 GLuint surf,
392 dri_bo *const_buffer,
393 const struct gl_program_parameter_list *params)
394 {
395 struct brw_context *brw = brw_context(ctx);
396 struct brw_surface_key key;
397 struct intel_context *intel = &brw->intel;
398 const int size = params->NumParameters * 4 * sizeof(GLfloat);
399
400 /* free old const buffer if too small */
401 if (const_buffer && const_buffer->size < size) {
402 dri_bo_unreference(const_buffer);
403 const_buffer = NULL;
404 }
405
406 /* alloc new buffer if needed */
407 if (!const_buffer) {
408 const_buffer =
409 drm_intel_bo_alloc(intel->bufmgr, "fp_const_buffer", size, 64);
410 }
411
412 memset(&key, 0, sizeof(key));
413
414 key.format = MESA_FORMAT_RGBA_FLOAT32;
415 key.internal_format = GL_RGBA;
416 key.bo = const_buffer;
417 key.depthmode = GL_NONE;
418 key.pitch = params->NumParameters;
419 key.width = params->NumParameters;
420 key.height = 1;
421 key.depth = 1;
422 key.cpp = 16;
423
424 /*
425 printf("%s:\n", __FUNCTION__);
426 printf(" width %d height %d depth %d cpp %d pitch %d\n",
427 key.width, key.height, key.depth, key.cpp, key.pitch);
428 */
429
430 dri_bo_unreference(brw->wm.surf_bo[surf]);
431 brw->wm.surf_bo[surf] = brw_search_cache(&brw->surface_cache,
432 BRW_SS_SURFACE,
433 &key, sizeof(key),
434 &key.bo, key.bo ? 1 : 0,
435 NULL);
436 if (brw->wm.surf_bo[surf] == NULL) {
437 brw->wm.surf_bo[surf] = brw_create_constant_surface(brw, &key);
438 }
439
440 return const_buffer;
441 }
442
443
444 /**
445 * Update the surface state for a VS constant buffer.
446 * The constant buffer will be (re)allocated here if needed.
447 */
448 static dri_bo *
449 brw_update_vs_constant_surface( GLcontext *ctx,
450 GLuint surf,
451 dri_bo *const_buffer,
452 const struct gl_program_parameter_list *params)
453 {
454 struct brw_context *brw = brw_context(ctx);
455 struct brw_surface_key key;
456 struct intel_context *intel = &brw->intel;
457 const int size = params->NumParameters * 4 * sizeof(GLfloat);
458
459 assert(surf == 0);
460
461 /* We always create a new VS constant buffer so that several can be
462 * in flight at a time. Free the old one first...
463 */
464 dri_bo_unreference(const_buffer);
465
466 /* alloc new buffer */
467 const_buffer =
468 drm_intel_bo_alloc(intel->bufmgr, "vp_const_buffer", size, 64);
469
470 memset(&key, 0, sizeof(key));
471
472 key.format = MESA_FORMAT_RGBA_FLOAT32;
473 key.internal_format = GL_RGBA;
474 key.bo = const_buffer;
475 key.depthmode = GL_NONE;
476 key.pitch = params->NumParameters;
477 key.width = params->NumParameters;
478 key.height = 1;
479 key.depth = 1;
480 key.cpp = 16;
481
482 /*
483 printf("%s:\n", __FUNCTION__);
484 printf(" width %d height %d depth %d cpp %d pitch %d\n",
485 key.width, key.height, key.depth, key.cpp, key.pitch);
486 */
487
488 dri_bo_unreference(brw->vs.surf_bo[surf]);
489 brw->vs.surf_bo[surf] = brw_search_cache(&brw->surface_cache,
490 BRW_SS_SURFACE,
491 &key, sizeof(key),
492 &key.bo, key.bo ? 1 : 0,
493 NULL);
494 if (brw->vs.surf_bo[surf] == NULL) {
495 brw->vs.surf_bo[surf] = brw_create_constant_surface(brw, &key);
496 }
497
498 return const_buffer;
499 }
500
501
502 /**
503 * Sets up a surface state structure to point at the given region.
504 * While it is only used for the front/back buffer currently, it should be
505 * usable for further buffers when doing ARB_draw_buffer support.
506 */
507 static void
508 brw_update_renderbuffer_surface(struct brw_context *brw,
509 struct gl_renderbuffer *rb,
510 unsigned int unit, GLboolean cached)
511 {
512 GLcontext *ctx = &brw->intel.ctx;
513 dri_bo *region_bo = NULL;
514 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
515 struct intel_region *region = irb ? irb->region : NULL;
516 struct {
517 unsigned int surface_type;
518 unsigned int surface_format;
519 unsigned int width, height, pitch, cpp;
520 GLubyte color_mask[4];
521 GLboolean color_blend;
522 uint32_t tiling;
523 uint32_t draw_offset;
524 } key;
525
526 memset(&key, 0, sizeof(key));
527
528 if (region != NULL) {
529 region_bo = region->buffer;
530
531 key.surface_type = BRW_SURFACE_2D;
532 switch (irb->texformat->MesaFormat) {
533 case MESA_FORMAT_ARGB8888:
534 key.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
535 break;
536 case MESA_FORMAT_RGB565:
537 key.surface_format = BRW_SURFACEFORMAT_B5G6R5_UNORM;
538 break;
539 case MESA_FORMAT_ARGB1555:
540 key.surface_format = BRW_SURFACEFORMAT_B5G5R5A1_UNORM;
541 break;
542 case MESA_FORMAT_ARGB4444:
543 key.surface_format = BRW_SURFACEFORMAT_B4G4R4A4_UNORM;
544 break;
545 default:
546 _mesa_problem(ctx, "Bad renderbuffer format: %d\n",
547 irb->texformat->MesaFormat);
548 }
549 key.tiling = region->tiling;
550 key.width = region->width;
551 key.height = region->height;
552 key.pitch = region->pitch;
553 key.cpp = region->cpp;
554 key.draw_offset = region->draw_offset; /* cur 3d or cube face offset */
555 } else {
556 key.surface_type = BRW_SURFACE_NULL;
557 key.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
558 key.tiling = 0;
559 key.width = 1;
560 key.height = 1;
561 key.cpp = 4;
562 key.draw_offset = 0;
563 }
564 memcpy(key.color_mask, ctx->Color.ColorMask,
565 sizeof(key.color_mask));
566 key.color_blend = (!ctx->Color._LogicOpEnabled &&
567 ctx->Color.BlendEnabled);
568
569 dri_bo_unreference(brw->wm.surf_bo[unit]);
570 brw->wm.surf_bo[unit] = NULL;
571 if (cached)
572 brw->wm.surf_bo[unit] = brw_search_cache(&brw->surface_cache,
573 BRW_SS_SURFACE,
574 &key, sizeof(key),
575 &region_bo, 1,
576 NULL);
577
578 if (brw->wm.surf_bo[unit] == NULL) {
579 struct brw_surface_state surf;
580
581 memset(&surf, 0, sizeof(surf));
582
583 surf.ss0.surface_format = key.surface_format;
584 surf.ss0.surface_type = key.surface_type;
585 surf.ss1.base_addr = key.draw_offset;
586 if (region_bo != NULL)
587 surf.ss1.base_addr += region_bo->offset; /* reloc */
588
589 surf.ss2.width = key.width - 1;
590 surf.ss2.height = key.height - 1;
591 brw_set_surface_tiling(&surf, key.tiling);
592 surf.ss3.pitch = (key.pitch * key.cpp) - 1;
593
594 /* _NEW_COLOR */
595 surf.ss0.color_blend = key.color_blend;
596 surf.ss0.writedisable_red = !key.color_mask[0];
597 surf.ss0.writedisable_green = !key.color_mask[1];
598 surf.ss0.writedisable_blue = !key.color_mask[2];
599 surf.ss0.writedisable_alpha = !key.color_mask[3];
600
601 /* Key size will never match key size for textures, so we're safe. */
602 brw->wm.surf_bo[unit] = brw_upload_cache(&brw->surface_cache,
603 BRW_SS_SURFACE,
604 &key, sizeof(key),
605 &region_bo, 1,
606 &surf, sizeof(surf),
607 NULL, NULL);
608 if (region_bo != NULL) {
609 /* We might sample from it, and we might render to it, so flag
610 * them both. We might be able to figure out from other state
611 * a more restrictive relocation to emit.
612 */
613 drm_intel_bo_emit_reloc(brw->wm.surf_bo[unit],
614 offsetof(struct brw_surface_state, ss1),
615 region_bo,
616 key.draw_offset,
617 I915_GEM_DOMAIN_RENDER,
618 I915_GEM_DOMAIN_RENDER);
619 }
620 }
621 }
622
623
624 /**
625 * Constructs the binding table for the WM surface state, which maps unit
626 * numbers to surface state objects.
627 */
628 static dri_bo *
629 brw_wm_get_binding_table(struct brw_context *brw)
630 {
631 dri_bo *bind_bo;
632
633 assert(brw->wm.nr_surfaces <= BRW_WM_MAX_SURF);
634
635 bind_bo = brw_search_cache(&brw->surface_cache, BRW_SS_SURF_BIND,
636 NULL, 0,
637 brw->wm.surf_bo, brw->wm.nr_surfaces,
638 NULL);
639
640 if (bind_bo == NULL) {
641 GLuint data_size = brw->wm.nr_surfaces * sizeof(GLuint);
642 uint32_t *data = malloc(data_size);
643 int i;
644
645 for (i = 0; i < brw->wm.nr_surfaces; i++)
646 if (brw->wm.surf_bo[i])
647 data[i] = brw->wm.surf_bo[i]->offset;
648 else
649 data[i] = 0;
650
651 bind_bo = brw_upload_cache( &brw->surface_cache, BRW_SS_SURF_BIND,
652 NULL, 0,
653 brw->wm.surf_bo, brw->wm.nr_surfaces,
654 data, data_size,
655 NULL, NULL);
656
657 /* Emit binding table relocations to surface state */
658 for (i = 0; i < BRW_WM_MAX_SURF; i++) {
659 if (brw->wm.surf_bo[i] != NULL) {
660 dri_bo_emit_reloc(bind_bo,
661 I915_GEM_DOMAIN_INSTRUCTION, 0,
662 0,
663 i * sizeof(GLuint),
664 brw->wm.surf_bo[i]);
665 }
666 }
667
668 free(data);
669 }
670
671 return bind_bo;
672 }
673
674 static void prepare_wm_surfaces(struct brw_context *brw )
675 {
676 GLcontext *ctx = &brw->intel.ctx;
677 struct intel_context *intel = &brw->intel;
678 GLuint i;
679 int old_nr_surfaces;
680
681 /* _NEW_BUFFERS */
682 /* Update surfaces for drawing buffers */
683 if (ctx->DrawBuffer->_NumColorDrawBuffers >= 1) {
684 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
685 brw_update_renderbuffer_surface(brw,
686 ctx->DrawBuffer->_ColorDrawBuffers[i],
687 i,
688 GL_FALSE);
689 }
690 } else {
691 brw_update_renderbuffer_surface(brw, NULL, 0, GL_TRUE);
692 }
693
694 old_nr_surfaces = brw->wm.nr_surfaces;
695 brw->wm.nr_surfaces = MAX_DRAW_BUFFERS;
696
697 /* Update surface / buffer for fragment shader constant buffer */
698 {
699 const GLuint surf = SURF_INDEX_FRAG_CONST_BUFFER;
700 struct brw_fragment_program *fp =
701 (struct brw_fragment_program *) brw->fragment_program;
702 fp->const_buffer =
703 brw_update_wm_constant_surface(ctx, surf, fp->const_buffer,
704 fp->program.Base.Parameters);
705
706 brw->wm.nr_surfaces = surf + 1;
707 }
708
709 /* Update surfaces for textures */
710 for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
711 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
712 const GLuint surf = SURF_INDEX_TEXTURE(i);
713
714 /* _NEW_TEXTURE, BRW_NEW_TEXDATA */
715 if (texUnit->_ReallyEnabled) {
716 if (texUnit->_Current == intel->frame_buffer_texobj) {
717 /* render to texture */
718 dri_bo_unreference(brw->wm.surf_bo[surf]);
719 brw->wm.surf_bo[surf] = brw->wm.surf_bo[0];
720 dri_bo_reference(brw->wm.surf_bo[surf]);
721 brw->wm.nr_surfaces = surf + 1;
722 } else {
723 /* regular texture */
724 brw_update_texture_surface(ctx, i);
725 brw->wm.nr_surfaces = surf + 1;
726 }
727 } else {
728 dri_bo_unreference(brw->wm.surf_bo[surf]);
729 brw->wm.surf_bo[surf] = NULL;
730 }
731 }
732
733 dri_bo_unreference(brw->wm.bind_bo);
734 brw->wm.bind_bo = brw_wm_get_binding_table(brw);
735
736 if (brw->wm.nr_surfaces != old_nr_surfaces)
737 brw->state.dirty.brw |= BRW_NEW_NR_WM_SURFACES;
738 }
739
740
741 /**
742 * Constructs the binding table for the VS surface state.
743 */
744 static dri_bo *
745 brw_vs_get_binding_table(struct brw_context *brw)
746 {
747 dri_bo *bind_bo;
748
749 assert(brw->vs.nr_surfaces <= BRW_VS_MAX_SURF);
750
751 bind_bo = brw_search_cache(&brw->surface_cache, BRW_SS_SURF_BIND,
752 NULL, 0,
753 brw->vs.surf_bo, brw->vs.nr_surfaces,
754 NULL);
755
756 if (bind_bo == NULL) {
757 GLuint data_size = brw->vs.nr_surfaces * sizeof(GLuint);
758 uint32_t *data = malloc(data_size);
759 int i;
760
761 for (i = 0; i < brw->vs.nr_surfaces; i++)
762 if (brw->vs.surf_bo[i])
763 data[i] = brw->vs.surf_bo[i]->offset;
764 else
765 data[i] = 0;
766
767 bind_bo = brw_upload_cache( &brw->surface_cache, BRW_SS_SURF_BIND,
768 NULL, 0,
769 brw->vs.surf_bo, brw->vs.nr_surfaces,
770 data, data_size,
771 NULL, NULL);
772
773 /* Emit binding table relocations to surface state */
774 for (i = 0; i < BRW_VS_MAX_SURF; i++) {
775 if (brw->vs.surf_bo[i] != NULL) {
776 dri_bo_emit_reloc(bind_bo,
777 I915_GEM_DOMAIN_INSTRUCTION, 0,
778 0,
779 i * sizeof(GLuint),
780 brw->vs.surf_bo[i]);
781 }
782 }
783
784 free(data);
785 }
786
787 return bind_bo;
788 }
789
790
791 /**
792 * Vertex shader surfaces (constant buffer).
793 */
794 static void prepare_vs_surfaces(struct brw_context *brw )
795 {
796 GLcontext *ctx = &brw->intel.ctx;
797
798 /* Update surface / buffer for vertex shader constant buffer */
799 {
800 const GLuint surf = SURF_INDEX_VERT_CONST_BUFFER;
801 struct brw_vertex_program *vp =
802 (struct brw_vertex_program *) brw->vertex_program;
803 vp->const_buffer =
804 brw_update_vs_constant_surface(ctx, surf, vp->const_buffer,
805 vp->program.Base.Parameters);
806
807 brw->vs.nr_surfaces = 1;
808 }
809
810 dri_bo_unreference(brw->vs.bind_bo);
811 brw->vs.bind_bo = brw_vs_get_binding_table(brw);
812
813 if (1)
814 brw->state.dirty.brw |= BRW_NEW_NR_VS_SURFACES;
815 }
816
817
818 static void
819 prepare_surfaces(struct brw_context *brw)
820 {
821 prepare_wm_surfaces(brw);
822 prepare_vs_surfaces(brw);
823 }
824
825
826 const struct brw_tracked_state brw_wm_surfaces = {
827 .dirty = {
828 .mesa = (_NEW_COLOR |
829 _NEW_TEXTURE |
830 _NEW_BUFFERS |
831 _NEW_PROGRAM |
832 _NEW_PROGRAM_CONSTANTS),
833 .brw = (BRW_NEW_CONTEXT),
834 .cache = 0
835 },
836 .prepare = prepare_surfaces,
837 };
838
839
840