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 VkResult
genX(init_device_state)37 genX(init_device_state)(struct anv_device *device)
38 {
39 GENX(MEMORY_OBJECT_CONTROL_STATE_pack)(NULL, &device->default_mocs,
40 &GENX(MOCS));
41
42 struct anv_batch batch;
43
44 uint32_t cmds[64];
45 batch.start = batch.next = cmds;
46 batch.end = (void *) cmds + sizeof(cmds);
47
48 anv_batch_emit(&batch, GENX(PIPELINE_SELECT), ps) {
49 #if GEN_GEN >= 9
50 ps.MaskBits = 3;
51 #endif
52 ps.PipelineSelection = _3D;
53 }
54
55 anv_batch_emit(&batch, GENX(3DSTATE_VF_STATISTICS), vfs)
56 vfs.StatisticsEnable = true;
57
58 anv_batch_emit(&batch, GENX(3DSTATE_AA_LINE_PARAMETERS), aa);
59
60 anv_batch_emit(&batch, GENX(3DSTATE_DRAWING_RECTANGLE), rect) {
61 rect.ClippedDrawingRectangleYMin = 0;
62 rect.ClippedDrawingRectangleXMin = 0;
63 rect.ClippedDrawingRectangleYMax = UINT16_MAX;
64 rect.ClippedDrawingRectangleXMax = UINT16_MAX;
65 rect.DrawingRectangleOriginY = 0;
66 rect.DrawingRectangleOriginX = 0;
67 }
68
69 #if GEN_GEN >= 8
70 anv_batch_emit(&batch, GENX(3DSTATE_WM_CHROMAKEY), ck);
71
72 /* See the Vulkan 1.0 spec Table 24.1 "Standard sample locations" and
73 * VkPhysicalDeviceFeatures::standardSampleLocations.
74 */
75 anv_batch_emit(&batch, GENX(3DSTATE_SAMPLE_PATTERN), sp) {
76 GEN_SAMPLE_POS_1X(sp._1xSample);
77 GEN_SAMPLE_POS_2X(sp._2xSample);
78 GEN_SAMPLE_POS_4X(sp._4xSample);
79 GEN_SAMPLE_POS_8X(sp._8xSample);
80 #if GEN_GEN >= 9
81 GEN_SAMPLE_POS_16X(sp._16xSample);
82 #endif
83 }
84 #endif
85
86 anv_batch_emit(&batch, GENX(MI_BATCH_BUFFER_END), bbe);
87
88 assert(batch.next <= batch.end);
89
90 return anv_device_submit_simple_batch(device, &batch);
91 }
92
93 static inline uint32_t
vk_to_gen_tex_filter(VkFilter filter,bool anisotropyEnable)94 vk_to_gen_tex_filter(VkFilter filter, bool anisotropyEnable)
95 {
96 switch (filter) {
97 default:
98 assert(!"Invalid filter");
99 case VK_FILTER_NEAREST:
100 return anisotropyEnable ? MAPFILTER_ANISOTROPIC : MAPFILTER_NEAREST;
101 case VK_FILTER_LINEAR:
102 return anisotropyEnable ? MAPFILTER_ANISOTROPIC : MAPFILTER_LINEAR;
103 }
104 }
105
106 static inline uint32_t
vk_to_gen_max_anisotropy(float ratio)107 vk_to_gen_max_anisotropy(float ratio)
108 {
109 return (anv_clamp_f(ratio, 2, 16) - 2) / 2;
110 }
111
112 static const uint32_t vk_to_gen_mipmap_mode[] = {
113 [VK_SAMPLER_MIPMAP_MODE_NEAREST] = MIPFILTER_NEAREST,
114 [VK_SAMPLER_MIPMAP_MODE_LINEAR] = MIPFILTER_LINEAR
115 };
116
117 static const uint32_t vk_to_gen_tex_address[] = {
118 [VK_SAMPLER_ADDRESS_MODE_REPEAT] = TCM_WRAP,
119 [VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT] = TCM_MIRROR,
120 [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE] = TCM_CLAMP,
121 [VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE] = TCM_MIRROR_ONCE,
122 [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER] = TCM_CLAMP_BORDER,
123 };
124
125 /* Vulkan specifies the result of shadow comparisons as:
126 * 1 if ref <op> texel,
127 * 0 otherwise.
128 *
129 * The hardware does:
130 * 0 if texel <op> ref,
131 * 1 otherwise.
132 *
133 * So, these look a bit strange because there's both a negation
134 * and swapping of the arguments involved.
135 */
136 static const uint32_t vk_to_gen_shadow_compare_op[] = {
137 [VK_COMPARE_OP_NEVER] = PREFILTEROPALWAYS,
138 [VK_COMPARE_OP_LESS] = PREFILTEROPLEQUAL,
139 [VK_COMPARE_OP_EQUAL] = PREFILTEROPNOTEQUAL,
140 [VK_COMPARE_OP_LESS_OR_EQUAL] = PREFILTEROPLESS,
141 [VK_COMPARE_OP_GREATER] = PREFILTEROPGEQUAL,
142 [VK_COMPARE_OP_NOT_EQUAL] = PREFILTEROPEQUAL,
143 [VK_COMPARE_OP_GREATER_OR_EQUAL] = PREFILTEROPGREATER,
144 [VK_COMPARE_OP_ALWAYS] = PREFILTEROPNEVER,
145 };
146
genX(CreateSampler)147 VkResult genX(CreateSampler)(
148 VkDevice _device,
149 const VkSamplerCreateInfo* pCreateInfo,
150 const VkAllocationCallbacks* pAllocator,
151 VkSampler* pSampler)
152 {
153 ANV_FROM_HANDLE(anv_device, device, _device);
154 struct anv_sampler *sampler;
155
156 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
157
158 sampler = vk_alloc2(&device->alloc, pAllocator, sizeof(*sampler), 8,
159 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
160 if (!sampler)
161 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
162
163 uint32_t border_color_offset = device->border_colors.offset +
164 pCreateInfo->borderColor * 64;
165
166 bool enable_min_filter_addr_rounding =
167 pCreateInfo->minFilter != VK_FILTER_NEAREST;
168 bool enable_mag_filter_addr_rounding =
169 pCreateInfo->magFilter != VK_FILTER_NEAREST;
170
171 struct GENX(SAMPLER_STATE) sampler_state = {
172 .SamplerDisable = false,
173 .TextureBorderColorMode = DX10OGL,
174
175 #if GEN_GEN >= 8
176 .LODPreClampMode = CLAMP_MODE_OGL,
177 #else
178 .LODPreClampEnable = CLAMP_ENABLE_OGL,
179 #endif
180
181 #if GEN_GEN == 8
182 .BaseMipLevel = 0.0,
183 #endif
184 .MipModeFilter = vk_to_gen_mipmap_mode[pCreateInfo->mipmapMode],
185 .MagModeFilter = vk_to_gen_tex_filter(pCreateInfo->magFilter,
186 pCreateInfo->anisotropyEnable),
187 .MinModeFilter = vk_to_gen_tex_filter(pCreateInfo->minFilter,
188 pCreateInfo->anisotropyEnable),
189 .TextureLODBias = anv_clamp_f(pCreateInfo->mipLodBias, -16, 15.996),
190 .AnisotropicAlgorithm = EWAApproximation,
191 .MinLOD = anv_clamp_f(pCreateInfo->minLod, 0, 14),
192 .MaxLOD = anv_clamp_f(pCreateInfo->maxLod, 0, 14),
193 .ChromaKeyEnable = 0,
194 .ChromaKeyIndex = 0,
195 .ChromaKeyMode = 0,
196 .ShadowFunction = vk_to_gen_shadow_compare_op[pCreateInfo->compareOp],
197 .CubeSurfaceControlMode = OVERRIDE,
198
199 .BorderColorPointer = border_color_offset,
200
201 #if GEN_GEN >= 8
202 .LODClampMagnificationMode = MIPNONE,
203 #endif
204
205 .MaximumAnisotropy = vk_to_gen_max_anisotropy(pCreateInfo->maxAnisotropy),
206 .RAddressMinFilterRoundingEnable = enable_min_filter_addr_rounding,
207 .RAddressMagFilterRoundingEnable = enable_mag_filter_addr_rounding,
208 .VAddressMinFilterRoundingEnable = enable_min_filter_addr_rounding,
209 .VAddressMagFilterRoundingEnable = enable_mag_filter_addr_rounding,
210 .UAddressMinFilterRoundingEnable = enable_min_filter_addr_rounding,
211 .UAddressMagFilterRoundingEnable = enable_mag_filter_addr_rounding,
212 .TrilinearFilterQuality = 0,
213 .NonnormalizedCoordinateEnable = pCreateInfo->unnormalizedCoordinates,
214 .TCXAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressModeU],
215 .TCYAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressModeV],
216 .TCZAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressModeW],
217 };
218
219 GENX(SAMPLER_STATE_pack)(NULL, sampler->state, &sampler_state);
220
221 *pSampler = anv_sampler_to_handle(sampler);
222
223 return VK_SUCCESS;
224 }
225