isl/state: Put pitch calculations together
[mesa.git] / src / intel / isl / isl_surface_state.c
1 /*
2 * Copyright 2016 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
24 #include <stdint.h>
25
26 #define __gen_address_type uint64_t
27 #define __gen_user_data void
28
29 static inline uint64_t
30 __gen_combine_address(void *data, void *loc, uint64_t addr, uint32_t delta)
31 {
32 return addr + delta;
33 }
34
35 #include "genxml/gen_macros.h"
36 #include "genxml/genX_pack.h"
37
38 #include "isl_priv.h"
39
40 #define __PASTE2(x, y) x ## y
41 #define __PASTE(x, y) __PASTE2(x, y)
42 #define isl_genX(x) __PASTE(isl_, genX(x))
43
44 #if GEN_GEN >= 8
45 static const uint8_t isl_to_gen_halign[] = {
46 [4] = HALIGN4,
47 [8] = HALIGN8,
48 [16] = HALIGN16,
49 };
50
51 static const uint8_t isl_to_gen_valign[] = {
52 [4] = VALIGN4,
53 [8] = VALIGN8,
54 [16] = VALIGN16,
55 };
56 #else
57 static const uint8_t isl_to_gen_halign[] = {
58 [4] = HALIGN_4,
59 [8] = HALIGN_8,
60 };
61
62 static const uint8_t isl_to_gen_valign[] = {
63 [2] = VALIGN_2,
64 [4] = VALIGN_4,
65 };
66 #endif
67
68 #if GEN_GEN >= 8
69 static const uint8_t isl_to_gen_tiling[] = {
70 [ISL_TILING_LINEAR] = LINEAR,
71 [ISL_TILING_X] = XMAJOR,
72 [ISL_TILING_Y0] = YMAJOR,
73 [ISL_TILING_Yf] = YMAJOR,
74 [ISL_TILING_Ys] = YMAJOR,
75 [ISL_TILING_W] = WMAJOR,
76 };
77 #endif
78
79 static const uint32_t isl_to_gen_multisample_layout[] = {
80 [ISL_MSAA_LAYOUT_NONE] = MSFMT_MSS,
81 [ISL_MSAA_LAYOUT_INTERLEAVED] = MSFMT_DEPTH_STENCIL,
82 [ISL_MSAA_LAYOUT_ARRAY] = MSFMT_MSS,
83 };
84
85 static uint8_t
86 get_surftype(enum isl_surf_dim dim, isl_surf_usage_flags_t usage)
87 {
88 switch (dim) {
89 default:
90 unreachable("bad isl_surf_dim");
91 case ISL_SURF_DIM_1D:
92 assert(!(usage & ISL_SURF_USAGE_CUBE_BIT));
93 return SURFTYPE_1D;
94 case ISL_SURF_DIM_2D:
95 if (usage & ISL_SURF_USAGE_STORAGE_BIT) {
96 /* Storage images are always plain 2-D, not cube */
97 return SURFTYPE_2D;
98 } else if (usage & ISL_SURF_USAGE_CUBE_BIT) {
99 return SURFTYPE_CUBE;
100 } else {
101 return SURFTYPE_2D;
102 }
103 case ISL_SURF_DIM_3D:
104 assert(!(usage & ISL_SURF_USAGE_CUBE_BIT));
105 return SURFTYPE_3D;
106 }
107 }
108
109 /**
110 * Get the values to pack into RENDER_SUFFACE_STATE.SurfaceHorizontalAlignment
111 * and SurfaceVerticalAlignment.
112 */
113 static void
114 get_halign_valign(const struct isl_surf *surf,
115 uint32_t *halign, uint32_t *valign)
116 {
117 if (GEN_GEN >= 9) {
118 if (isl_tiling_is_std_y(surf->tiling) ||
119 surf->dim_layout == ISL_DIM_LAYOUT_GEN9_1D) {
120 /* The hardware ignores the alignment values. Anyway, the surface's
121 * true alignment is likely outside the enum range of HALIGN* and
122 * VALIGN*.
123 */
124 *halign = 0;
125 *valign = 0;
126 } else {
127 /* In Skylake, RENDER_SUFFACE_STATE.SurfaceVerticalAlignment is in units
128 * of surface elements (not pixels nor samples). For compressed formats,
129 * a "surface element" is defined as a compression block. For example,
130 * if SurfaceVerticalAlignment is VALIGN_4 and SurfaceFormat is an ETC2
131 * format (ETC2 has a block height of 4), then the vertical alignment is
132 * 4 compression blocks or, equivalently, 16 pixels.
133 */
134 struct isl_extent3d image_align_el
135 = isl_surf_get_image_alignment_el(surf);
136
137 *halign = isl_to_gen_halign[image_align_el.width];
138 *valign = isl_to_gen_valign[image_align_el.height];
139 }
140 } else {
141 /* Pre-Skylake, RENDER_SUFFACE_STATE.SurfaceVerticalAlignment is in
142 * units of surface samples. For example, if SurfaceVerticalAlignment
143 * is VALIGN_4 and the surface is singlesampled, then for any surface
144 * format (compressed or not) the vertical alignment is
145 * 4 pixels.
146 */
147 struct isl_extent3d image_align_sa
148 = isl_surf_get_image_alignment_sa(surf);
149
150 *halign = isl_to_gen_halign[image_align_sa.width];
151 *valign = isl_to_gen_valign[image_align_sa.height];
152 }
153 }
154
155 #if GEN_GEN >= 8
156 static uint32_t
157 get_qpitch(const struct isl_surf *surf)
158 {
159 switch (surf->dim) {
160 default:
161 unreachable("Bad isl_surf_dim");
162 case ISL_SURF_DIM_1D:
163 if (GEN_GEN >= 9) {
164 /* QPitch is usually expressed as rows of surface elements (where
165 * a surface element is an compression block or a single surface
166 * sample). Skylake 1D is an outlier.
167 *
168 * From the Skylake BSpec >> Memory Views >> Common Surface
169 * Formats >> Surface Layout and Tiling >> 1D Surfaces:
170 *
171 * Surface QPitch specifies the distance in pixels between array
172 * slices.
173 */
174 return isl_surf_get_array_pitch_el(surf);
175 } else {
176 return isl_surf_get_array_pitch_el_rows(surf);
177 }
178 case ISL_SURF_DIM_2D:
179 case ISL_SURF_DIM_3D:
180 if (GEN_GEN >= 9) {
181 return isl_surf_get_array_pitch_el_rows(surf);
182 } else {
183 /* From the Broadwell PRM for RENDER_SURFACE_STATE.QPitch
184 *
185 * "This field must be set to an integer multiple of the Surface
186 * Vertical Alignment. For compressed textures (BC*, FXT1,
187 * ETC*, and EAC* Surface Formats), this field is in units of
188 * rows in the uncompressed surface, and must be set to an
189 * integer multiple of the vertical alignment parameter "j"
190 * defined in the Common Surface Formats section."
191 */
192 return isl_surf_get_array_pitch_sa_rows(surf);
193 }
194 }
195 }
196 #endif /* GEN_GEN >= 8 */
197
198 void
199 isl_genX(surf_fill_state_s)(const struct isl_device *dev, void *state,
200 const struct isl_surf_fill_state_info *restrict info)
201 {
202 uint32_t halign, valign;
203 get_halign_valign(info->surf, &halign, &valign);
204
205 struct GENX(RENDER_SURFACE_STATE) s = { 0 };
206
207 s.SurfaceType = get_surftype(info->surf->dim, info->view->usage);
208
209 if (info->view->usage & ISL_SURF_USAGE_STORAGE_BIT) {
210 s.SurfaceFormat =
211 isl_lower_storage_image_format(dev->info, info->view->format);
212 } else {
213 s.SurfaceFormat = info->view->format;
214 }
215
216 s.Width = info->surf->logical_level0_px.width - 1;
217 s.Height = info->surf->logical_level0_px.height - 1;
218
219 switch (s.SurfaceType) {
220 case SURFTYPE_1D:
221 case SURFTYPE_2D:
222 s.MinimumArrayElement = info->view->base_array_layer;
223
224 /* From the Broadwell PRM >> RENDER_SURFACE_STATE::Depth:
225 *
226 * For SURFTYPE_1D, 2D, and CUBE: The range of this field is reduced
227 * by one for each increase from zero of Minimum Array Element. For
228 * example, if Minimum Array Element is set to 1024 on a 2D surface,
229 * the range of this field is reduced to [0,1023].
230 *
231 * In other words, 'Depth' is the number of array layers.
232 */
233 s.Depth = info->view->array_len - 1;
234
235 /* From the Broadwell PRM >> RENDER_SURFACE_STATE::RenderTargetViewExtent:
236 *
237 * For Render Target and Typed Dataport 1D and 2D Surfaces:
238 * This field must be set to the same value as the Depth field.
239 */
240 s.RenderTargetViewExtent = s.Depth;
241 break;
242 case SURFTYPE_CUBE:
243 s.MinimumArrayElement = info->view->base_array_layer;
244 /* Same as SURFTYPE_2D, but divided by 6 */
245 s.Depth = info->view->array_len / 6 - 1;
246 s.RenderTargetViewExtent = s.Depth;
247 break;
248 case SURFTYPE_3D:
249 s.MinimumArrayElement = info->view->base_array_layer;
250
251 /* From the Broadwell PRM >> RENDER_SURFACE_STATE::Depth:
252 *
253 * If the volume texture is MIP-mapped, this field specifies the
254 * depth of the base MIP level.
255 */
256 s.Depth = info->surf->logical_level0_px.depth - 1;
257
258 /* From the Broadwell PRM >> RENDER_SURFACE_STATE::RenderTargetViewExtent:
259 *
260 * For Render Target and Typed Dataport 3D Surfaces: This field
261 * indicates the extent of the accessible 'R' coordinates minus 1 on
262 * the LOD currently being rendered to.
263 */
264 s.RenderTargetViewExtent = isl_minify(info->surf->logical_level0_px.depth,
265 info->view->base_level) - 1;
266 break;
267 default:
268 unreachable("bad SurfaceType");
269 }
270
271 s.SurfaceArray = info->surf->phys_level0_sa.array_len > 1;
272
273 if (info->view->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) {
274 /* For render target surfaces, the hardware interprets field
275 * MIPCount/LOD as LOD. The Broadwell PRM says:
276 *
277 * MIPCountLOD defines the LOD that will be rendered into.
278 * SurfaceMinLOD is ignored.
279 */
280 s.MIPCountLOD = info->view->base_level;
281 s.SurfaceMinLOD = 0;
282 } else {
283 /* For non render target surfaces, the hardware interprets field
284 * MIPCount/LOD as MIPCount. The range of levels accessible by the
285 * sampler engine is [SurfaceMinLOD, SurfaceMinLOD + MIPCountLOD].
286 */
287 s.SurfaceMinLOD = info->view->base_level;
288 s.MIPCountLOD = MAX(info->view->levels, 1) - 1;
289 }
290
291 s.SurfaceVerticalAlignment = valign;
292 s.SurfaceHorizontalAlignment = halign;
293
294 if (info->surf->tiling == ISL_TILING_W) {
295 /* From the Broadwell PRM documentation for this field:
296 *
297 * "If the surface is a stencil buffer (and thus has Tile Mode set
298 * to TILEMODE_WMAJOR), the pitch must be set to 2x the value
299 * computed based on width, as the stencil buffer is stored with
300 * two rows interleaved."
301 */
302 s.SurfacePitch = info->surf->row_pitch * 2 - 1;
303 } else {
304 s.SurfacePitch = info->surf->row_pitch - 1;
305 }
306
307 #if GEN_GEN >= 8
308 s.SurfaceQPitch = get_qpitch(info->surf) >> 2;
309 #elif GEN_GEN == 7
310 s.SurfaceArraySpacing = info->surf->array_pitch_span ==
311 ISL_ARRAY_PITCH_SPAN_COMPACT;
312 #endif
313
314 #if GEN_GEN >= 8
315 s.TileMode = isl_to_gen_tiling[info->surf->tiling];
316 #else
317 s.TiledSurface = info->surf->tiling != ISL_TILING_LINEAR,
318 s.TileWalk = info->surf->tiling == ISL_TILING_X ? TILEWALK_XMAJOR :
319 TILEWALK_YMAJOR;
320 #endif
321
322 #if GEN_GEN >= 8
323 s.SamplerL2BypassModeDisable = true;
324 #endif
325
326 #if GEN_GEN >= 8
327 s.RenderCacheReadWriteMode = WriteOnlyCache;
328 #else
329 s.RenderCacheReadWriteMode = 0;
330 #endif
331
332 #if GEN_GEN >= 8
333 s.CubeFaceEnablePositiveZ = 1;
334 s.CubeFaceEnableNegativeZ = 1;
335 s.CubeFaceEnablePositiveY = 1;
336 s.CubeFaceEnableNegativeY = 1;
337 s.CubeFaceEnablePositiveX = 1;
338 s.CubeFaceEnableNegativeX = 1;
339 #else
340 s.CubeFaceEnables = 0x3f;
341 #endif
342
343 s.MultisampledSurfaceStorageFormat =
344 isl_to_gen_multisample_layout[info->surf->msaa_layout];
345 s.NumberofMultisamples = ffs(info->surf->samples) - 1;
346
347 #if (GEN_GEN >= 8 || GEN_IS_HASWELL)
348 s.ShaderChannelSelectRed = info->view->channel_select[0];
349 s.ShaderChannelSelectGreen = info->view->channel_select[1];
350 s.ShaderChannelSelectBlue = info->view->channel_select[2];
351 s.ShaderChannelSelectAlpha = info->view->channel_select[3];
352 #endif
353
354 s.SurfaceBaseAddress = info->address;
355 s.MOCS = info->mocs;
356
357 #if GEN_GEN >= 8
358 s.AuxiliarySurfaceMode = AUX_NONE;
359 #else
360 s.MCSEnable = false;
361 #endif
362
363 #if GEN_GEN >= 8
364 /* From the CHV PRM, Volume 2d, page 321 (RENDER_SURFACE_STATE dword 0
365 * bit 9 "Sampler L2 Bypass Mode Disable" Programming Notes):
366 *
367 * This bit must be set for the following surface types: BC2_UNORM
368 * BC3_UNORM BC5_UNORM BC5_SNORM BC7_UNORM
369 */
370 if (GEN_GEN >= 9 || dev->info->is_cherryview) {
371 switch (info->view->format) {
372 case ISL_FORMAT_BC2_UNORM:
373 case ISL_FORMAT_BC3_UNORM:
374 case ISL_FORMAT_BC5_UNORM:
375 case ISL_FORMAT_BC5_SNORM:
376 case ISL_FORMAT_BC7_UNORM:
377 s.SamplerL2BypassModeDisable = true;
378 break;
379 default:
380 break;
381 }
382 }
383 #endif
384
385 if (GEN_GEN <= 8) {
386 /* Prior to Sky Lake, we only have one bit for the clear color which
387 * gives us 0 or 1 in whatever the surface's format happens to be.
388 */
389 if (isl_format_has_int_channel(info->view->format)) {
390 for (unsigned i = 0; i < 4; i++) {
391 assert(info->clear_color.u32[i] == 0 ||
392 info->clear_color.u32[i] == 1);
393 }
394 } else {
395 for (unsigned i = 0; i < 4; i++) {
396 assert(info->clear_color.f32[i] == 0.0f ||
397 info->clear_color.f32[i] == 1.0f);
398 }
399 }
400 s.RedClearColor = info->clear_color.u32[0] != 0;
401 s.GreenClearColor = info->clear_color.u32[1] != 0;
402 s.BlueClearColor = info->clear_color.u32[2] != 0;
403 s.AlphaClearColor = info->clear_color.u32[3] != 0;
404 } else {
405 s.RedClearColor = info->clear_color.u32[0];
406 s.GreenClearColor = info->clear_color.u32[1];
407 s.BlueClearColor = info->clear_color.u32[2];
408 s.AlphaClearColor = info->clear_color.u32[3];
409 }
410
411 GENX(RENDER_SURFACE_STATE_pack)(NULL, state, &s);
412 }
413
414 void
415 isl_genX(buffer_fill_state_s)(void *state,
416 const struct isl_buffer_fill_state_info *restrict info)
417 {
418 uint32_t num_elements = info->size / info->stride;
419
420 struct GENX(RENDER_SURFACE_STATE) surface_state = {
421 .SurfaceType = SURFTYPE_BUFFER,
422 .SurfaceArray = false,
423 .SurfaceFormat = info->format,
424 .SurfaceVerticalAlignment = isl_to_gen_valign[4],
425 .SurfaceHorizontalAlignment = isl_to_gen_halign[4],
426 .Height = ((num_elements - 1) >> 7) & 0x3fff,
427 .Width = (num_elements - 1) & 0x7f,
428 .Depth = ((num_elements - 1) >> 21) & 0x3f,
429 .SurfacePitch = info->stride - 1,
430 .NumberofMultisamples = MULTISAMPLECOUNT_1,
431
432 #if (GEN_GEN >= 8)
433 .TileMode = LINEAR,
434 #else
435 .TiledSurface = false,
436 #endif
437
438 #if (GEN_GEN >= 8)
439 .SamplerL2BypassModeDisable = true,
440 .RenderCacheReadWriteMode = WriteOnlyCache,
441 #else
442 .RenderCacheReadWriteMode = 0,
443 #endif
444
445 .MOCS = info->mocs,
446
447 #if (GEN_GEN >= 8 || GEN_IS_HASWELL)
448 .ShaderChannelSelectRed = SCS_RED,
449 .ShaderChannelSelectGreen = SCS_GREEN,
450 .ShaderChannelSelectBlue = SCS_BLUE,
451 .ShaderChannelSelectAlpha = SCS_ALPHA,
452 #endif
453 .SurfaceBaseAddress = info->address,
454 };
455
456 GENX(RENDER_SURFACE_STATE_pack)(NULL, state, &surface_state);
457 }