Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

genX_state.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. #include <assert.h>
  24. #include <stdbool.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include "anv_private.h"
  29. #include "common/gen_sample_positions.h"
  30. #include "genxml/gen_macros.h"
  31. #include "genxml/genX_pack.h"
  32. #include "vk_util.h"
  33. #if GEN_GEN == 10
  34. /**
  35. * From Gen10 Workarounds page in h/w specs:
  36. * WaSampleOffsetIZ:
  37. * "Prior to the 3DSTATE_SAMPLE_PATTERN driver must ensure there are no
  38. * markers in the pipeline by programming a PIPE_CONTROL with stall."
  39. */
  40. static void
  41. gen10_emit_wa_cs_stall_flush(struct anv_batch *batch)
  42. {
  43. anv_batch_emit(batch, GENX(PIPE_CONTROL), pc) {
  44. pc.CommandStreamerStallEnable = true;
  45. pc.StallAtPixelScoreboard = true;
  46. }
  47. }
  48. /**
  49. * From Gen10 Workarounds page in h/w specs:
  50. * WaSampleOffsetIZ:_cs_stall_flush
  51. * "When 3DSTATE_SAMPLE_PATTERN is programmed, driver must then issue an
  52. * MI_LOAD_REGISTER_IMM command to an offset between 0x7000 and 0x7FFF(SVL)
  53. * after the command to ensure the state has been delivered prior to any
  54. * command causing a marker in the pipeline."
  55. */
  56. static void
  57. gen10_emit_wa_lri_to_cache_mode_zero(struct anv_batch *batch)
  58. {
  59. /* Before changing the value of CACHE_MODE_0 register, GFX pipeline must
  60. * be idle; i.e., full flush is required.
  61. */
  62. anv_batch_emit(batch, GENX(PIPE_CONTROL), pc) {
  63. pc.DepthCacheFlushEnable = true;
  64. pc.DCFlushEnable = true;
  65. pc.RenderTargetCacheFlushEnable = true;
  66. pc.InstructionCacheInvalidateEnable = true;
  67. pc.StateCacheInvalidationEnable = true;
  68. pc.TextureCacheInvalidationEnable = true;
  69. pc.VFCacheInvalidationEnable = true;
  70. pc.ConstantCacheInvalidationEnable =true;
  71. }
  72. /* Write to CACHE_MODE_0 (0x7000) */
  73. uint32_t cache_mode_0 = 0;
  74. anv_pack_struct(&cache_mode_0, GENX(CACHE_MODE_0));
  75. anv_batch_emit(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
  76. lri.RegisterOffset = GENX(CACHE_MODE_0_num);
  77. lri.DataDWord = cache_mode_0;
  78. }
  79. }
  80. #endif
  81. VkResult
  82. genX(init_device_state)(struct anv_device *device)
  83. {
  84. GENX(MEMORY_OBJECT_CONTROL_STATE_pack)(NULL, &device->default_mocs,
  85. &GENX(MOCS));
  86. #if GEN_GEN >= 8
  87. GENX(MEMORY_OBJECT_CONTROL_STATE_pack)(NULL, &device->external_mocs,
  88. &GENX(EXTERNAL_MOCS));
  89. #else
  90. device->external_mocs = device->default_mocs;
  91. #endif
  92. struct anv_batch batch;
  93. uint32_t cmds[64];
  94. batch.start = batch.next = cmds;
  95. batch.end = (void *) cmds + sizeof(cmds);
  96. anv_batch_emit(&batch, GENX(PIPELINE_SELECT), ps) {
  97. #if GEN_GEN >= 9
  98. ps.MaskBits = 3;
  99. #endif
  100. ps.PipelineSelection = _3D;
  101. }
  102. #if GEN_GEN == 9
  103. uint32_t cache_mode_1;
  104. anv_pack_struct(&cache_mode_1, GENX(CACHE_MODE_1),
  105. .FloatBlendOptimizationEnable = true,
  106. .FloatBlendOptimizationEnableMask = true,
  107. .PartialResolveDisableInVC = true,
  108. .PartialResolveDisableInVCMask = true);
  109. anv_batch_emit(&batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
  110. lri.RegisterOffset = GENX(CACHE_MODE_1_num);
  111. lri.DataDWord = cache_mode_1;
  112. }
  113. #endif
  114. anv_batch_emit(&batch, GENX(3DSTATE_AA_LINE_PARAMETERS), aa);
  115. anv_batch_emit(&batch, GENX(3DSTATE_DRAWING_RECTANGLE), rect) {
  116. rect.ClippedDrawingRectangleYMin = 0;
  117. rect.ClippedDrawingRectangleXMin = 0;
  118. rect.ClippedDrawingRectangleYMax = UINT16_MAX;
  119. rect.ClippedDrawingRectangleXMax = UINT16_MAX;
  120. rect.DrawingRectangleOriginY = 0;
  121. rect.DrawingRectangleOriginX = 0;
  122. }
  123. #if GEN_GEN >= 8
  124. anv_batch_emit(&batch, GENX(3DSTATE_WM_CHROMAKEY), ck);
  125. #if GEN_GEN == 10
  126. gen10_emit_wa_cs_stall_flush(&batch);
  127. #endif
  128. /* See the Vulkan 1.0 spec Table 24.1 "Standard sample locations" and
  129. * VkPhysicalDeviceFeatures::standardSampleLocations.
  130. */
  131. anv_batch_emit(&batch, GENX(3DSTATE_SAMPLE_PATTERN), sp) {
  132. GEN_SAMPLE_POS_1X(sp._1xSample);
  133. GEN_SAMPLE_POS_2X(sp._2xSample);
  134. GEN_SAMPLE_POS_4X(sp._4xSample);
  135. GEN_SAMPLE_POS_8X(sp._8xSample);
  136. #if GEN_GEN >= 9
  137. GEN_SAMPLE_POS_16X(sp._16xSample);
  138. #endif
  139. }
  140. #endif
  141. #if GEN_GEN == 10
  142. gen10_emit_wa_lri_to_cache_mode_zero(&batch);
  143. #endif
  144. #if GEN_GEN == 11
  145. /* The default behavior of bit 5 "Headerless Message for Pre-emptable
  146. * Contexts" in SAMPLER MODE register is set to 0, which means
  147. * headerless sampler messages are not allowed for pre-emptable
  148. * contexts. Set the bit 5 to 1 to allow them.
  149. */
  150. uint32_t sampler_mode;
  151. anv_pack_struct(&sampler_mode, GENX(SAMPLER_MODE),
  152. .HeaderlessMessageforPreemptableContexts = true,
  153. .HeaderlessMessageforPreemptableContextsMask = true);
  154. anv_batch_emit(&batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
  155. lri.RegisterOffset = GENX(SAMPLER_MODE_num);
  156. lri.DataDWord = sampler_mode;
  157. }
  158. /* Bit 1 "Enabled Texel Offset Precision Fix" must be set in
  159. * HALF_SLICE_CHICKEN7 register.
  160. */
  161. uint32_t half_slice_chicken7;
  162. anv_pack_struct(&half_slice_chicken7, GENX(HALF_SLICE_CHICKEN7),
  163. .EnabledTexelOffsetPrecisionFix = true,
  164. .EnabledTexelOffsetPrecisionFixMask = true);
  165. anv_batch_emit(&batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
  166. lri.RegisterOffset = GENX(HALF_SLICE_CHICKEN7_num);
  167. lri.DataDWord = half_slice_chicken7;
  168. }
  169. #endif
  170. /* Set the "CONSTANT_BUFFER Address Offset Disable" bit, so
  171. * 3DSTATE_CONSTANT_XS buffer 0 is an absolute address.
  172. *
  173. * This is only safe on kernels with context isolation support.
  174. */
  175. if (GEN_GEN >= 8 &&
  176. device->instance->physicalDevice.has_context_isolation) {
  177. UNUSED uint32_t tmp_reg;
  178. #if GEN_GEN >= 9
  179. anv_pack_struct(&tmp_reg, GENX(CS_DEBUG_MODE2),
  180. .CONSTANT_BUFFERAddressOffsetDisable = true,
  181. .CONSTANT_BUFFERAddressOffsetDisableMask = true);
  182. anv_batch_emit(&batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
  183. lri.RegisterOffset = GENX(CS_DEBUG_MODE2_num);
  184. lri.DataDWord = tmp_reg;
  185. }
  186. #elif GEN_GEN == 8
  187. anv_pack_struct(&tmp_reg, GENX(INSTPM),
  188. .CONSTANT_BUFFERAddressOffsetDisable = true,
  189. .CONSTANT_BUFFERAddressOffsetDisableMask = true);
  190. anv_batch_emit(&batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
  191. lri.RegisterOffset = GENX(INSTPM_num);
  192. lri.DataDWord = tmp_reg;
  193. }
  194. #endif
  195. }
  196. anv_batch_emit(&batch, GENX(MI_BATCH_BUFFER_END), bbe);
  197. assert(batch.next <= batch.end);
  198. return anv_device_submit_simple_batch(device, &batch);
  199. }
  200. static uint32_t
  201. vk_to_gen_tex_filter(VkFilter filter, bool anisotropyEnable)
  202. {
  203. switch (filter) {
  204. default:
  205. assert(!"Invalid filter");
  206. case VK_FILTER_NEAREST:
  207. return anisotropyEnable ? MAPFILTER_ANISOTROPIC : MAPFILTER_NEAREST;
  208. case VK_FILTER_LINEAR:
  209. return anisotropyEnable ? MAPFILTER_ANISOTROPIC : MAPFILTER_LINEAR;
  210. }
  211. }
  212. static uint32_t
  213. vk_to_gen_max_anisotropy(float ratio)
  214. {
  215. return (anv_clamp_f(ratio, 2, 16) - 2) / 2;
  216. }
  217. static const uint32_t vk_to_gen_mipmap_mode[] = {
  218. [VK_SAMPLER_MIPMAP_MODE_NEAREST] = MIPFILTER_NEAREST,
  219. [VK_SAMPLER_MIPMAP_MODE_LINEAR] = MIPFILTER_LINEAR
  220. };
  221. static const uint32_t vk_to_gen_tex_address[] = {
  222. [VK_SAMPLER_ADDRESS_MODE_REPEAT] = TCM_WRAP,
  223. [VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT] = TCM_MIRROR,
  224. [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE] = TCM_CLAMP,
  225. [VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE] = TCM_MIRROR_ONCE,
  226. [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER] = TCM_CLAMP_BORDER,
  227. };
  228. /* Vulkan specifies the result of shadow comparisons as:
  229. * 1 if ref <op> texel,
  230. * 0 otherwise.
  231. *
  232. * The hardware does:
  233. * 0 if texel <op> ref,
  234. * 1 otherwise.
  235. *
  236. * So, these look a bit strange because there's both a negation
  237. * and swapping of the arguments involved.
  238. */
  239. static const uint32_t vk_to_gen_shadow_compare_op[] = {
  240. [VK_COMPARE_OP_NEVER] = PREFILTEROPALWAYS,
  241. [VK_COMPARE_OP_LESS] = PREFILTEROPLEQUAL,
  242. [VK_COMPARE_OP_EQUAL] = PREFILTEROPNOTEQUAL,
  243. [VK_COMPARE_OP_LESS_OR_EQUAL] = PREFILTEROPLESS,
  244. [VK_COMPARE_OP_GREATER] = PREFILTEROPGEQUAL,
  245. [VK_COMPARE_OP_NOT_EQUAL] = PREFILTEROPEQUAL,
  246. [VK_COMPARE_OP_GREATER_OR_EQUAL] = PREFILTEROPGREATER,
  247. [VK_COMPARE_OP_ALWAYS] = PREFILTEROPNEVER,
  248. };
  249. #if GEN_GEN >= 9
  250. static const uint32_t vk_to_gen_sampler_reduction_mode[] = {
  251. [VK_SAMPLER_REDUCTION_MODE_WEIGHTED_AVERAGE_EXT] = STD_FILTER,
  252. [VK_SAMPLER_REDUCTION_MODE_MIN_EXT] = MINIMUM,
  253. [VK_SAMPLER_REDUCTION_MODE_MAX_EXT] = MAXIMUM,
  254. };
  255. #endif
  256. VkResult genX(CreateSampler)(
  257. VkDevice _device,
  258. const VkSamplerCreateInfo* pCreateInfo,
  259. const VkAllocationCallbacks* pAllocator,
  260. VkSampler* pSampler)
  261. {
  262. ANV_FROM_HANDLE(anv_device, device, _device);
  263. struct anv_sampler *sampler;
  264. assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
  265. sampler = vk_zalloc2(&device->alloc, pAllocator, sizeof(*sampler), 8,
  266. VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  267. if (!sampler)
  268. return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
  269. sampler->n_planes = 1;
  270. uint32_t border_color_offset = device->border_colors.offset +
  271. pCreateInfo->borderColor * 64;
  272. #if GEN_GEN >= 9
  273. unsigned sampler_reduction_mode = STD_FILTER;
  274. bool enable_sampler_reduction = false;
  275. #endif
  276. vk_foreach_struct(ext, pCreateInfo->pNext) {
  277. switch (ext->sType) {
  278. case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO: {
  279. VkSamplerYcbcrConversionInfo *pSamplerConversion =
  280. (VkSamplerYcbcrConversionInfo *) ext;
  281. ANV_FROM_HANDLE(anv_ycbcr_conversion, conversion,
  282. pSamplerConversion->conversion);
  283. if (conversion == NULL)
  284. break;
  285. sampler->n_planes = conversion->format->n_planes;
  286. sampler->conversion = conversion;
  287. break;
  288. }
  289. #if GEN_GEN >= 9
  290. case VK_STRUCTURE_TYPE_SAMPLER_REDUCTION_MODE_CREATE_INFO_EXT: {
  291. struct VkSamplerReductionModeCreateInfoEXT *sampler_reduction =
  292. (struct VkSamplerReductionModeCreateInfoEXT *) ext;
  293. sampler_reduction_mode =
  294. vk_to_gen_sampler_reduction_mode[sampler_reduction->reductionMode];
  295. enable_sampler_reduction = true;
  296. break;
  297. }
  298. #endif
  299. default:
  300. anv_debug_ignored_stype(ext->sType);
  301. break;
  302. }
  303. }
  304. for (unsigned p = 0; p < sampler->n_planes; p++) {
  305. const bool plane_has_chroma =
  306. sampler->conversion && sampler->conversion->format->planes[p].has_chroma;
  307. const VkFilter min_filter =
  308. plane_has_chroma ? sampler->conversion->chroma_filter : pCreateInfo->minFilter;
  309. const VkFilter mag_filter =
  310. plane_has_chroma ? sampler->conversion->chroma_filter : pCreateInfo->magFilter;
  311. const bool enable_min_filter_addr_rounding = min_filter != VK_FILTER_NEAREST;
  312. const bool enable_mag_filter_addr_rounding = mag_filter != VK_FILTER_NEAREST;
  313. /* From Broadwell PRM, SAMPLER_STATE:
  314. * "Mip Mode Filter must be set to MIPFILTER_NONE for Planar YUV surfaces."
  315. */
  316. const uint32_t mip_filter_mode =
  317. (sampler->conversion &&
  318. isl_format_is_yuv(sampler->conversion->format->planes[0].isl_format)) ?
  319. MIPFILTER_NONE : vk_to_gen_mipmap_mode[pCreateInfo->mipmapMode];
  320. struct GENX(SAMPLER_STATE) sampler_state = {
  321. .SamplerDisable = false,
  322. .TextureBorderColorMode = DX10OGL,
  323. #if GEN_GEN >= 8
  324. .LODPreClampMode = CLAMP_MODE_OGL,
  325. #else
  326. .LODPreClampEnable = CLAMP_ENABLE_OGL,
  327. #endif
  328. #if GEN_GEN == 8
  329. .BaseMipLevel = 0.0,
  330. #endif
  331. .MipModeFilter = mip_filter_mode,
  332. .MagModeFilter = vk_to_gen_tex_filter(mag_filter, pCreateInfo->anisotropyEnable),
  333. .MinModeFilter = vk_to_gen_tex_filter(min_filter, pCreateInfo->anisotropyEnable),
  334. .TextureLODBias = anv_clamp_f(pCreateInfo->mipLodBias, -16, 15.996),
  335. .AnisotropicAlgorithm = EWAApproximation,
  336. .MinLOD = anv_clamp_f(pCreateInfo->minLod, 0, 14),
  337. .MaxLOD = anv_clamp_f(pCreateInfo->maxLod, 0, 14),
  338. .ChromaKeyEnable = 0,
  339. .ChromaKeyIndex = 0,
  340. .ChromaKeyMode = 0,
  341. .ShadowFunction = vk_to_gen_shadow_compare_op[pCreateInfo->compareOp],
  342. .CubeSurfaceControlMode = OVERRIDE,
  343. .BorderColorPointer = border_color_offset,
  344. #if GEN_GEN >= 8
  345. .LODClampMagnificationMode = MIPNONE,
  346. #endif
  347. .MaximumAnisotropy = vk_to_gen_max_anisotropy(pCreateInfo->maxAnisotropy),
  348. .RAddressMinFilterRoundingEnable = enable_min_filter_addr_rounding,
  349. .RAddressMagFilterRoundingEnable = enable_mag_filter_addr_rounding,
  350. .VAddressMinFilterRoundingEnable = enable_min_filter_addr_rounding,
  351. .VAddressMagFilterRoundingEnable = enable_mag_filter_addr_rounding,
  352. .UAddressMinFilterRoundingEnable = enable_min_filter_addr_rounding,
  353. .UAddressMagFilterRoundingEnable = enable_mag_filter_addr_rounding,
  354. .TrilinearFilterQuality = 0,
  355. .NonnormalizedCoordinateEnable = pCreateInfo->unnormalizedCoordinates,
  356. .TCXAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressModeU],
  357. .TCYAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressModeV],
  358. .TCZAddressControlMode = vk_to_gen_tex_address[pCreateInfo->addressModeW],
  359. #if GEN_GEN >= 9
  360. .ReductionType = sampler_reduction_mode,
  361. .ReductionTypeEnable = enable_sampler_reduction,
  362. #endif
  363. };
  364. GENX(SAMPLER_STATE_pack)(NULL, sampler->state[p], &sampler_state);
  365. }
  366. *pSampler = anv_sampler_to_handle(sampler);
  367. return VK_SUCCESS;
  368. }