i965: Don't clip everything if FRONT_AND_BACK culling while culling disabled.
[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 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->cache, BRW_SS_SURFACE,
573 &key, sizeof(key),
574 &region_bo, 1,
575 NULL);
576
577 if (brw->wm.surf_bo[unit] == NULL) {
578 struct brw_surface_state surf;
579
580 memset(&surf, 0, sizeof(surf));
581
582 surf.ss0.surface_format = key.surface_format;
583 surf.ss0.surface_type = key.surface_type;
584 surf.ss1.base_addr = key.draw_offset;
585 if (region_bo != NULL)
586 surf.ss1.base_addr += region_bo->offset; /* reloc */
587
588 surf.ss2.width = key.width - 1;
589 surf.ss2.height = key.height - 1;
590 brw_set_surface_tiling(&surf, key.tiling);
591 surf.ss3.pitch = (key.pitch * key.cpp) - 1;
592
593 /* _NEW_COLOR */
594 surf.ss0.color_blend = key.color_blend;
595 surf.ss0.writedisable_red = !key.color_mask[0];
596 surf.ss0.writedisable_green = !key.color_mask[1];
597 surf.ss0.writedisable_blue = !key.color_mask[2];
598 surf.ss0.writedisable_alpha = !key.color_mask[3];
599
600 /* Key size will never match key size for textures, so we're safe. */
601 brw->wm.surf_bo[unit] = brw_upload_cache(&brw->cache, BRW_SS_SURFACE,
602 &key, sizeof(key),
603 &region_bo, 1,
604 &surf, sizeof(surf),
605 NULL, NULL);
606 if (region_bo != NULL) {
607 /* We might sample from it, and we might render to it, so flag
608 * them both. We might be able to figure out from other state
609 * a more restrictive relocation to emit.
610 */
611 drm_intel_bo_emit_reloc(brw->wm.surf_bo[unit],
612 offsetof(struct brw_surface_state, ss1),
613 region_bo,
614 key.draw_offset,
615 I915_GEM_DOMAIN_RENDER,
616 I915_GEM_DOMAIN_RENDER);
617 }
618 }
619 }
620
621
622 /**
623 * Constructs the binding table for the WM surface state, which maps unit
624 * numbers to surface state objects.
625 */
626 static dri_bo *
627 brw_wm_get_binding_table(struct brw_context *brw)
628 {
629 dri_bo *bind_bo;
630
631 assert(brw->wm.nr_surfaces <= BRW_WM_MAX_SURF);
632
633 bind_bo = brw_search_cache(&brw->cache, BRW_SS_SURF_BIND,
634 NULL, 0,
635 brw->wm.surf_bo, brw->wm.nr_surfaces,
636 NULL);
637
638 if (bind_bo == NULL) {
639 GLuint data_size = brw->wm.nr_surfaces * sizeof(GLuint);
640 uint32_t *data = malloc(data_size);
641 int i;
642
643 for (i = 0; i < brw->wm.nr_surfaces; i++)
644 if (brw->wm.surf_bo[i])
645 data[i] = brw->wm.surf_bo[i]->offset;
646 else
647 data[i] = 0;
648
649 bind_bo = brw_upload_cache( &brw->cache, BRW_SS_SURF_BIND,
650 NULL, 0,
651 brw->wm.surf_bo, brw->wm.nr_surfaces,
652 data, data_size,
653 NULL, NULL);
654
655 /* Emit binding table relocations to surface state */
656 for (i = 0; i < BRW_WM_MAX_SURF; i++) {
657 if (brw->wm.surf_bo[i] != NULL) {
658 dri_bo_emit_reloc(bind_bo,
659 I915_GEM_DOMAIN_INSTRUCTION, 0,
660 0,
661 i * sizeof(GLuint),
662 brw->wm.surf_bo[i]);
663 }
664 }
665
666 free(data);
667 }
668
669 return bind_bo;
670 }
671
672 static void prepare_wm_surfaces(struct brw_context *brw )
673 {
674 GLcontext *ctx = &brw->intel.ctx;
675 struct intel_context *intel = &brw->intel;
676 GLuint i;
677 int old_nr_surfaces;
678
679 /* _NEW_BUFFERS */
680 /* Update surfaces for drawing buffers */
681 if (ctx->DrawBuffer->_NumColorDrawBuffers >= 1) {
682 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
683 brw_update_renderbuffer_surface(brw,
684 ctx->DrawBuffer->_ColorDrawBuffers[i],
685 i,
686 GL_FALSE);
687 }
688 } else {
689 brw_update_renderbuffer_surface(brw, NULL, 0, GL_TRUE);
690 }
691
692 old_nr_surfaces = brw->wm.nr_surfaces;
693 brw->wm.nr_surfaces = MAX_DRAW_BUFFERS;
694
695 /* Update surface / buffer for fragment shader constant buffer */
696 {
697 const GLuint surf = SURF_INDEX_FRAG_CONST_BUFFER;
698 struct brw_fragment_program *fp =
699 (struct brw_fragment_program *) brw->fragment_program;
700 fp->const_buffer =
701 brw_update_wm_constant_surface(ctx, surf, fp->const_buffer,
702 fp->program.Base.Parameters);
703
704 brw->wm.nr_surfaces = surf + 1;
705 }
706
707 /* Update surfaces for textures */
708 for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
709 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
710 const GLuint surf = SURF_INDEX_TEXTURE(i);
711
712 /* _NEW_TEXTURE, BRW_NEW_TEXDATA */
713 if (texUnit->_ReallyEnabled) {
714 if (texUnit->_Current == intel->frame_buffer_texobj) {
715 /* render to texture */
716 dri_bo_unreference(brw->wm.surf_bo[surf]);
717 brw->wm.surf_bo[surf] = brw->wm.surf_bo[0];
718 dri_bo_reference(brw->wm.surf_bo[surf]);
719 brw->wm.nr_surfaces = surf + 1;
720 } else {
721 /* regular texture */
722 brw_update_texture_surface(ctx, i);
723 brw->wm.nr_surfaces = surf + 1;
724 }
725 } else {
726 dri_bo_unreference(brw->wm.surf_bo[surf]);
727 brw->wm.surf_bo[surf] = NULL;
728 }
729 }
730
731 dri_bo_unreference(brw->wm.bind_bo);
732 brw->wm.bind_bo = brw_wm_get_binding_table(brw);
733
734 if (brw->wm.nr_surfaces != old_nr_surfaces)
735 brw->state.dirty.brw |= BRW_NEW_NR_WM_SURFACES;
736 }
737
738
739 /**
740 * Constructs the binding table for the VS surface state.
741 */
742 static dri_bo *
743 brw_vs_get_binding_table(struct brw_context *brw)
744 {
745 dri_bo *bind_bo;
746
747 assert(brw->vs.nr_surfaces <= BRW_VS_MAX_SURF);
748
749 bind_bo = brw_search_cache(&brw->cache, BRW_SS_SURF_BIND,
750 NULL, 0,
751 brw->vs.surf_bo, brw->vs.nr_surfaces,
752 NULL);
753
754 if (bind_bo == NULL) {
755 GLuint data_size = brw->vs.nr_surfaces * sizeof(GLuint);
756 uint32_t *data = malloc(data_size);
757 int i;
758
759 for (i = 0; i < brw->vs.nr_surfaces; i++)
760 if (brw->vs.surf_bo[i])
761 data[i] = brw->vs.surf_bo[i]->offset;
762 else
763 data[i] = 0;
764
765 bind_bo = brw_upload_cache( &brw->cache, BRW_SS_SURF_BIND,
766 NULL, 0,
767 brw->vs.surf_bo, brw->vs.nr_surfaces,
768 data, data_size,
769 NULL, NULL);
770
771 /* Emit binding table relocations to surface state */
772 for (i = 0; i < BRW_VS_MAX_SURF; i++) {
773 if (brw->vs.surf_bo[i] != NULL) {
774 dri_bo_emit_reloc(bind_bo,
775 I915_GEM_DOMAIN_INSTRUCTION, 0,
776 0,
777 i * sizeof(GLuint),
778 brw->vs.surf_bo[i]);
779 }
780 }
781
782 free(data);
783 }
784
785 return bind_bo;
786 }
787
788
789 /**
790 * Vertex shader surfaces. Just constant buffer for now. Could add vertex
791 * shader textures in the future.
792 */
793 static void prepare_vs_surfaces(struct brw_context *brw )
794 {
795 GLcontext *ctx = &brw->intel.ctx;
796
797 /* Update surface / buffer for vertex shader constant buffer */
798 {
799 const GLuint surf = SURF_INDEX_VERT_CONST_BUFFER;
800 struct brw_vertex_program *vp =
801 (struct brw_vertex_program *) brw->vertex_program;
802 vp->const_buffer =
803 brw_update_vs_constant_surface(ctx, surf, vp->const_buffer,
804 vp->program.Base.Parameters);
805
806 brw->vs.nr_surfaces = 1;
807 }
808
809 dri_bo_unreference(brw->vs.bind_bo);
810 brw->vs.bind_bo = brw_vs_get_binding_table(brw);
811
812 if (1)
813 brw->state.dirty.brw |= BRW_NEW_NR_VS_SURFACES;
814 }
815
816
817 static void
818 prepare_surfaces(struct brw_context *brw)
819 {
820 prepare_wm_surfaces(brw);
821 prepare_vs_surfaces(brw);
822 }
823
824
825 const struct brw_tracked_state brw_wm_surfaces = {
826 .dirty = {
827 .mesa = _NEW_COLOR | _NEW_TEXTURE | _NEW_BUFFERS | _NEW_PROGRAM,
828 .brw = BRW_NEW_CONTEXT,
829 .cache = 0
830 },
831 .prepare = prepare_surfaces,
832 };
833
834
835