OSDN Git Service

mesa: fix error checking of DXT sRGB formats in _mesa_base_tex_format()
[android-x86/external-mesa.git] / src / mesa / drivers / dri / i965 / gen7_wm_surface_state.c
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 #include "main/mtypes.h"
24 #include "main/blend.h"
25 #include "main/samplerobj.h"
26 #include "program/prog_parameter.h"
27
28 #include "intel_mipmap_tree.h"
29 #include "intel_batchbuffer.h"
30 #include "intel_tex.h"
31 #include "intel_fbo.h"
32 #include "intel_buffer_objects.h"
33
34 #include "brw_context.h"
35 #include "brw_state.h"
36 #include "brw_defines.h"
37 #include "brw_wm.h"
38
39 /**
40  * Convert an swizzle enumeration (i.e. SWIZZLE_X) to one of the Gen7.5+
41  * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED)
42  */
43 static unsigned
44 swizzle_to_scs(GLenum swizzle)
45 {
46    switch (swizzle) {
47    case SWIZZLE_X:
48       return HSW_SCS_RED;
49    case SWIZZLE_Y:
50       return HSW_SCS_GREEN;
51    case SWIZZLE_Z:
52       return HSW_SCS_BLUE;
53    case SWIZZLE_W:
54       return HSW_SCS_ALPHA;
55    case SWIZZLE_ZERO:
56       return HSW_SCS_ZERO;
57    case SWIZZLE_ONE:
58       return HSW_SCS_ONE;
59    }
60
61    assert(!"Should not get here: invalid swizzle mode");
62    return HSW_SCS_ZERO;
63 }
64
65 uint32_t
66 gen7_surface_tiling_mode(uint32_t tiling)
67 {
68    switch (tiling) {
69    case I915_TILING_X:
70       return GEN7_SURFACE_TILING_X;
71    case I915_TILING_Y:
72       return GEN7_SURFACE_TILING_Y;
73    default:
74       return GEN7_SURFACE_TILING_NONE;
75    }
76 }
77
78
79 uint32_t
80 gen7_surface_msaa_bits(unsigned num_samples, enum intel_msaa_layout layout)
81 {
82    uint32_t ss4 = 0;
83
84    if (num_samples > 4)
85       ss4 |= GEN7_SURFACE_MULTISAMPLECOUNT_8;
86    else if (num_samples > 1)
87       ss4 |= GEN7_SURFACE_MULTISAMPLECOUNT_4;
88    else
89       ss4 |= GEN7_SURFACE_MULTISAMPLECOUNT_1;
90
91    if (layout == INTEL_MSAA_LAYOUT_IMS)
92       ss4 |= GEN7_SURFACE_MSFMT_DEPTH_STENCIL;
93    else
94       ss4 |= GEN7_SURFACE_MSFMT_MSS;
95
96    return ss4;
97 }
98
99
100 void
101 gen7_set_surface_mcs_info(struct brw_context *brw,
102                           uint32_t *surf,
103                           uint32_t surf_offset,
104                           const struct intel_mipmap_tree *mcs_mt,
105                           bool is_render_target)
106 {
107    /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
108     *
109     *     "The MCS surface must be stored as Tile Y."
110     */
111    assert(mcs_mt->region->tiling == I915_TILING_Y);
112
113    /* Compute the pitch in units of tiles.  To do this we need to divide the
114     * pitch in bytes by 128, since a single Y-tile is 128 bytes wide.
115     */
116    unsigned pitch_tiles = mcs_mt->region->pitch / 128;
117
118    /* The upper 20 bits of surface state DWORD 6 are the upper 20 bits of the
119     * GPU address of the MCS buffer; the lower 12 bits contain other control
120     * information.  Since buffer addresses are always on 4k boundaries (and
121     * thus have their lower 12 bits zero), we can use an ordinary reloc to do
122     * the necessary address translation.
123     */
124    assert ((mcs_mt->region->bo->offset & 0xfff) == 0);
125
126    surf[6] = GEN7_SURFACE_MCS_ENABLE |
127              SET_FIELD(pitch_tiles - 1, GEN7_SURFACE_MCS_PITCH) |
128              mcs_mt->region->bo->offset;
129
130    drm_intel_bo_emit_reloc(brw->intel.batch.bo,
131                            surf_offset + 6 * 4,
132                            mcs_mt->region->bo,
133                            surf[6] & 0xfff,
134                            is_render_target ? I915_GEM_DOMAIN_RENDER
135                            : I915_GEM_DOMAIN_SAMPLER,
136                            is_render_target ? I915_GEM_DOMAIN_RENDER : 0);
137 }
138
139
140 void
141 gen7_check_surface_setup(uint32_t *surf, bool is_render_target)
142 {
143    unsigned num_multisamples = surf[4] & INTEL_MASK(5, 3);
144    unsigned multisampled_surface_storage_format = surf[4] & (1 << 6);
145    unsigned surface_array_spacing = surf[0] & (1 << 10);
146    bool is_multisampled = num_multisamples != GEN7_SURFACE_MULTISAMPLECOUNT_1;
147
148    (void) surface_array_spacing;
149
150    /* From the Graphics BSpec: vol5c Shared Functions [SNB+] > State >
151     * SURFACE_STATE > SURFACE_STATE for most messages [DevIVB]: Surface Array
152     * Spacing:
153     *
154     *   If Multisampled Surface Storage Format is MSFMT_MSS and Number of
155     *   Multisamples is not MULTISAMPLECOUNT_1, this field must be set to
156     *   ARYSPC_LOD0.
157     */
158    if (multisampled_surface_storage_format == GEN7_SURFACE_MSFMT_MSS
159        && is_multisampled)
160       assert(surface_array_spacing == GEN7_SURFACE_ARYSPC_LOD0);
161
162    /* From the Graphics BSpec: vol5c Shared Functions [SNB+] > State >
163     * SURFACE_STATE > SURFACE_STATE for most messages [DevIVB]: Multisampled
164     * Surface Storage Format:
165     *
166     *   All multisampled render target surfaces must have this field set to
167     *   MSFMT_MSS.
168     *
169     * But also:
170     *
171     *   This field is ignored if Number of Multisamples is MULTISAMPLECOUNT_1.
172     */
173    if (is_render_target && is_multisampled) {
174       assert(multisampled_surface_storage_format == GEN7_SURFACE_MSFMT_MSS);
175    }
176
177    /* From the Graphics BSpec: vol5c Shared Functions [SNB+] > State >
178     * SURFACE_STATE > SURFACE_STATE for most messages [DevIVB]: Multisampled
179     * Surface Storage Format:
180     *
181     *   If the surface’s Number of Multisamples is MULTISAMPLECOUNT_8, Width
182     *   is >= 8192 (meaning the actual surface width is >= 8193 pixels), this
183     *   field must be set to MSFMT_MSS.
184     */
185    uint32_t width = GET_FIELD(surf[2], GEN7_SURFACE_WIDTH) + 1;
186    if (num_multisamples == GEN7_SURFACE_MULTISAMPLECOUNT_8 && width >= 8193) {
187       assert(multisampled_surface_storage_format == GEN7_SURFACE_MSFMT_MSS);
188    }
189
190    /* From the Graphics BSpec: vol5c Shared Functions [SNB+] > State >
191     * SURFACE_STATE > SURFACE_STATE for most messages [DevIVB]: Multisampled
192     * Surface Storage Format:
193     *
194     *   If the surface’s Number of Multisamples is MULTISAMPLECOUNT_8,
195     *   ((Depth+1) * (Height+1)) is > 4,194,304, OR if the surface’s Number of
196     *   Multisamples is MULTISAMPLECOUNT_4, ((Depth+1) * (Height+1)) is >
197     *   8,388,608, this field must be set to MSFMT_DEPTH_STENCIL.This field
198     *   must be set to MSFMT_DEPTH_STENCIL if Surface Format is one of the
199     *   following: I24X8_UNORM, L24X8_UNORM, A24X8_UNORM, or
200     *   R24_UNORM_X8_TYPELESS.
201     *
202     * But also:
203     *
204     *   This field is ignored if Number of Multisamples is MULTISAMPLECOUNT_1.
205     */
206    uint32_t depth = GET_FIELD(surf[3], BRW_SURFACE_DEPTH) + 1;
207    uint32_t height = GET_FIELD(surf[2], GEN7_SURFACE_HEIGHT) + 1;
208    if (num_multisamples == GEN7_SURFACE_MULTISAMPLECOUNT_8 &&
209        depth * height > 4194304) {
210       assert(multisampled_surface_storage_format ==
211              GEN7_SURFACE_MSFMT_DEPTH_STENCIL);
212    }
213    if (num_multisamples == GEN7_SURFACE_MULTISAMPLECOUNT_4 &&
214        depth * height > 8388608) {
215       assert(multisampled_surface_storage_format ==
216              GEN7_SURFACE_MSFMT_DEPTH_STENCIL);
217    }
218    if (is_multisampled) {
219       switch (GET_FIELD(surf[0], BRW_SURFACE_FORMAT)) {
220       case BRW_SURFACEFORMAT_I24X8_UNORM:
221       case BRW_SURFACEFORMAT_L24X8_UNORM:
222       case BRW_SURFACEFORMAT_A24X8_UNORM:
223       case BRW_SURFACEFORMAT_R24_UNORM_X8_TYPELESS:
224          assert(multisampled_surface_storage_format ==
225                 GEN7_SURFACE_MSFMT_DEPTH_STENCIL);
226       }
227    }
228 }
229
230
231 static void
232 gen7_update_buffer_texture_surface(struct gl_context *ctx,
233                                    unsigned unit,
234                                    uint32_t *binding_table,
235                                    unsigned surf_index)
236 {
237    struct brw_context *brw = brw_context(ctx);
238    struct intel_context *intel = &brw->intel;
239    struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
240    struct intel_buffer_object *intel_obj =
241       intel_buffer_object(tObj->BufferObject);
242    drm_intel_bo *bo = intel_obj ? intel_obj->buffer : NULL;
243    gl_format format = tObj->_BufferObjectFormat;
244
245    uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
246                                     8 * 4, 32, &binding_table[surf_index]);
247    memset(surf, 0, 8 * 4);
248
249    uint32_t surface_format = brw_format_for_mesa_format(format);
250    if (surface_format == 0 && format != MESA_FORMAT_RGBA_FLOAT32) {
251       _mesa_problem(NULL, "bad format %s for texture buffer\n",
252                     _mesa_get_format_name(format));
253    }
254
255    surf[0] = BRW_SURFACE_BUFFER << BRW_SURFACE_TYPE_SHIFT |
256              surface_format << BRW_SURFACE_FORMAT_SHIFT |
257              BRW_SURFACE_RC_READ_WRITE;
258
259    if (bo) {
260       surf[1] = bo->offset; /* reloc */
261
262       /* Emit relocation to surface contents.  Section 5.1.1 of the gen4
263        * bspec ("Data Cache") says that the data cache does not exist as
264        * a separate cache and is just the sampler cache.
265        */
266       drm_intel_bo_emit_reloc(intel->batch.bo,
267                               binding_table[surf_index] + 4,
268                               bo, 0,
269                               I915_GEM_DOMAIN_SAMPLER, 0);
270
271       int texel_size = _mesa_get_format_bytes(format);
272       int w = intel_obj->Base.Size / texel_size;
273
274       /* note that these differ from GEN6 */
275       surf[2] = SET_FIELD(w & 0x7f, GEN7_SURFACE_WIDTH) | /* bits 6:0 of size */
276                 SET_FIELD((w >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT); /* 20:7 */
277       surf[3] = SET_FIELD((w >> 21) & 0x3f, BRW_SURFACE_DEPTH) | /* bits 26:21 */
278                 (texel_size - 1);
279    }
280
281    gen7_check_surface_setup(surf, false /* is_render_target */);
282 }
283
284 static void
285 gen7_update_texture_surface(struct gl_context *ctx,
286                             unsigned unit,
287                             uint32_t *binding_table,
288                             unsigned surf_index)
289 {
290    struct brw_context *brw = brw_context(ctx);
291    struct intel_context *intel = &brw->intel;
292    struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
293    struct intel_texture_object *intelObj = intel_texture_object(tObj);
294    struct intel_mipmap_tree *mt = intelObj->mt;
295    struct gl_texture_image *firstImage = tObj->Image[0][tObj->BaseLevel];
296    struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
297    int width, height, depth;
298    uint32_t tile_x, tile_y;
299
300    if (tObj->Target == GL_TEXTURE_BUFFER) {
301       gen7_update_buffer_texture_surface(ctx, unit, binding_table, surf_index);
302       return;
303    }
304
305    intel_miptree_get_dimensions_for_image(firstImage, &width, &height, &depth);
306
307    uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
308                                     8 * 4, 32, &binding_table[surf_index]);
309    memset(surf, 0, 8 * 4);
310
311    uint32_t tex_format = translate_tex_format(intel,
312                                               mt->format,
313                                               firstImage->InternalFormat,
314                                               tObj->DepthMode,
315                                               sampler->sRGBDecode);
316
317    surf[0] = translate_tex_target(tObj->Target) << BRW_SURFACE_TYPE_SHIFT |
318              tex_format << BRW_SURFACE_FORMAT_SHIFT |
319              gen7_surface_tiling_mode(mt->region->tiling) |
320              BRW_SURFACE_CUBEFACE_ENABLES;
321
322    if (mt->align_h == 4)
323       surf[0] |= GEN7_SURFACE_VALIGN_4;
324    if (mt->align_w == 8)
325       surf[0] |= GEN7_SURFACE_HALIGN_8;
326
327    if (depth > 1 && tObj->Target != GL_TEXTURE_3D)
328       surf[0] |= GEN7_SURFACE_IS_ARRAY;
329
330    if (mt->array_spacing_lod0)
331       surf[0] |= GEN7_SURFACE_ARYSPC_LOD0;
332
333    surf[1] = mt->region->bo->offset + mt->offset; /* reloc */
334    surf[1] += intel_miptree_get_tile_offsets(intelObj->mt, firstImage->Level, 0,
335                                              &tile_x, &tile_y);
336
337    surf[2] = SET_FIELD(width - 1, GEN7_SURFACE_WIDTH) |
338              SET_FIELD(height - 1, GEN7_SURFACE_HEIGHT);
339    surf[3] = SET_FIELD(depth - 1, BRW_SURFACE_DEPTH) |
340              ((intelObj->mt->region->pitch) - 1);
341
342    surf[4] = gen7_surface_msaa_bits(mt->num_samples, mt->msaa_layout);
343
344    assert(brw->has_surface_tile_offset || (tile_x == 0 && tile_y == 0));
345    /* Note that the low bits of these fields are missing, so
346     * there's the possibility of getting in trouble.
347     */
348    surf[5] = ((tile_x / 4) << BRW_SURFACE_X_OFFSET_SHIFT |
349               (tile_y / 2) << BRW_SURFACE_Y_OFFSET_SHIFT |
350               /* mip count */
351               (intelObj->_MaxLevel - tObj->BaseLevel));
352
353    if (intel->is_haswell) {
354       /* Handling GL_ALPHA as a surface format override breaks 1.30+ style
355        * texturing functions that return a float, as our code generation always
356        * selects the .x channel (which would always be 0).
357        */
358       const bool alpha_depth = tObj->DepthMode == GL_ALPHA &&
359          (firstImage->_BaseFormat == GL_DEPTH_COMPONENT ||
360           firstImage->_BaseFormat == GL_DEPTH_STENCIL);
361
362       const int swizzle = unlikely(alpha_depth)
363          ? SWIZZLE_XYZW : brw_get_texture_swizzle(ctx, tObj);
364
365       surf[7] =
366          SET_FIELD(swizzle_to_scs(GET_SWZ(swizzle, 0)), GEN7_SURFACE_SCS_R) |
367          SET_FIELD(swizzle_to_scs(GET_SWZ(swizzle, 1)), GEN7_SURFACE_SCS_G) |
368          SET_FIELD(swizzle_to_scs(GET_SWZ(swizzle, 2)), GEN7_SURFACE_SCS_B) |
369          SET_FIELD(swizzle_to_scs(GET_SWZ(swizzle, 3)), GEN7_SURFACE_SCS_A);
370    }
371
372    /* Emit relocation to surface contents */
373    drm_intel_bo_emit_reloc(brw->intel.batch.bo,
374                            binding_table[surf_index] + 4,
375                            intelObj->mt->region->bo,
376                            surf[1] - intelObj->mt->region->bo->offset,
377                            I915_GEM_DOMAIN_SAMPLER, 0);
378
379    gen7_check_surface_setup(surf, false /* is_render_target */);
380 }
381
382 /**
383  * Create the constant buffer surface.  Vertex/fragment shader constants will
384  * be read from this buffer with Data Port Read instructions/messages.
385  */
386 static void
387 gen7_create_constant_surface(struct brw_context *brw,
388                              drm_intel_bo *bo,
389                              uint32_t offset,
390                              uint32_t size,
391                              uint32_t *out_offset,
392                              bool dword_pitch)
393 {
394    struct intel_context *intel = &brw->intel;
395    uint32_t stride = dword_pitch ? 4 : 16;
396    uint32_t elements = ALIGN(size, stride) / stride;
397    const GLint w = elements - 1;
398
399    uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
400                                     8 * 4, 32, out_offset);
401    memset(surf, 0, 8 * 4);
402
403    surf[0] = BRW_SURFACE_BUFFER << BRW_SURFACE_TYPE_SHIFT |
404              BRW_SURFACEFORMAT_R32G32B32A32_FLOAT << BRW_SURFACE_FORMAT_SHIFT |
405              BRW_SURFACE_RC_READ_WRITE;
406
407    assert(bo);
408    surf[1] = bo->offset + offset; /* reloc */
409
410    /* note that these differ from GEN6 */
411    surf[2] = SET_FIELD(w & 0x7f, GEN7_SURFACE_WIDTH) |
412              SET_FIELD((w >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT);
413    surf[3] = SET_FIELD((w >> 21) & 0x3f, BRW_SURFACE_DEPTH) |
414              (stride - 1);
415
416    if (intel->is_haswell) {
417       surf[7] = SET_FIELD(HSW_SCS_RED,   GEN7_SURFACE_SCS_R) |
418                 SET_FIELD(HSW_SCS_GREEN, GEN7_SURFACE_SCS_G) |
419                 SET_FIELD(HSW_SCS_BLUE,  GEN7_SURFACE_SCS_B) |
420                 SET_FIELD(HSW_SCS_ALPHA, GEN7_SURFACE_SCS_A);
421    }
422
423    /* Emit relocation to surface contents.  Section 5.1.1 of the gen4
424     * bspec ("Data Cache") says that the data cache does not exist as
425     * a separate cache and is just the sampler cache.
426     */
427    drm_intel_bo_emit_reloc(intel->batch.bo,
428                            *out_offset + 4,
429                            bo, offset,
430                            I915_GEM_DOMAIN_SAMPLER, 0);
431
432    gen7_check_surface_setup(surf, false /* is_render_target */);
433 }
434
435 /**
436  * Create a surface for shader time.
437  */
438 void
439 gen7_create_shader_time_surface(struct brw_context *brw, uint32_t *out_offset)
440 {
441    struct intel_context *intel = &brw->intel;
442    const int w = brw->shader_time.bo->size - 1;
443
444    uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
445                                     8 * 4, 32, out_offset);
446    memset(surf, 0, 8 * 4);
447
448    surf[0] = BRW_SURFACE_BUFFER << BRW_SURFACE_TYPE_SHIFT |
449              BRW_SURFACEFORMAT_RAW << BRW_SURFACE_FORMAT_SHIFT |
450              BRW_SURFACE_RC_READ_WRITE;
451
452    surf[1] = brw->shader_time.bo->offset; /* reloc */
453
454    /* note that these differ from GEN6 */
455    surf[2] = SET_FIELD(w & 0x7f, GEN7_SURFACE_WIDTH) |
456              SET_FIELD((w >> 7) & 0x3fff, GEN7_SURFACE_HEIGHT);
457    surf[3] = SET_FIELD((w >> 21) & 0x3f, BRW_SURFACE_DEPTH);
458
459    /* Unlike texture or renderbuffer surfaces, we only do untyped operations
460     * on the shader_time surface, so there's no need to set HSW channel
461     * overrides.
462     */
463
464    /* Emit relocation to surface contents.  Section 5.1.1 of the gen4
465     * bspec ("Data Cache") says that the data cache does not exist as
466     * a separate cache and is just the sampler cache.
467     */
468    drm_intel_bo_emit_reloc(intel->batch.bo,
469                            *out_offset + 4,
470                            brw->shader_time.bo, 0,
471                            I915_GEM_DOMAIN_SAMPLER, 0);
472
473    gen7_check_surface_setup(surf, false /* is_render_target */);
474 }
475
476 static void
477 gen7_update_null_renderbuffer_surface(struct brw_context *brw, unsigned unit)
478 {
479    /* From the Ivy bridge PRM, Vol4 Part1 p62 (Surface Type: Programming
480     * Notes):
481     *
482     *     A null surface is used in instances where an actual surface is not
483     *     bound. When a write message is generated to a null surface, no
484     *     actual surface is written to. When a read message (including any
485     *     sampling engine message) is generated to a null surface, the result
486     *     is all zeros. Note that a null surface type is allowed to be used
487     *     with all messages, even if it is not specificially indicated as
488     *     supported. All of the remaining fields in surface state are ignored
489     *     for null surfaces, with the following exceptions: Width, Height,
490     *     Depth, LOD, and Render Target View Extent fields must match the
491     *     depth buffer’s corresponding state for all render target surfaces,
492     *     including null.
493     */
494    struct intel_context *intel = &brw->intel;
495    struct gl_context *ctx = &intel->ctx;
496
497    /* _NEW_BUFFERS */
498    const struct gl_framebuffer *fb = ctx->DrawBuffer;
499
500    uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
501                                     8 * 4, 32, &brw->wm.surf_offset[unit]);
502    memset(surf, 0, 8 * 4);
503
504    /* From the Ivybridge PRM, Volume 4, Part 1, page 65,
505     * Tiled Surface: Programming Notes:
506     * "If Surface Type is SURFTYPE_NULL, this field must be TRUE."
507     */
508    surf[0] = BRW_SURFACE_NULL << BRW_SURFACE_TYPE_SHIFT |
509              BRW_SURFACEFORMAT_B8G8R8A8_UNORM << BRW_SURFACE_FORMAT_SHIFT |
510              GEN7_SURFACE_TILING_Y;
511
512    surf[2] = SET_FIELD(fb->Width - 1, GEN7_SURFACE_WIDTH) |
513              SET_FIELD(fb->Height - 1, GEN7_SURFACE_HEIGHT);
514
515    gen7_check_surface_setup(surf, true /* is_render_target */);
516 }
517
518 /**
519  * Sets up a surface state structure to point at the given region.
520  * While it is only used for the front/back buffer currently, it should be
521  * usable for further buffers when doing ARB_draw_buffer support.
522  */
523 static void
524 gen7_update_renderbuffer_surface(struct brw_context *brw,
525                                  struct gl_renderbuffer *rb,
526                                  unsigned int unit)
527 {
528    struct intel_context *intel = &brw->intel;
529    struct gl_context *ctx = &intel->ctx;
530    struct intel_renderbuffer *irb = intel_renderbuffer(rb);
531    struct intel_region *region = irb->mt->region;
532    uint32_t tile_x, tile_y;
533    uint32_t format;
534    /* _NEW_BUFFERS */
535    gl_format rb_format = _mesa_get_render_format(ctx, intel_rb_format(irb));
536
537    uint32_t *surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
538                                     8 * 4, 32, &brw->wm.surf_offset[unit]);
539    memset(surf, 0, 8 * 4);
540
541    /* Render targets can't use IMS layout */
542    assert(irb->mt->msaa_layout != INTEL_MSAA_LAYOUT_IMS);
543
544    assert(brw_render_target_supported(intel, rb));
545    format = brw->render_target_format[rb_format];
546    if (unlikely(!brw->format_supported_as_render_target[rb_format])) {
547       _mesa_problem(ctx, "%s: renderbuffer format %s unsupported\n",
548                     __FUNCTION__, _mesa_get_format_name(rb_format));
549    }
550
551    surf[0] = BRW_SURFACE_2D << BRW_SURFACE_TYPE_SHIFT |
552              format << BRW_SURFACE_FORMAT_SHIFT |
553              (irb->mt->array_spacing_lod0 ? GEN7_SURFACE_ARYSPC_LOD0
554                                           : GEN7_SURFACE_ARYSPC_FULL) |
555              gen7_surface_tiling_mode(region->tiling);
556
557    if (irb->mt->align_h == 4)
558       surf[0] |= GEN7_SURFACE_VALIGN_4;
559    if (irb->mt->align_w == 8)
560       surf[0] |= GEN7_SURFACE_HALIGN_8;
561
562    /* reloc */
563    surf[1] = intel_renderbuffer_get_tile_offsets(irb, &tile_x, &tile_y) +
564              region->bo->offset; /* reloc */
565
566    assert(brw->has_surface_tile_offset);
567    /* Note that the low bits of these fields are missing, so
568     * there's the possibility of getting in trouble.
569     */
570    assert(tile_x % 4 == 0);
571    assert(tile_y % 2 == 0);
572    surf[5] = SET_FIELD(tile_x / 4, BRW_SURFACE_X_OFFSET) |
573              SET_FIELD(tile_y / 2, BRW_SURFACE_Y_OFFSET);
574
575    surf[2] = SET_FIELD(rb->Width - 1, GEN7_SURFACE_WIDTH) |
576              SET_FIELD(rb->Height - 1, GEN7_SURFACE_HEIGHT);
577    surf[3] = region->pitch - 1;
578
579    surf[4] = gen7_surface_msaa_bits(irb->mt->num_samples, irb->mt->msaa_layout);
580
581    if (irb->mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) {
582       gen7_set_surface_mcs_info(brw, surf, brw->wm.surf_offset[unit],
583                                 irb->mt->mcs_mt, true /* is RT */);
584    }
585
586    if (intel->is_haswell) {
587       surf[7] = SET_FIELD(HSW_SCS_RED,   GEN7_SURFACE_SCS_R) |
588                 SET_FIELD(HSW_SCS_GREEN, GEN7_SURFACE_SCS_G) |
589                 SET_FIELD(HSW_SCS_BLUE,  GEN7_SURFACE_SCS_B) |
590                 SET_FIELD(HSW_SCS_ALPHA, GEN7_SURFACE_SCS_A);
591    }
592
593    drm_intel_bo_emit_reloc(brw->intel.batch.bo,
594                            brw->wm.surf_offset[unit] + 4,
595                            region->bo,
596                            surf[1] - region->bo->offset,
597                            I915_GEM_DOMAIN_RENDER,
598                            I915_GEM_DOMAIN_RENDER);
599
600    gen7_check_surface_setup(surf, true /* is_render_target */);
601 }
602
603 void
604 gen7_init_vtable_surface_functions(struct brw_context *brw)
605 {
606    struct intel_context *intel = &brw->intel;
607
608    intel->vtbl.update_texture_surface = gen7_update_texture_surface;
609    intel->vtbl.update_renderbuffer_surface = gen7_update_renderbuffer_surface;
610    intel->vtbl.update_null_renderbuffer_surface =
611       gen7_update_null_renderbuffer_surface;
612    intel->vtbl.create_constant_surface = gen7_create_constant_surface;
613 }