intel/blorp: Make the MOCS setting part of blorp_address
[mesa.git] / src / intel / vulkan / genX_state.c
1 /*
2 * Copyright © 2015 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 <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 #include "common/gen_sample_positions.h"
33 #include "genxml/gen_macros.h"
34 #include "genxml/genX_pack.h"
35
36 #include "vk_util.h"
37
38 VkResult
39 genX(init_device_state)(struct anv_device *device)
40 {
41 GENX(MEMORY_OBJECT_CONTROL_STATE_pack)(NULL, &device->default_mocs,
42 &GENX(MOCS));
43
44 struct anv_batch batch;
45
46 uint32_t cmds[64];
47 batch.start = batch.next = cmds;
48 batch.end = (void *) cmds + sizeof(cmds);
49
50 anv_batch_emit(&batch, GENX(PIPELINE_SELECT), ps) {
51 #if GEN_GEN >= 9
52 ps.MaskBits = 3;
53 #endif
54 ps.PipelineSelection = _3D;
55 }
56
57 #if GEN_GEN == 9
58 uint32_t cache_mode_1;
59 anv_pack_struct(&cache_mode_1, GENX(CACHE_MODE_1),
60 .FloatBlendOptimizationEnable = true,
61 .FloatBlendOptimizationEnableMask = true,
62 .PartialResolveDisableInVC = true,
63 .PartialResolveDisableInVCMask = true);
64
65 anv_batch_emit(&batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
66 lri.RegisterOffset = GENX(CACHE_MODE_1_num);
67 lri.DataDWord = cache_mode_1;
68 }
69 #endif
70
71 anv_batch_emit(&batch, GENX(3DSTATE_AA_LINE_PARAMETERS), aa);
72
73 anv_batch_emit(&batch, GENX(3DSTATE_DRAWING_RECTANGLE), rect) {
74 rect.ClippedDrawingRectangleYMin = 0;
75 rect.ClippedDrawingRectangleXMin = 0;
76 rect.ClippedDrawingRectangleYMax = UINT16_MAX;
77 rect.ClippedDrawingRectangleXMax = UINT16_MAX;
78 rect.DrawingRectangleOriginY = 0;
79 rect.DrawingRectangleOriginX = 0;
80 }
81
82 #if GEN_GEN >= 8
83 anv_batch_emit(&batch, GENX(3DSTATE_WM_CHROMAKEY), ck);
84
85 /* See the Vulkan 1.0 spec Table 24.1 "Standard sample locations" and
86 * VkPhysicalDeviceFeatures::standardSampleLocations.
87 */
88 anv_batch_emit(&batch, GENX(3DSTATE_SAMPLE_PATTERN), sp) {
89 GEN_SAMPLE_POS_1X(sp._1xSample);
90 GEN_SAMPLE_POS_2X(sp._2xSample);
91 GEN_SAMPLE_POS_4X(sp._4xSample);
92 GEN_SAMPLE_POS_8X(sp._8xSample);
93 #if GEN_GEN >= 9
94 GEN_SAMPLE_POS_16X(sp._16xSample);
95 #endif
96 }
97 #endif
98
99 anv_batch_emit(&batch, GENX(MI_BATCH_BUFFER_END), bbe);
100
101 assert(batch.next <= batch.end);
102
103 return anv_device_submit_simple_batch(device, &batch);
104 }
105
106 static uint32_t
107 vk_to_gen_tex_filter(VkFilter filter, bool anisotropyEnable)
108 {
109 switch (filter) {
110 default:
111 assert(!"Invalid filter");
112 case VK_FILTER_NEAREST:
113 return anisotropyEnable ? MAPFILTER_ANISOTROPIC : MAPFILTER_NEAREST;
114 case VK_FILTER_LINEAR:
115 return anisotropyEnable ? MAPFILTER_ANISOTROPIC : MAPFILTER_LINEAR;
116 }
117 }
118
119 static uint32_t
120 vk_to_gen_max_anisotropy(float ratio)
121 {
122 return (anv_clamp_f(ratio, 2, 16) - 2) / 2;
123 }
124
125 static const uint32_t vk_to_gen_mipmap_mode[] = {
126 [VK_SAMPLER_MIPMAP_MODE_NEAREST] = MIPFILTER_NEAREST,
127 [VK_SAMPLER_MIPMAP_MODE_LINEAR] = MIPFILTER_LINEAR
128 };
129
130 static const uint32_t vk_to_gen_tex_address[] = {
131 [VK_SAMPLER_ADDRESS_MODE_REPEAT] = TCM_WRAP,
132 [VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT] = TCM_MIRROR,
133 [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE] = TCM_CLAMP,
134 [VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE] = TCM_MIRROR_ONCE,
135 [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER] = TCM_CLAMP_BORDER,
136 };
137
138 /* Vulkan specifies the result of shadow comparisons as:
139 * 1 if ref <op> texel,
140 * 0 otherwise.
141 *
142 * The hardware does:
143 * 0 if texel <op> ref,
144 * 1 otherwise.
145 *
146 * So, these look a bit strange because there's both a negation
147 * and swapping of the arguments involved.
148 */
149 static const uint32_t vk_to_gen_shadow_compare_op[] = {
150 [VK_COMPARE_OP_NEVER] = PREFILTEROPALWAYS,
151 [VK_COMPARE_OP_LESS] = PREFILTEROPLEQUAL,
152 [VK_COMPARE_OP_EQUAL] = PREFILTEROPNOTEQUAL,
153 [VK_COMPARE_OP_LESS_OR_EQUAL] = PREFILTEROPLESS,
154 [VK_COMPARE_OP_GREATER] = PREFILTEROPGEQUAL,
155 [VK_COMPARE_OP_NOT_EQUAL] = PREFILTEROPEQUAL,
156 [VK_COMPARE_OP_GREATER_OR_EQUAL] = PREFILTEROPGREATER,
157 [VK_COMPARE_OP_ALWAYS] = PREFILTEROPNEVER,
158 };
159
160 VkResult genX(CreateSampler)(
161 VkDevice _device,
162 const VkSamplerCreateInfo* pCreateInfo,
163 const VkAllocationCallbacks* pAllocator,
164 VkSampler* pSampler)
165 {
166 ANV_FROM_HANDLE(anv_device, device, _device);
167 struct anv_sampler *sampler;
168
169 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
170
171 sampler = vk_zalloc2(&device->alloc, pAllocator, sizeof(*sampler), 8,
172 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
173 if (!sampler)
174 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
175
176 sampler->n_planes = 1;
177
178 uint32_t border_color_offset = device->border_colors.offset +
179 pCreateInfo->borderColor * 64;
180
181 vk_foreach_struct(ext, pCreateInfo->pNext) {
182 switch (ext->sType) {
183 case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO_KHR: {
184 VkSamplerYcbcrConversionInfoKHR *pSamplerConversion =
185 (VkSamplerYcbcrConversionInfoKHR *) ext;
186 ANV_FROM_HANDLE(anv_ycbcr_conversion, conversion,
187 pSamplerConversion->conversion);
188
189 if (conversion == NULL)
190 break;
191
192 sampler->n_planes = conversion->format->n_planes;
193 sampler->conversion = conversion;
194 break;
195 }
196 default:
197 anv_debug_ignored_stype(ext->sType);
198 break;
199 }
200 }
201
202 for (unsigned p = 0; p < sampler->n_planes; p++) {
203 const bool plane_has_chroma =
204 sampler->conversion && sampler->conversion->format->planes[p].has_chroma;
205 const VkFilter min_filter =
206 plane_has_chroma ? sampler->conversion->chroma_filter : pCreateInfo->minFilter;
207 const VkFilter mag_filter =
208 plane_has_chroma ? sampler->conversion->chroma_filter : pCreateInfo->magFilter;
209 const bool enable_min_filter_addr_rounding = min_filter != VK_FILTER_NEAREST;
210 const bool enable_mag_filter_addr_rounding = mag_filter != VK_FILTER_NEAREST;
211 /* From Broadwell PRM, SAMPLER_STATE:
212 * "Mip Mode Filter must be set to MIPFILTER_NONE for Planar YUV surfaces."
213 */
214 const uint32_t mip_filter_mode =
215 (sampler->conversion &&
216 isl_format_is_yuv(sampler->conversion->format->planes[0].isl_format)) ?
217 MIPFILTER_NONE : vk_to_gen_mipmap_mode[pCreateInfo->mipmapMode];
218
219 struct GENX(SAMPLER_STATE) sampler_state = {
220 .SamplerDisable = false,
221 .TextureBorderColorMode = DX10OGL,
222
223 #if GEN_GEN >= 8
224 .LODPreClampMode = CLAMP_MODE_OGL,
225 #else
226 .LODPreClampEnable = CLAMP_ENABLE_OGL,
227 #endif
228
229 #if GEN_GEN == 8
230 .BaseMipLevel = 0.0,
231 #endif
232 .MipModeFilter = mip_filter_mode,
233 .MagModeFilter = vk_to_gen_tex_filter(mag_filter, pCreateInfo->anisotropyEnable),
234 .MinModeFilter = vk_to_gen_tex_filter(min_filter, pCreateInfo->anisotropyEnable),
235 .TextureLODBias = anv_clamp_f(pCreateInfo->mipLodBias, -16, 15.996),
236 .AnisotropicAlgorithm = EWAApproximation,
237 .MinLOD = anv_clamp_f(pCreateInfo->minLod, 0, 14),
238 .MaxLOD = anv_clamp_f(pCreateInfo->maxLod, 0, 14),
239 .ChromaKeyEnable = 0,
240 .ChromaKeyIndex = 0,
241 .ChromaKeyMode = 0,
242 .ShadowFunction = vk_to_gen_shadow_compare_op[pCreateInfo->compareOp],
243 .CubeSurfaceControlMode = OVERRIDE,
244
245 .BorderColorPointer = border_color_offset,
246
247 #if GEN_GEN >= 8
248 .LODClampMagnificationMode = MIPNONE,
249 #endif
250
251 .MaximumAnisotropy = vk_to_gen_max_anisotropy(pCreateInfo->maxAnisotropy),
252 .RAddressMinFilterRoundingEnable = enable_min_filter_addr_rounding,
253 .RAddressMagFilterRoundingEnable = enable_mag_filter_addr_rounding,
254 .VAddressMinFilterRoundingEnable = enable_min_filter_addr_rounding,
255 .VAddressMagFilterRoundingEnable = enable_mag_filter_addr_rounding,
256 .UAddressMinFilterRoundingEnable = enable_min_filter_addr_rounding,
257 .UAddressMagFilterRoundingEnable = enable_mag_filter_addr_rounding,
258 .TrilinearFilterQuality = 0,
259 .NonnormalizedCoordinateEnable = pCreateInfo->unnormalizedCoordinates,
260 .TCXAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressModeU],
261 .TCYAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressModeV],
262 .TCZAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressModeW],
263 };
264
265 GENX(SAMPLER_STATE_pack)(NULL, sampler->state[p], &sampler_state);
266 }
267
268 *pSampler = anv_sampler_to_handle(sampler);
269
270 return VK_SUCCESS;
271 }