Merge remote branch 'origin/master' into radeon-rewrite
[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->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->cache, BRW_SS_SURFACE,
325 &key, sizeof(key),
326 &key.bo, key.bo ? 1 : 0,
327 NULL);
328 if (brw->wm.surf_bo[surf] == NULL) {
329 brw->wm.surf_bo[surf] = brw_create_texture_surface(brw, &key);
330 }
331 }
332
333
334
335 /**
336 * Create the constant buffer surface. Vertex/fragment shader constants will be
337 * read from this buffer with Data Port Read instructions/messages.
338 */
339 static dri_bo *
340 brw_create_constant_surface( struct brw_context *brw,
341 struct brw_surface_key *key )
342 {
343 const GLint w = key->width - 1;
344 struct brw_surface_state surf;
345 dri_bo *bo;
346
347 memset(&surf, 0, sizeof(surf));
348
349 surf.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
350 surf.ss0.surface_type = BRW_SURFACE_BUFFER;
351 surf.ss0.surface_format = BRW_SURFACEFORMAT_R32G32B32A32_FLOAT;
352
353 assert(key->bo);
354 if (key->bo)
355 surf.ss1.base_addr = key->bo->offset; /* reloc */
356 else
357 surf.ss1.base_addr = key->offset;
358
359 surf.ss2.width = w & 0x7f; /* bits 6:0 of size or width */
360 surf.ss2.height = (w >> 7) & 0x1fff; /* bits 19:7 of size or width */
361 surf.ss3.depth = (w >> 20) & 0x7f; /* bits 26:20 of size or width */
362 surf.ss3.pitch = (key->pitch * key->cpp) - 1; /* ignored?? */
363 brw_set_surface_tiling(&surf, key->tiling); /* tiling now allowed */
364
365 bo = brw_upload_cache(&brw->cache, BRW_SS_SURFACE,
366 key, sizeof(*key),
367 &key->bo, key->bo ? 1 : 0,
368 &surf, sizeof(surf),
369 NULL, NULL);
370
371 if (key->bo) {
372 /* Emit relocation to surface contents */
373 dri_bo_emit_reloc(bo,
374 I915_GEM_DOMAIN_SAMPLER, 0,
375 0,
376 offsetof(struct brw_surface_state, ss1),
377 key->bo);
378 }
379
380 return bo;
381 }
382
383
384 /**
385 * Update the surface state for a WM constant buffer.
386 * The constant buffer will be (re)allocated here if needed.
387 */
388 static dri_bo *
389 brw_update_wm_constant_surface( GLcontext *ctx,
390 GLuint surf,
391 dri_bo *const_buffer,
392 const struct gl_program_parameter_list *params)
393 {
394 struct brw_context *brw = brw_context(ctx);
395 struct brw_surface_key key;
396 struct intel_context *intel = &brw->intel;
397 const int size = params->NumParameters * 4 * sizeof(GLfloat);
398
399 /* free old const buffer if too small */
400 if (const_buffer && const_buffer->size < size) {
401 dri_bo_unreference(const_buffer);
402 const_buffer = NULL;
403 }
404
405 /* alloc new buffer if needed */
406 if (!const_buffer) {
407 const_buffer =
408 drm_intel_bo_alloc(intel->bufmgr, "fp_const_buffer", size, 64);
409 }
410
411 memset(&key, 0, sizeof(key));
412
413 key.format = MESA_FORMAT_RGBA_FLOAT32;
414 key.internal_format = GL_RGBA;
415 key.bo = const_buffer;
416 key.depthmode = GL_NONE;
417 key.pitch = params->NumParameters;
418 key.width = params->NumParameters;
419 key.height = 1;
420 key.depth = 1;
421 key.cpp = 16;
422
423 /*
424 printf("%s:\n", __FUNCTION__);
425 printf(" width %d height %d depth %d cpp %d pitch %d\n",
426 key.width, key.height, key.depth, key.cpp, key.pitch);
427 */
428
429 dri_bo_unreference(brw->wm.surf_bo[surf]);
430 brw->wm.surf_bo[surf] = brw_search_cache(&brw->cache, BRW_SS_SURFACE,
431 &key, sizeof(key),
432 &key.bo, key.bo ? 1 : 0,
433 NULL);
434 if (brw->wm.surf_bo[surf] == NULL) {
435 brw->wm.surf_bo[surf] = brw_create_constant_surface(brw, &key);
436 }
437
438 return const_buffer;
439 }
440
441
442 /**
443 * Update the surface state for a VS constant buffer.
444 * The constant buffer will be (re)allocated here if needed.
445 */
446 static dri_bo *
447 brw_update_vs_constant_surface( GLcontext *ctx,
448 GLuint surf,
449 dri_bo *const_buffer,
450 const struct gl_program_parameter_list *params)
451 {
452 struct brw_context *brw = brw_context(ctx);
453 struct brw_surface_key key;
454 struct intel_context *intel = &brw->intel;
455 const int size = params->NumParameters * 4 * sizeof(GLfloat);
456
457 assert(surf == 0);
458
459 /* free old const buffer if too small */
460 if (const_buffer && const_buffer->size < size) {
461 dri_bo_unreference(const_buffer);
462 const_buffer = NULL;
463 }
464
465 /* alloc new buffer if needed */
466 if (!const_buffer) {
467 const_buffer =
468 drm_intel_bo_alloc(intel->bufmgr, "vp_const_buffer", size, 64);
469 }
470
471 memset(&key, 0, sizeof(key));
472
473 key.format = MESA_FORMAT_RGBA_FLOAT32;
474 key.internal_format = GL_RGBA;
475 key.bo = const_buffer;
476 key.depthmode = GL_NONE;
477 key.pitch = params->NumParameters;
478 key.width = params->NumParameters;
479 key.height = 1;
480 key.depth = 1;
481 key.cpp = 16;
482
483 /*
484 printf("%s:\n", __FUNCTION__);
485 printf(" width %d height %d depth %d cpp %d pitch %d\n",
486 key.width, key.height, key.depth, key.cpp, key.pitch);
487 */
488
489 dri_bo_unreference(brw->vs.surf_bo[surf]);
490 brw->vs.surf_bo[surf] = brw_search_cache(&brw->cache, 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 } key;
524
525 memset(&key, 0, sizeof(key));
526
527 if (region != NULL) {
528 region_bo = region->buffer;
529
530 key.surface_type = BRW_SURFACE_2D;
531 switch (irb->texformat->MesaFormat) {
532 case MESA_FORMAT_ARGB8888:
533 key.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
534 break;
535 case MESA_FORMAT_RGB565:
536 key.surface_format = BRW_SURFACEFORMAT_B5G6R5_UNORM;
537 break;
538 case MESA_FORMAT_ARGB1555:
539 key.surface_format = BRW_SURFACEFORMAT_B5G5R5A1_UNORM;
540 break;
541 case MESA_FORMAT_ARGB4444:
542 key.surface_format = BRW_SURFACEFORMAT_B4G4R4A4_UNORM;
543 break;
544 default:
545 _mesa_problem(ctx, "Bad renderbuffer format: %d\n",
546 irb->texformat->MesaFormat);
547 }
548 key.tiling = region->tiling;
549 key.width = region->width;
550 key.height = region->height;
551 key.pitch = region->pitch;
552 key.cpp = region->cpp;
553 } else {
554 key.surface_type = BRW_SURFACE_NULL;
555 key.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
556 key.tiling = 0;
557 key.width = 1;
558 key.height = 1;
559 key.cpp = 4;
560 }
561 memcpy(key.color_mask, ctx->Color.ColorMask,
562 sizeof(key.color_mask));
563 key.color_blend = (!ctx->Color._LogicOpEnabled &&
564 ctx->Color.BlendEnabled);
565
566 dri_bo_unreference(brw->wm.surf_bo[unit]);
567 brw->wm.surf_bo[unit] = NULL;
568 if (cached)
569 brw->wm.surf_bo[unit] = brw_search_cache(&brw->cache, BRW_SS_SURFACE,
570 &key, sizeof(key),
571 &region_bo, 1,
572 NULL);
573
574 if (brw->wm.surf_bo[unit] == NULL) {
575 struct brw_surface_state surf;
576
577 memset(&surf, 0, sizeof(surf));
578
579 surf.ss0.surface_format = key.surface_format;
580 surf.ss0.surface_type = key.surface_type;
581 if (region_bo != NULL)
582 surf.ss1.base_addr = region_bo->offset; /* reloc */
583
584 surf.ss2.width = key.width - 1;
585 surf.ss2.height = key.height - 1;
586 brw_set_surface_tiling(&surf, key.tiling);
587 surf.ss3.pitch = (key.pitch * key.cpp) - 1;
588
589 /* _NEW_COLOR */
590 surf.ss0.color_blend = key.color_blend;
591 surf.ss0.writedisable_red = !key.color_mask[0];
592 surf.ss0.writedisable_green = !key.color_mask[1];
593 surf.ss0.writedisable_blue = !key.color_mask[2];
594 surf.ss0.writedisable_alpha = !key.color_mask[3];
595
596 /* Key size will never match key size for textures, so we're safe. */
597 brw->wm.surf_bo[unit] = brw_upload_cache(&brw->cache, BRW_SS_SURFACE,
598 &key, sizeof(key),
599 &region_bo, 1,
600 &surf, sizeof(surf),
601 NULL, NULL);
602 if (region_bo != NULL) {
603 /* We might sample from it, and we might render to it, so flag
604 * them both. We might be able to figure out from other state
605 * a more restrictive relocation to emit.
606 */
607 dri_bo_emit_reloc(brw->wm.surf_bo[unit],
608 I915_GEM_DOMAIN_RENDER,
609 I915_GEM_DOMAIN_RENDER,
610 0,
611 offsetof(struct brw_surface_state, ss1),
612 region_bo);
613 }
614 }
615 }
616
617
618 /**
619 * Constructs the binding table for the WM surface state, which maps unit
620 * numbers to surface state objects.
621 */
622 static dri_bo *
623 brw_wm_get_binding_table(struct brw_context *brw)
624 {
625 dri_bo *bind_bo;
626
627 assert(brw->wm.nr_surfaces <= BRW_WM_MAX_SURF);
628
629 bind_bo = brw_search_cache(&brw->cache, BRW_SS_SURF_BIND,
630 NULL, 0,
631 brw->wm.surf_bo, brw->wm.nr_surfaces,
632 NULL);
633
634 if (bind_bo == NULL) {
635 GLuint data_size = brw->wm.nr_surfaces * sizeof(GLuint);
636 uint32_t *data = malloc(data_size);
637 int i;
638
639 for (i = 0; i < brw->wm.nr_surfaces; i++)
640 if (brw->wm.surf_bo[i])
641 data[i] = brw->wm.surf_bo[i]->offset;
642 else
643 data[i] = 0;
644
645 bind_bo = brw_upload_cache( &brw->cache, BRW_SS_SURF_BIND,
646 NULL, 0,
647 brw->wm.surf_bo, brw->wm.nr_surfaces,
648 data, data_size,
649 NULL, NULL);
650
651 /* Emit binding table relocations to surface state */
652 for (i = 0; i < BRW_WM_MAX_SURF; i++) {
653 if (brw->wm.surf_bo[i] != NULL) {
654 dri_bo_emit_reloc(bind_bo,
655 I915_GEM_DOMAIN_INSTRUCTION, 0,
656 0,
657 i * sizeof(GLuint),
658 brw->wm.surf_bo[i]);
659 }
660 }
661
662 free(data);
663 }
664
665 return bind_bo;
666 }
667
668 static void prepare_wm_surfaces(struct brw_context *brw )
669 {
670 GLcontext *ctx = &brw->intel.ctx;
671 struct intel_context *intel = &brw->intel;
672 GLuint i;
673 int old_nr_surfaces;
674
675 /* _NEW_BUFFERS */
676 /* Update surfaces for drawing buffers */
677 if (ctx->DrawBuffer->_NumColorDrawBuffers >= 1) {
678 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
679 brw_update_renderbuffer_surface(brw,
680 ctx->DrawBuffer->_ColorDrawBuffers[i],
681 i,
682 GL_FALSE);
683 }
684 } else {
685 brw_update_renderbuffer_surface(brw, NULL, 0, GL_TRUE);
686 }
687
688 old_nr_surfaces = brw->wm.nr_surfaces;
689 brw->wm.nr_surfaces = MAX_DRAW_BUFFERS;
690
691 /* Update surface / buffer for fragment shader constant buffer */
692 {
693 const GLuint surf = SURF_INDEX_FRAG_CONST_BUFFER;
694 struct brw_fragment_program *fp =
695 (struct brw_fragment_program *) brw->fragment_program;
696 fp->const_buffer =
697 brw_update_wm_constant_surface(ctx, surf, fp->const_buffer,
698 fp->program.Base.Parameters);
699
700 brw->wm.nr_surfaces = surf + 1;
701 }
702
703 /* Update surfaces for textures */
704 for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
705 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
706 const GLuint surf = SURF_INDEX_TEXTURE(i);
707
708 /* _NEW_TEXTURE, BRW_NEW_TEXDATA */
709 if (texUnit->_ReallyEnabled) {
710 if (texUnit->_Current == intel->frame_buffer_texobj) {
711 /* render to texture */
712 dri_bo_unreference(brw->wm.surf_bo[surf]);
713 brw->wm.surf_bo[surf] = brw->wm.surf_bo[0];
714 dri_bo_reference(brw->wm.surf_bo[surf]);
715 brw->wm.nr_surfaces = surf + 1;
716 } else {
717 /* regular texture */
718 brw_update_texture_surface(ctx, i);
719 brw->wm.nr_surfaces = surf + 1;
720 }
721 } else {
722 dri_bo_unreference(brw->wm.surf_bo[surf]);
723 brw->wm.surf_bo[surf] = NULL;
724 }
725 }
726
727 dri_bo_unreference(brw->wm.bind_bo);
728 brw->wm.bind_bo = brw_wm_get_binding_table(brw);
729
730 if (brw->wm.nr_surfaces != old_nr_surfaces)
731 brw->state.dirty.brw |= BRW_NEW_NR_WM_SURFACES;
732 }
733
734
735 /**
736 * Constructs the binding table for the VS surface state.
737 */
738 static dri_bo *
739 brw_vs_get_binding_table(struct brw_context *brw)
740 {
741 dri_bo *bind_bo;
742
743 assert(brw->vs.nr_surfaces <= BRW_VS_MAX_SURF);
744
745 bind_bo = brw_search_cache(&brw->cache, BRW_SS_SURF_BIND,
746 NULL, 0,
747 brw->vs.surf_bo, brw->vs.nr_surfaces,
748 NULL);
749
750 if (bind_bo == NULL) {
751 GLuint data_size = brw->vs.nr_surfaces * sizeof(GLuint);
752 uint32_t *data = malloc(data_size);
753 int i;
754
755 for (i = 0; i < brw->vs.nr_surfaces; i++)
756 if (brw->vs.surf_bo[i])
757 data[i] = brw->vs.surf_bo[i]->offset;
758 else
759 data[i] = 0;
760
761 bind_bo = brw_upload_cache( &brw->cache, BRW_SS_SURF_BIND,
762 NULL, 0,
763 brw->vs.surf_bo, brw->vs.nr_surfaces,
764 data, data_size,
765 NULL, NULL);
766
767 /* Emit binding table relocations to surface state */
768 for (i = 0; i < BRW_VS_MAX_SURF; i++) {
769 if (brw->vs.surf_bo[i] != NULL) {
770 dri_bo_emit_reloc(bind_bo,
771 I915_GEM_DOMAIN_INSTRUCTION, 0,
772 0,
773 i * sizeof(GLuint),
774 brw->vs.surf_bo[i]);
775 }
776 }
777
778 free(data);
779 }
780
781 return bind_bo;
782 }
783
784
785 /**
786 * Vertex shader surfaces. Just constant buffer for now. Could add vertex
787 * shader textures in the future.
788 */
789 static void prepare_vs_surfaces(struct brw_context *brw )
790 {
791 GLcontext *ctx = &brw->intel.ctx;
792
793 /* Update surface / buffer for vertex shader constant buffer */
794 {
795 const GLuint surf = SURF_INDEX_VERT_CONST_BUFFER;
796 struct brw_vertex_program *vp =
797 (struct brw_vertex_program *) brw->vertex_program;
798 vp->const_buffer =
799 brw_update_vs_constant_surface(ctx, surf, vp->const_buffer,
800 vp->program.Base.Parameters);
801
802 brw->vs.nr_surfaces = 1;
803 }
804
805 dri_bo_unreference(brw->vs.bind_bo);
806 brw->vs.bind_bo = brw_vs_get_binding_table(brw);
807
808 if (1)
809 brw->state.dirty.brw |= BRW_NEW_NR_VS_SURFACES;
810 }
811
812
813 static void
814 prepare_surfaces(struct brw_context *brw)
815 {
816 prepare_wm_surfaces(brw);
817 prepare_vs_surfaces(brw);
818 }
819
820
821 const struct brw_tracked_state brw_wm_surfaces = {
822 .dirty = {
823 .mesa = _NEW_COLOR | _NEW_TEXTURE | _NEW_BUFFERS | _NEW_PROGRAM,
824 .brw = BRW_NEW_CONTEXT,
825 .cache = 0
826 },
827 .prepare = prepare_surfaces,
828 };
829
830
831