2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/ShaderModule.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-05-01 13:27:07 +00:00
|
|
|
#include "common/HashUtils.h"
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/BindGroupLayout.h"
|
|
|
|
#include "dawn_native/Device.h"
|
|
|
|
#include "dawn_native/Pipeline.h"
|
|
|
|
#include "dawn_native/PipelineLayout.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-09-06 15:25:46 +00:00
|
|
|
#include <spirv-tools/libspirv.hpp>
|
2019-09-04 08:47:14 +00:00
|
|
|
#include <spirv_cross.hpp>
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-04-11 14:52:55 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
namespace dawn_native {
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-09-26 00:12:41 +00:00
|
|
|
namespace {
|
|
|
|
Format::Type SpirvCrossBaseTypeToFormatType(spirv_cross::SPIRType::BaseType spirvBaseType) {
|
|
|
|
switch (spirvBaseType) {
|
|
|
|
case spirv_cross::SPIRType::Float:
|
|
|
|
return Format::Float;
|
|
|
|
case spirv_cross::SPIRType::Int:
|
|
|
|
return Format::Sint;
|
|
|
|
case spirv_cross::SPIRType::UInt:
|
|
|
|
return Format::Uint;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return Format::Other;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2018-08-22 13:37:29 +00:00
|
|
|
MaybeError ValidateShaderModuleDescriptor(DeviceBase*,
|
|
|
|
const ShaderModuleDescriptor* descriptor) {
|
2018-09-10 14:17:24 +00:00
|
|
|
if (descriptor->nextInChain != nullptr) {
|
|
|
|
return DAWN_VALIDATION_ERROR("nextInChain must be nullptr");
|
|
|
|
}
|
2018-09-06 15:25:46 +00:00
|
|
|
|
2018-11-28 16:54:31 +00:00
|
|
|
spvtools::SpirvTools spirvTools(SPV_ENV_VULKAN_1_1);
|
2018-09-06 15:25:46 +00:00
|
|
|
|
|
|
|
std::ostringstream errorStream;
|
|
|
|
errorStream << "SPIRV Validation failure:" << std::endl;
|
|
|
|
|
|
|
|
spirvTools.SetMessageConsumer([&errorStream](spv_message_level_t level, const char*,
|
|
|
|
const spv_position_t& position,
|
|
|
|
const char* message) {
|
|
|
|
switch (level) {
|
|
|
|
case SPV_MSG_FATAL:
|
|
|
|
case SPV_MSG_INTERNAL_ERROR:
|
|
|
|
case SPV_MSG_ERROR:
|
|
|
|
errorStream << "error: line " << position.index << ": " << message << std::endl;
|
|
|
|
break;
|
|
|
|
case SPV_MSG_WARNING:
|
|
|
|
errorStream << "warning: line " << position.index << ": " << message
|
|
|
|
<< std::endl;
|
|
|
|
break;
|
|
|
|
case SPV_MSG_INFO:
|
|
|
|
errorStream << "info: line " << position.index << ": " << message << std::endl;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!spirvTools.Validate(descriptor->code, descriptor->codeSize)) {
|
2018-09-10 14:17:24 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(errorStream.str().c_str());
|
2018-09-06 15:25:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 15:01:20 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShaderModuleBase
|
|
|
|
|
2019-10-30 00:20:03 +00:00
|
|
|
ShaderModuleBase::ShaderModuleBase(DeviceBase* device, const ShaderModuleDescriptor* descriptor)
|
|
|
|
: CachedObject(device), mCode(descriptor->code, descriptor->code + descriptor->codeSize) {
|
2019-09-26 00:12:41 +00:00
|
|
|
mFragmentOutputFormatBaseTypes.fill(Format::Other);
|
2017-07-26 16:35:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 13:09:18 +00:00
|
|
|
ShaderModuleBase::ShaderModuleBase(DeviceBase* device, ObjectBase::ErrorTag tag)
|
2019-10-30 00:20:03 +00:00
|
|
|
: CachedObject(device, tag) {
|
2019-02-13 13:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 13:27:07 +00:00
|
|
|
ShaderModuleBase::~ShaderModuleBase() {
|
2019-10-30 00:20:03 +00:00
|
|
|
if (IsCachedReference()) {
|
2019-05-01 13:27:07 +00:00
|
|
|
GetDevice()->UncacheShaderModule(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 13:09:18 +00:00
|
|
|
// static
|
|
|
|
ShaderModuleBase* ShaderModuleBase::MakeError(DeviceBase* device) {
|
|
|
|
return new ShaderModuleBase(device, ObjectBase::kError);
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
void ShaderModuleBase::ExtractSpirvInfo(const spirv_cross::Compiler& compiler) {
|
2019-02-13 13:09:18 +00:00
|
|
|
ASSERT(!IsError());
|
|
|
|
|
2018-10-15 12:54:30 +00:00
|
|
|
DeviceBase* device = GetDevice();
|
2019-04-01 21:48:38 +00:00
|
|
|
// TODO(cwallez@chromium.org): make errors here creation errors
|
2017-06-05 20:23:18 +00:00
|
|
|
// currently errors here do not prevent the shadermodule from being used
|
2017-04-20 18:38:20 +00:00
|
|
|
const auto& resources = compiler.get_shader_resources();
|
|
|
|
|
|
|
|
switch (compiler.get_execution_model()) {
|
|
|
|
case spv::ExecutionModelVertex:
|
2019-08-27 08:42:29 +00:00
|
|
|
mExecutionModel = SingleShaderStage::Vertex;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
case spv::ExecutionModelFragment:
|
2019-08-27 08:42:29 +00:00
|
|
|
mExecutionModel = SingleShaderStage::Fragment;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
case spv::ExecutionModelGLCompute:
|
2019-08-27 08:42:29 +00:00
|
|
|
mExecutionModel = SingleShaderStage::Compute;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-07-11 01:48:12 +00:00
|
|
|
UNREACHABLE();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (resources.push_constant_buffers.size() > 0) {
|
2019-10-23 11:57:41 +00:00
|
|
|
GetDevice()->HandleError(wgpu::ErrorType::Validation,
|
2019-08-27 21:41:56 +00:00
|
|
|
"Push constants aren't supported.");
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in bindingInfo with the SPIRV bindings
|
2019-04-11 14:52:55 +00:00
|
|
|
auto ExtractResourcesBinding = [this](const spirv_cross::SmallVector<spirv_cross::Resource>&
|
|
|
|
resources,
|
2017-11-24 18:59:42 +00:00
|
|
|
const spirv_cross::Compiler& compiler,
|
2019-10-23 11:57:41 +00:00
|
|
|
wgpu::BindingType bindingType) {
|
2017-04-20 18:38:20 +00:00
|
|
|
for (const auto& resource : resources) {
|
2018-08-24 12:51:21 +00:00
|
|
|
ASSERT(compiler.get_decoration_bitset(resource.id).get(spv::DecorationBinding));
|
|
|
|
ASSERT(
|
|
|
|
compiler.get_decoration_bitset(resource.id).get(spv::DecorationDescriptorSet));
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
uint32_t binding = compiler.get_decoration(resource.id, spv::DecorationBinding);
|
|
|
|
uint32_t set = compiler.get_decoration(resource.id, spv::DecorationDescriptorSet);
|
|
|
|
|
|
|
|
if (binding >= kMaxBindingsPerGroup || set >= kMaxBindGroups) {
|
2019-10-23 11:57:41 +00:00
|
|
|
GetDevice()->HandleError(wgpu::ErrorType::Validation,
|
2019-08-27 21:41:56 +00:00
|
|
|
"Binding over limits in the SPIRV");
|
2017-04-20 18:38:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
auto& info = mBindingInfo[set][binding];
|
2017-04-20 18:38:20 +00:00
|
|
|
info.used = true;
|
|
|
|
info.id = resource.id;
|
|
|
|
info.base_type_id = resource.base_type_id;
|
2019-11-11 20:05:28 +00:00
|
|
|
switch (bindingType) {
|
|
|
|
case wgpu::BindingType::SampledTexture: {
|
|
|
|
spirv_cross::SPIRType::BaseType textureComponentType =
|
|
|
|
compiler.get_type(compiler.get_type(info.base_type_id).image.type)
|
|
|
|
.basetype;
|
|
|
|
info.textureComponentType =
|
|
|
|
SpirvCrossBaseTypeToFormatType(textureComponentType);
|
|
|
|
info.type = bindingType;
|
|
|
|
} break;
|
|
|
|
case wgpu::BindingType::StorageBuffer: {
|
|
|
|
// Differentiate between readonly storage bindings and writable ones based
|
|
|
|
// on the NonWritable decoration
|
|
|
|
spirv_cross::Bitset flags = compiler.get_buffer_block_flags(resource.id);
|
|
|
|
if (flags.get(spv::DecorationNonWritable)) {
|
|
|
|
info.type = wgpu::BindingType::ReadonlyStorageBuffer;
|
|
|
|
} else {
|
|
|
|
info.type = wgpu::BindingType::StorageBuffer;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
info.type = bindingType;
|
2019-10-31 09:51:11 +00:00
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
ExtractResourcesBinding(resources.uniform_buffers, compiler,
|
2019-10-23 11:57:41 +00:00
|
|
|
wgpu::BindingType::UniformBuffer);
|
2017-11-24 18:59:42 +00:00
|
|
|
ExtractResourcesBinding(resources.separate_images, compiler,
|
2019-10-23 11:57:41 +00:00
|
|
|
wgpu::BindingType::SampledTexture);
|
|
|
|
ExtractResourcesBinding(resources.separate_samplers, compiler, wgpu::BindingType::Sampler);
|
2017-11-24 18:59:42 +00:00
|
|
|
ExtractResourcesBinding(resources.storage_buffers, compiler,
|
2019-10-23 11:57:41 +00:00
|
|
|
wgpu::BindingType::StorageBuffer);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
// Extract the vertex attributes
|
2019-08-27 08:42:29 +00:00
|
|
|
if (mExecutionModel == SingleShaderStage::Vertex) {
|
2017-04-20 18:38:20 +00:00
|
|
|
for (const auto& attrib : resources.stage_inputs) {
|
2018-08-24 12:51:21 +00:00
|
|
|
ASSERT(compiler.get_decoration_bitset(attrib.id).get(spv::DecorationLocation));
|
2017-04-20 18:38:20 +00:00
|
|
|
uint32_t location = compiler.get_decoration(attrib.id, spv::DecorationLocation);
|
|
|
|
|
|
|
|
if (location >= kMaxVertexAttributes) {
|
2019-10-23 11:57:41 +00:00
|
|
|
device->HandleError(wgpu::ErrorType::Validation,
|
2019-08-27 21:41:56 +00:00
|
|
|
"Attribute location over limits in the SPIRV");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
mUsedVertexAttributes.set(location);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
// Without a location qualifier on vertex outputs, spirv_cross::CompilerMSL gives them
|
|
|
|
// all the location 0, causing a compile error.
|
2017-04-20 18:38:20 +00:00
|
|
|
for (const auto& attrib : resources.stage_outputs) {
|
2018-08-24 12:51:21 +00:00
|
|
|
if (!compiler.get_decoration_bitset(attrib.id).get(spv::DecorationLocation)) {
|
2019-10-23 11:57:41 +00:00
|
|
|
device->HandleError(wgpu::ErrorType::Validation,
|
2019-08-27 21:41:56 +00:00
|
|
|
"Need location qualifier on vertex output");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 08:42:29 +00:00
|
|
|
if (mExecutionModel == SingleShaderStage::Fragment) {
|
2017-11-24 18:59:42 +00:00
|
|
|
// Without a location qualifier on vertex inputs, spirv_cross::CompilerMSL gives them
|
|
|
|
// all the location 0, causing a compile error.
|
2017-04-20 18:38:20 +00:00
|
|
|
for (const auto& attrib : resources.stage_inputs) {
|
2018-08-24 12:51:21 +00:00
|
|
|
if (!compiler.get_decoration_bitset(attrib.id).get(spv::DecorationLocation)) {
|
2019-10-23 11:57:41 +00:00
|
|
|
device->HandleError(wgpu::ErrorType::Validation,
|
2019-08-27 21:41:56 +00:00
|
|
|
"Need location qualifier on fragment input");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 01:21:28 +00:00
|
|
|
|
|
|
|
for (const auto& fragmentOutput : resources.stage_outputs) {
|
|
|
|
ASSERT(
|
|
|
|
compiler.get_decoration_bitset(fragmentOutput.id).get(spv::DecorationLocation));
|
|
|
|
uint32_t location =
|
|
|
|
compiler.get_decoration(fragmentOutput.id, spv::DecorationLocation);
|
|
|
|
if (location >= kMaxColorAttachments) {
|
2019-10-23 11:57:41 +00:00
|
|
|
device->HandleError(wgpu::ErrorType::Validation,
|
2019-09-25 01:21:28 +00:00
|
|
|
"Fragment output location over limits in the SPIRV");
|
|
|
|
return;
|
|
|
|
}
|
2019-09-26 00:12:41 +00:00
|
|
|
|
|
|
|
spirv_cross::SPIRType::BaseType shaderFragmentOutputBaseType =
|
|
|
|
compiler.get_type(fragmentOutput.base_type_id).basetype;
|
|
|
|
Format::Type formatType =
|
|
|
|
SpirvCrossBaseTypeToFormatType(shaderFragmentOutputBaseType);
|
|
|
|
ASSERT(formatType != Format::Type::Other);
|
|
|
|
mFragmentOutputFormatBaseTypes[location] = formatType;
|
2019-09-25 01:21:28 +00:00
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ShaderModuleBase::ModuleBindingInfo& ShaderModuleBase::GetBindingInfo() const {
|
2019-02-13 13:09:18 +00:00
|
|
|
ASSERT(!IsError());
|
2017-11-23 18:32:51 +00:00
|
|
|
return mBindingInfo;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::bitset<kMaxVertexAttributes>& ShaderModuleBase::GetUsedVertexAttributes() const {
|
2019-02-13 13:09:18 +00:00
|
|
|
ASSERT(!IsError());
|
2017-11-23 18:32:51 +00:00
|
|
|
return mUsedVertexAttributes;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 00:12:41 +00:00
|
|
|
const ShaderModuleBase::FragmentOutputBaseTypes& ShaderModuleBase::GetFragmentOutputBaseTypes()
|
|
|
|
const {
|
|
|
|
ASSERT(!IsError());
|
|
|
|
return mFragmentOutputFormatBaseTypes;
|
|
|
|
}
|
|
|
|
|
2019-08-27 08:42:29 +00:00
|
|
|
SingleShaderStage ShaderModuleBase::GetExecutionModel() const {
|
2019-02-13 13:09:18 +00:00
|
|
|
ASSERT(!IsError());
|
2017-11-23 18:32:51 +00:00
|
|
|
return mExecutionModel;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderModuleBase::IsCompatibleWithPipelineLayout(const PipelineLayoutBase* layout) {
|
2019-02-13 13:09:18 +00:00
|
|
|
ASSERT(!IsError());
|
|
|
|
|
2019-02-11 23:35:33 +00:00
|
|
|
for (uint32_t group : IterateBitSet(layout->GetBindGroupLayoutsMask())) {
|
2017-04-20 18:38:20 +00:00
|
|
|
if (!IsCompatibleWithBindGroupLayout(group, layout->GetBindGroupLayout(group))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-02-11 23:35:33 +00:00
|
|
|
|
|
|
|
for (uint32_t group : IterateBitSet(~layout->GetBindGroupLayoutsMask())) {
|
|
|
|
for (size_t i = 0; i < kMaxBindingsPerGroup; ++i) {
|
|
|
|
if (mBindingInfo[group][i].used) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
bool ShaderModuleBase::IsCompatibleWithBindGroupLayout(size_t group,
|
|
|
|
const BindGroupLayoutBase* layout) {
|
2019-02-13 13:09:18 +00:00
|
|
|
ASSERT(!IsError());
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
const auto& layoutInfo = layout->GetBindingInfo();
|
|
|
|
for (size_t i = 0; i < kMaxBindingsPerGroup; ++i) {
|
2017-11-23 18:32:51 +00:00
|
|
|
const auto& moduleInfo = mBindingInfo[group][i];
|
2019-05-17 02:05:37 +00:00
|
|
|
const auto& layoutBindingType = layoutInfo.types[i];
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
if (!moduleInfo.used) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-09 07:58:57 +00:00
|
|
|
if (layoutBindingType != moduleInfo.type) {
|
2019-11-11 20:05:28 +00:00
|
|
|
// Binding mismatch between shader and bind group is invalid. For example, a
|
|
|
|
// writable binding in the shader with a readonly storage buffer in the bind group
|
|
|
|
// layout is invalid. However, a readonly binding in the shader with a writable
|
|
|
|
// storage buffer in the bind group layout is valid.
|
|
|
|
bool validBindingConversion =
|
|
|
|
layoutBindingType == wgpu::BindingType::StorageBuffer &&
|
|
|
|
moduleInfo.type == wgpu::BindingType::ReadonlyStorageBuffer;
|
|
|
|
if (!validBindingConversion) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2019-05-17 02:05:37 +00:00
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
if ((layoutInfo.visibilities[i] & StageBit(mExecutionModel)) == 0) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-31 09:51:11 +00:00
|
|
|
|
|
|
|
if (layoutBindingType == wgpu::BindingType::SampledTexture) {
|
|
|
|
Format::Type layoutTextureComponentType =
|
|
|
|
Format::TextureComponentTypeToFormatType(layoutInfo.textureComponentTypes[i]);
|
|
|
|
if (layoutTextureComponentType != moduleInfo.textureComponentType) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-01 13:27:07 +00:00
|
|
|
size_t ShaderModuleBase::HashFunc::operator()(const ShaderModuleBase* module) const {
|
|
|
|
size_t hash = 0;
|
|
|
|
|
|
|
|
for (uint32_t word : module->mCode) {
|
|
|
|
HashCombine(&hash, word);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderModuleBase::EqualityFunc::operator()(const ShaderModuleBase* a,
|
|
|
|
const ShaderModuleBase* b) const {
|
|
|
|
return a->mCode == b->mCode;
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
} // namespace dawn_native
|