2 * Copyright © 2015 Intel Corporation
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:
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
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
30 #include "anv_private.h"
32 #include "common/gen_sample_positions.h"
33 #include "genxml/gen_macros.h"
34 #include "genxml/genX_pack.h"
37 genX(init_device_state
)(struct anv_device
*device
)
39 GENX(MEMORY_OBJECT_CONTROL_STATE_pack
)(NULL
, &device
->default_mocs
,
42 struct anv_batch batch
;
45 batch
.start
= batch
.next
= cmds
;
46 batch
.end
= (void *) cmds
+ sizeof(cmds
);
48 anv_batch_emit(&batch
, GENX(PIPELINE_SELECT
), ps
) {
52 ps
.PipelineSelection
= _3D
;
55 anv_batch_emit(&batch
, GENX(3DSTATE_VF_STATISTICS
), vfs
)
56 vfs
.StatisticsEnable
= true;
58 anv_batch_emit(&batch
, GENX(3DSTATE_AA_LINE_PARAMETERS
), aa
);
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;
70 anv_batch_emit(&batch
, GENX(3DSTATE_WM_CHROMAKEY
), ck
);
72 /* See the Vulkan 1.0 spec Table 24.1 "Standard sample locations" and
73 * VkPhysicalDeviceFeatures::standardSampleLocations.
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
);
81 GEN_SAMPLE_POS_16X(sp
._16xSample
);
86 anv_batch_emit(&batch
, GENX(MI_BATCH_BUFFER_END
), bbe
);
88 assert(batch
.next
<= batch
.end
);
90 return anv_device_submit_simple_batch(device
, &batch
);
93 static inline uint32_t
94 vk_to_gen_tex_filter(VkFilter filter
, bool anisotropyEnable
)
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
;
106 static inline uint32_t
107 vk_to_gen_max_anisotropy(float ratio
)
109 return (anv_clamp_f(ratio
, 2, 16) - 2) / 2;
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
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
,
125 /* Vulkan specifies the result of shadow comparisons as:
126 * 1 if ref <op> texel,
130 * 0 if texel <op> ref,
133 * So, these look a bit strange because there's both a negation
134 * and swapping of the arguments involved.
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
,
147 VkResult
genX(CreateSampler
)(
149 const VkSamplerCreateInfo
* pCreateInfo
,
150 const VkAllocationCallbacks
* pAllocator
,
153 ANV_FROM_HANDLE(anv_device
, device
, _device
);
154 struct anv_sampler
*sampler
;
156 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
158 sampler
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*sampler
), 8,
159 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
161 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
163 uint32_t border_color_offset
= device
->border_colors
.offset
+
164 pCreateInfo
->borderColor
* 64;
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
;
171 struct GENX(SAMPLER_STATE
) sampler_state
= {
172 .SamplerDisable
= false,
173 .TextureBorderColorMode
= DX10OGL
,
176 .LODPreClampMode
= CLAMP_MODE_OGL
,
178 .LODPreClampEnable
= CLAMP_ENABLE_OGL
,
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,
196 .ShadowFunction
= vk_to_gen_shadow_compare_op
[pCreateInfo
->compareOp
],
197 .CubeSurfaceControlMode
= OVERRIDE
,
199 .BorderColorPointer
= border_color_offset
,
202 .LODClampMagnificationMode
= MIPNONE
,
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
],
219 GENX(SAMPLER_STATE_pack
)(NULL
, sampler
->state
, &sampler_state
);
221 *pSampler
= anv_sampler_to_handle(sampler
);