dawn-cmake/src/dawn_native/ShaderModule.h
Corentin Wallez 4f8bdaf473 Make ShaderModuleBase use an internal EntryPointMetadata
WGSL and SPIR-V modules can contain multiple entrypoints, for different
shader stages, that the pipelines can choose from. This is the first CL
in a stack that will change Dawn internals to not rely on ShaderModules
having a single entrypoint.

EntryPointMetadata is introduced that will contain all reflection data
for an entrypoint of a shader module. To ease review this CL doesn't
introduce any functional changes and doesn't expose the
EntryPointMetadata at the ShaderModuleBase interface. Instead
ShaderModuleBase contains a single metadata object for its single entry
point, and layout-related queries and proxied to the EntryPointMetadata
object.

Finally some small renames and formatting changes are done.

Bug: dawn:216
Change-Id: I0f4d12a5075ba14c5e8fd666be4073d34288f6f9
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/27240
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
2020-08-26 09:57:52 +00:00

135 lines
4.7 KiB
C++

// Copyright 2017 The Dawn Authors
//
// 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.
#ifndef DAWNNATIVE_SHADERMODULE_H_
#define DAWNNATIVE_SHADERMODULE_H_
#include "common/Constants.h"
#include "common/ityp_array.h"
#include "dawn_native/BindingInfo.h"
#include "dawn_native/CachedObject.h"
#include "dawn_native/Error.h"
#include "dawn_native/Format.h"
#include "dawn_native/Forward.h"
#include "dawn_native/PerStage.h"
#include "dawn_native/dawn_platform.h"
#include "spvc/spvc.hpp"
#include <bitset>
#include <map>
#include <vector>
namespace spirv_cross {
class Compiler;
}
namespace dawn_native {
MaybeError ValidateShaderModuleDescriptor(DeviceBase* device,
const ShaderModuleDescriptor* descriptor);
class ShaderModuleBase : public CachedObject {
public:
ShaderModuleBase(DeviceBase* device, const ShaderModuleDescriptor* descriptor);
~ShaderModuleBase() override;
static ShaderModuleBase* MakeError(DeviceBase* device);
MaybeError ExtractSpirvInfo(const spirv_cross::Compiler& compiler);
struct ShaderBindingInfo : BindingInfo {
// The SPIRV ID of the resource.
uint32_t id;
uint32_t base_type_id;
private:
// Disallow access to unused members.
using BindingInfo::hasDynamicOffset;
using BindingInfo::visibility;
};
using BindingInfoMap = std::map<BindingNumber, ShaderBindingInfo>;
using ModuleBindingInfo = ityp::array<BindGroupIndex, BindingInfoMap, kMaxBindGroups>;
const ModuleBindingInfo& GetBindingInfo() const;
const std::bitset<kMaxVertexAttributes>& GetUsedVertexAttributes() const;
SingleShaderStage GetExecutionModel() const;
// 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;
MaybeError ValidateCompatibilityWithPipelineLayout(const PipelineLayoutBase* layout) const;
RequiredBufferSizes ComputeRequiredBufferSizesForLayout(
const PipelineLayoutBase* layout) const;
// 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;
};
shaderc_spvc::Context* GetContext();
const std::vector<uint32_t>& GetSpirv() const;
#ifdef DAWN_ENABLE_WGSL
ResultOrError<std::vector<uint32_t>> GeneratePullingSpirv(
const VertexStateDescriptor& vertexState,
const std::string& entryPoint,
uint32_t pullingBufferBindingSet) const;
#endif
struct EntryPointMetadata {
EntryPointMetadata();
ModuleBindingInfo bindings;
std::bitset<kMaxVertexAttributes> usedVertexAttributes;
SingleShaderStage stage;
FragmentOutputBaseTypes fragmentOutputFormatBaseTypes;
};
protected:
static MaybeError CheckSpvcSuccess(shaderc_spvc_status status, const char* error_msg);
shaderc_spvc::CompileOptions GetCompileOptions() const;
MaybeError InitializeBase();
shaderc_spvc::Context mSpvcContext;
private:
ShaderModuleBase(DeviceBase* device, ObjectBase::ErrorTag tag);
// Different implementations reflection into the shader depending on
// whether using spvc, or directly accessing spirv-cross.
ResultOrError<std::unique_ptr<EntryPointMetadata>> ExtractSpirvInfoWithSpvc();
ResultOrError<std::unique_ptr<EntryPointMetadata>> ExtractSpirvInfoWithSpirvCross(
const spirv_cross::Compiler& compiler);
enum class Type { Undefined, Spirv, Wgsl };
Type mType;
std::vector<uint32_t> mSpirv;
std::string mWgsl;
std::unique_ptr<EntryPointMetadata> mMainEntryPoint;
};
} // namespace dawn_native
#endif // DAWNNATIVE_SHADERMODULE_H_