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 14:42:33 +00:00
|
|
|
#ifndef DAWNNATIVE_SHADERMODULE_H_
|
|
|
|
#define DAWNNATIVE_SHADERMODULE_H_
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-06 18:41:13 +00:00
|
|
|
#include "common/Constants.h"
|
2020-03-27 18:54:03 +00:00
|
|
|
#include "dawn_native/BindingInfo.h"
|
2019-10-30 00:20:03 +00:00
|
|
|
#include "dawn_native/CachedObject.h"
|
2018-08-20 15:01:20 +00:00
|
|
|
#include "dawn_native/Error.h"
|
2019-09-26 00:12:41 +00:00
|
|
|
#include "dawn_native/Format.h"
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/Forward.h"
|
2019-07-12 17:52:22 +00:00
|
|
|
#include "dawn_native/PerStage.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-25 15:03:23 +00:00
|
|
|
#include "dawn_native/dawn_platform.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-12-03 20:25:13 +00:00
|
|
|
#include "spvc/spvc.hpp"
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <array>
|
|
|
|
#include <bitset>
|
2020-03-20 21:56:30 +00:00
|
|
|
#include <map>
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace spirv_cross {
|
|
|
|
class Compiler;
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
namespace dawn_native {
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-08-20 15:01:20 +00:00
|
|
|
MaybeError ValidateShaderModuleDescriptor(DeviceBase* device,
|
|
|
|
const ShaderModuleDescriptor* descriptor);
|
|
|
|
|
2019-10-30 00:20:03 +00:00
|
|
|
class ShaderModuleBase : public CachedObject {
|
2017-11-24 18:59:42 +00:00
|
|
|
public:
|
2019-10-30 00:20:03 +00:00
|
|
|
ShaderModuleBase(DeviceBase* device, const ShaderModuleDescriptor* descriptor);
|
2019-05-01 13:27:07 +00:00
|
|
|
~ShaderModuleBase() override;
|
2017-11-24 18:59:42 +00:00
|
|
|
|
2019-02-13 13:09:18 +00:00
|
|
|
static ShaderModuleBase* MakeError(DeviceBase* device);
|
|
|
|
|
2020-01-15 14:56:22 +00:00
|
|
|
MaybeError ExtractSpirvInfo(const spirv_cross::Compiler& compiler);
|
2017-11-24 18:59:42 +00:00
|
|
|
|
2020-03-27 18:54:03 +00:00
|
|
|
struct ShaderBindingInfo : BindingInfo {
|
2017-11-24 18:59:42 +00:00
|
|
|
// The SPIRV ID of the resource.
|
|
|
|
uint32_t id;
|
|
|
|
uint32_t base_type_id;
|
2020-03-27 18:54:03 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Disallow access to unused members.
|
|
|
|
using BindingInfo::hasDynamicOffset;
|
|
|
|
using BindingInfo::visibility;
|
2017-11-24 18:59:42 +00:00
|
|
|
};
|
2020-03-27 18:54:03 +00:00
|
|
|
|
|
|
|
using ModuleBindingInfo =
|
|
|
|
std::array<std::map<BindingNumber, ShaderBindingInfo>, kMaxBindGroups>;
|
2017-11-24 18:59:42 +00:00
|
|
|
|
|
|
|
const ModuleBindingInfo& GetBindingInfo() const;
|
|
|
|
const std::bitset<kMaxVertexAttributes>& GetUsedVertexAttributes() const;
|
2019-08-27 08:42:29 +00:00
|
|
|
SingleShaderStage GetExecutionModel() const;
|
2017-11-24 18:59:42 +00:00
|
|
|
|
2019-09-26 00:12:41 +00:00
|
|
|
// An array to record the basic types (float, int and uint) of the fragment shader outputs
|
|
|
|
// or Format::Type::Other means the fragment shader output is unused.
|
|
|
|
using FragmentOutputBaseTypes = std::array<Format::Type, kMaxColorAttachments>;
|
|
|
|
const FragmentOutputBaseTypes& GetFragmentOutputBaseTypes() const;
|
|
|
|
|
2019-11-22 17:02:22 +00:00
|
|
|
bool IsCompatibleWithPipelineLayout(const PipelineLayoutBase* layout) const;
|
2017-11-24 18:59:42 +00:00
|
|
|
|
2019-05-01 13:27:07 +00:00
|
|
|
// Functors necessary for the unordered_set<ShaderModuleBase*>-based cache.
|
|
|
|
struct HashFunc {
|
|
|
|
size_t operator()(const ShaderModuleBase* module) const;
|
|
|
|
};
|
|
|
|
struct EqualityFunc {
|
|
|
|
bool operator()(const ShaderModuleBase* a, const ShaderModuleBase* b) const;
|
|
|
|
};
|
|
|
|
|
2020-02-06 17:26:46 +00:00
|
|
|
shaderc_spvc::Context* GetContext() {
|
|
|
|
return &mSpvcContext;
|
|
|
|
}
|
|
|
|
|
2019-12-03 20:25:13 +00:00
|
|
|
protected:
|
2020-01-15 16:58:23 +00:00
|
|
|
static MaybeError CheckSpvcSuccess(shaderc_spvc_status status, const char* error_msg);
|
2020-02-06 17:26:46 +00:00
|
|
|
shaderc_spvc::CompileOptions GetCompileOptions();
|
2020-01-15 16:58:23 +00:00
|
|
|
|
2019-12-03 20:25:13 +00:00
|
|
|
shaderc_spvc::Context mSpvcContext;
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
private:
|
2019-02-13 13:09:18 +00:00
|
|
|
ShaderModuleBase(DeviceBase* device, ObjectBase::ErrorTag tag);
|
|
|
|
|
2019-11-22 17:02:22 +00:00
|
|
|
bool IsCompatibleWithBindGroupLayout(size_t group, const BindGroupLayoutBase* layout) const;
|
2017-11-24 18:59:42 +00:00
|
|
|
|
2020-01-14 16:29:41 +00:00
|
|
|
// Different implementations reflection into the shader depending on
|
|
|
|
// whether using spvc, or directly accessing spirv-cross.
|
2020-01-15 14:56:22 +00:00
|
|
|
MaybeError ExtractSpirvInfoWithSpvc();
|
|
|
|
MaybeError ExtractSpirvInfoWithSpirvCross(const spirv_cross::Compiler& compiler);
|
2020-01-14 16:29:41 +00:00
|
|
|
|
2019-05-01 13:27:07 +00:00
|
|
|
// TODO(cwallez@chromium.org): The code is only stored for deduplication. We could maybe
|
|
|
|
// store a cryptographic hash of the code instead?
|
|
|
|
std::vector<uint32_t> mCode;
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
ModuleBindingInfo mBindingInfo;
|
|
|
|
std::bitset<kMaxVertexAttributes> mUsedVertexAttributes;
|
2019-08-27 08:42:29 +00:00
|
|
|
SingleShaderStage mExecutionModel;
|
2019-09-26 00:12:41 +00:00
|
|
|
|
|
|
|
FragmentOutputBaseTypes mFragmentOutputFormatBaseTypes;
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
} // namespace dawn_native
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-24 14:42:33 +00:00
|
|
|
#endif // DAWNNATIVE_SHADERMODULE_H_
|