d20bdb5bfe300606e87b1afaff55768c0c1f4de0
[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/samplerobj.h"
25 #include "program/prog_parameter.h"
26
27 #include "intel_mipmap_tree.h"
28 #include "intel_batchbuffer.h"
29 #include "intel_tex.h"
30 #include "intel_fbo.h"
31
32 #include "brw_context.h"
33 #include "brw_state.h"
34 #include "brw_defines.h"
35 #include "brw_wm.h"
36
37 static void
38 gen7_set_surface_tiling(struct gen7_surface_state *surf, uint32_t tiling)
39 {
40 switch (tiling) {
41 case I915_TILING_NONE:
42 surf->ss0.tiled_surface = 0;
43 surf->ss0.tile_walk = 0;
44 break;
45 case I915_TILING_X:
46 surf->ss0.tiled_surface = 1;
47 surf->ss0.tile_walk = BRW_TILEWALK_XMAJOR;
48 break;
49 case I915_TILING_Y:
50 surf->ss0.tiled_surface = 1;
51 surf->ss0.tile_walk = BRW_TILEWALK_YMAJOR;
52 break;
53 }
54 }
55
56 static void
57 gen7_update_texture_surface(struct gl_context *ctx, GLuint unit)
58 {
59 struct brw_context *brw = brw_context(ctx);
60 struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
61 struct intel_texture_object *intelObj = intel_texture_object(tObj);
62 struct intel_mipmap_tree *mt = intelObj->mt;
63 struct gl_texture_image *firstImage = tObj->Image[0][tObj->BaseLevel];
64 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
65 const GLuint surf_index = SURF_INDEX_TEXTURE(unit);
66 struct gen7_surface_state *surf;
67 int width, height, depth;
68
69 intel_miptree_get_dimensions_for_image(firstImage, &width, &height, &depth);
70
71 surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
72 sizeof(*surf), 32, &brw->bind.surf_offset[surf_index]);
73 memset(surf, 0, sizeof(*surf));
74
75 if (mt->align_h == 4)
76 surf->ss0.vertical_alignment = 1;
77
78 surf->ss0.surface_type = translate_tex_target(tObj->Target);
79 surf->ss0.surface_format = translate_tex_format(firstImage->TexFormat,
80 firstImage->InternalFormat,
81 sampler->DepthMode,
82 sampler->sRGBDecode);
83 if (tObj->Target == GL_TEXTURE_CUBE_MAP) {
84 surf->ss0.cube_pos_x = 1;
85 surf->ss0.cube_pos_y = 1;
86 surf->ss0.cube_pos_z = 1;
87 surf->ss0.cube_neg_x = 1;
88 surf->ss0.cube_neg_y = 1;
89 surf->ss0.cube_neg_z = 1;
90 }
91
92 gen7_set_surface_tiling(surf, intelObj->mt->region->tiling);
93
94 /* ss0 remaining fields:
95 * - is_array
96 * - vertical_alignment
97 * - horizontal_alignment
98 * - vert_line_stride (exists on gen6 but we ignore it)
99 * - vert_line_stride_ofs (exists on gen6 but we ignore it)
100 * - surface_array_spacing
101 * - render_cache_read_write (exists on gen6 but ignored here)
102 */
103
104 surf->ss1.base_addr = intelObj->mt->region->bo->offset; /* reloc */
105
106 surf->ss2.width = width - 1;
107 surf->ss2.height = height - 1;
108
109 surf->ss3.pitch = (intelObj->mt->region->pitch * intelObj->mt->cpp) - 1;
110 surf->ss3.depth = depth - 1;
111
112 /* ss4: ignored? */
113
114 surf->ss5.mip_count = intelObj->_MaxLevel - tObj->BaseLevel;
115 surf->ss5.min_lod = 0;
116
117 /* ss5 remaining fields:
118 * - x_offset (N/A for textures?)
119 * - y_offset (ditto)
120 * - cache_control
121 */
122
123 /* Emit relocation to surface contents */
124 drm_intel_bo_emit_reloc(brw->intel.batch.bo,
125 brw->bind.surf_offset[surf_index] +
126 offsetof(struct gen7_surface_state, ss1),
127 intelObj->mt->region->bo, 0,
128 I915_GEM_DOMAIN_SAMPLER, 0);
129 }
130
131 /**
132 * Create the constant buffer surface. Vertex/fragment shader constants will
133 * be read from this buffer with Data Port Read instructions/messages.
134 */
135 void
136 gen7_create_constant_surface(struct brw_context *brw,
137 drm_intel_bo *bo,
138 int width,
139 uint32_t *out_offset)
140 {
141 const GLint w = width - 1;
142 struct gen7_surface_state *surf;
143
144 surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
145 sizeof(*surf), 32, out_offset);
146 memset(surf, 0, sizeof(*surf));
147
148 surf->ss0.surface_type = BRW_SURFACE_BUFFER;
149 surf->ss0.surface_format = BRW_SURFACEFORMAT_R32G32B32A32_FLOAT;
150
151 surf->ss0.render_cache_read_write = 1;
152
153 assert(bo);
154 surf->ss1.base_addr = bo->offset; /* reloc */
155
156 surf->ss2.width = w & 0x7f; /* bits 6:0 of size or width */
157 surf->ss2.height = (w >> 7) & 0x1fff; /* bits 19:7 of size or width */
158 surf->ss3.depth = (w >> 20) & 0x7f; /* bits 26:20 of size or width */
159 surf->ss3.pitch = (width * 16) - 1; /* ignored?? */
160 gen7_set_surface_tiling(surf, I915_TILING_NONE); /* tiling now allowed */
161
162 /* Emit relocation to surface contents. Section 5.1.1 of the gen4
163 * bspec ("Data Cache") says that the data cache does not exist as
164 * a separate cache and is just the sampler cache.
165 */
166 drm_intel_bo_emit_reloc(brw->intel.batch.bo,
167 (*out_offset +
168 offsetof(struct gen7_surface_state, ss1)),
169 bo, 0,
170 I915_GEM_DOMAIN_SAMPLER, 0);
171 }
172
173 static void
174 gen7_update_null_renderbuffer_surface(struct brw_context *brw, unsigned unit)
175 {
176 struct gen7_surface_state *surf;
177
178 surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
179 sizeof(*surf), 32, &brw->bind.surf_offset[unit]);
180 memset(surf, 0, sizeof(*surf));
181
182 surf->ss0.surface_type = BRW_SURFACE_NULL;
183 surf->ss0.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
184 }
185
186 /**
187 * Sets up a surface state structure to point at the given region.
188 * While it is only used for the front/back buffer currently, it should be
189 * usable for further buffers when doing ARB_draw_buffer support.
190 */
191 static void
192 gen7_update_renderbuffer_surface(struct brw_context *brw,
193 struct gl_renderbuffer *rb,
194 unsigned int unit)
195 {
196 struct intel_context *intel = &brw->intel;
197 struct gl_context *ctx = &intel->ctx;
198 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
199 struct intel_region *region = irb->mt->region;
200 struct gen7_surface_state *surf;
201 uint32_t tile_x, tile_y;
202
203 surf = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
204 sizeof(*surf), 32, &brw->bind.surf_offset[unit]);
205 memset(surf, 0, sizeof(*surf));
206
207 if (irb->mt->align_h == 4)
208 surf->ss0.vertical_alignment = 1;
209
210 switch (irb->Base.Format) {
211 case MESA_FORMAT_SARGB8:
212 /* without GL_EXT_framebuffer_sRGB we shouldn't bind sRGB
213 surfaces to the blend/update as sRGB */
214 if (ctx->Color.sRGBEnabled)
215 surf->ss0.surface_format = brw_format_for_mesa_format(irb->Base.Format);
216 else
217 surf->ss0.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
218 break;
219 default:
220 assert(brw_render_target_supported(intel, irb->Base.Format));
221 surf->ss0.surface_format = brw->render_target_format[irb->Base.Format];
222 if (unlikely(!brw->format_supported_as_render_target[irb->Base.Format])) {
223 _mesa_problem(ctx, "%s: renderbuffer format %s unsupported\n",
224 __FUNCTION__, _mesa_get_format_name(irb->Base.Format));
225 }
226 break;
227 }
228
229 surf->ss0.surface_type = BRW_SURFACE_2D;
230 /* reloc */
231 surf->ss1.base_addr = intel_renderbuffer_tile_offsets(irb, &tile_x, &tile_y);
232 surf->ss1.base_addr += region->bo->offset; /* reloc */
233
234 assert(brw->has_surface_tile_offset);
235 /* Note that the low bits of these fields are missing, so
236 * there's the possibility of getting in trouble.
237 */
238 assert(tile_x % 4 == 0);
239 assert(tile_y % 2 == 0);
240 surf->ss5.x_offset = tile_x / 4;
241 surf->ss5.y_offset = tile_y / 2;
242
243 surf->ss2.width = rb->Width - 1;
244 surf->ss2.height = rb->Height - 1;
245 gen7_set_surface_tiling(surf, region->tiling);
246 surf->ss3.pitch = (region->pitch * region->cpp) - 1;
247
248 drm_intel_bo_emit_reloc(brw->intel.batch.bo,
249 brw->bind.surf_offset[unit] +
250 offsetof(struct gen7_surface_state, ss1),
251 region->bo,
252 surf->ss1.base_addr - region->bo->offset,
253 I915_GEM_DOMAIN_RENDER,
254 I915_GEM_DOMAIN_RENDER);
255 }
256
257 void
258 gen7_init_vtable_surface_functions(struct brw_context *brw)
259 {
260 struct intel_context *intel = &brw->intel;
261
262 intel->vtbl.update_texture_surface = gen7_update_texture_surface;
263 intel->vtbl.update_renderbuffer_surface = gen7_update_renderbuffer_surface;
264 intel->vtbl.update_null_renderbuffer_surface =
265 gen7_update_null_renderbuffer_surface;
266 intel->vtbl.create_constant_surface = gen7_create_constant_surface;
267 }