blob: ea75eae794ad16de82d167560d571ecea66fc666 [file] [log] [blame]
Alexis Hetu259ad3d2018-11-15 13:44:31 -05001// Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "VkShaderModule.hpp"
Nicolas Capens318d5e02018-11-16 14:28:36 -050016
Nicolas Capens45510ad2020-06-04 22:26:40 -040017#include "spirv-tools/libspirv.hpp"
18
Nicolas Capens318d5e02018-11-16 14:28:36 -050019#include <cstring>
Alexis Hetu259ad3d2018-11-15 13:44:31 -050020
Nicolas Capens157ba262019-12-10 17:49:14 -050021namespace vk {
Alexis Hetu259ad3d2018-11-15 13:44:31 -050022
Ben Clayton2ed93ab2019-12-17 20:38:03 +000023ShaderModule::ShaderModule(const VkShaderModuleCreateInfo *pCreateInfo, void *mem)
Nicolas Capensce9f17f2021-10-15 17:05:10 -040024 : binary(pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t))
Alexis Hetu259ad3d2018-11-15 13:44:31 -050025{
Nicolas Capens45510ad2020-06-04 22:26:40 -040026#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
Sean Risser019feda2020-11-09 14:19:27 -050027 spvtools::SpirvTools spirvTools(SPIRV_VERSION);
Alexis Hetu09ed4582022-02-23 14:24:50 -050028 spirvTools.SetMessageConsumer([](spv_message_level_t level, const char *source, const spv_position_t &position, const char *message) {
Mikael Pessa1a74e1c2021-12-06 09:52:03 -080029 switch(level)
30 {
31 case SPV_MSG_FATAL: sw::warn("SPIR-V FATAL: %d:%d %s\n", int(position.line), int(position.column), message);
32 case SPV_MSG_INTERNAL_ERROR: sw::warn("SPIR-V INTERNAL_ERROR: %d:%d %s\n", int(position.line), int(position.column), message);
33 case SPV_MSG_ERROR: sw::warn("SPIR-V ERROR: %d:%d %s\n", int(position.line), int(position.column), message);
34 case SPV_MSG_WARNING: sw::warn("SPIR-V WARNING: %d:%d %s\n", int(position.line), int(position.column), message);
35 case SPV_MSG_INFO: sw::trace("SPIR-V INFO: %d:%d %s\n", int(position.line), int(position.column), message);
36 case SPV_MSG_DEBUG: sw::trace("SPIR-V DEBUG: %d:%d %s\n", int(position.line), int(position.column), message);
37 default: sw::trace("SPIR-V MESSAGE: %d:%d %s\n", int(position.line), int(position.column), message);
38 }
39 });
40
Sean Risser236d75a2020-12-11 12:58:11 -050041 spvtools::ValidatorOptions validatorOptions = {};
42 validatorOptions.SetScalarBlockLayout(true); // VK_EXT_scalar_block_layout
43 validatorOptions.SetUniformBufferStandardLayout(true); // VK_KHR_uniform_buffer_standard_layout
Alexis Hetu09ed4582022-02-23 14:24:50 -050044 validatorOptions.SetAllowLocalSizeId(true); // VK_KHR_maintenance4
Nicolas Capens4f907502020-12-10 00:24:52 -050045
Nicolas Capens14fca802021-10-15 15:30:24 -040046 ASSERT(spirvTools.Validate(binary.data(), binary.size(), validatorOptions)); // The SPIR-V code passed to vkCreateShaderModule must be valid (b/158228522)
Nicolas Capens45510ad2020-06-04 22:26:40 -040047#endif
Alexis Hetu259ad3d2018-11-15 13:44:31 -050048}
49
Ben Clayton2ed93ab2019-12-17 20:38:03 +000050size_t ShaderModule::ComputeRequiredAllocationSize(const VkShaderModuleCreateInfo *pCreateInfo)
Alexis Hetu259ad3d2018-11-15 13:44:31 -050051{
Nicolas Capens14fca802021-10-15 15:30:24 -040052 return 0;
Alexis Hetu259ad3d2018-11-15 13:44:31 -050053}
54
Nicolas Capens157ba262019-12-10 17:49:14 -050055} // namespace vk