dawn-cmake/src/dawn_native/Pipeline.cpp

122 lines
4.1 KiB
C++
Raw Normal View History

// Copyright 2017 The Dawn Authors
2017-05-31 00:03:44 +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/Pipeline.h"
2017-05-31 00:03:44 +00:00
2018-07-24 11:53:51 +00:00
#include "dawn_native/DepthStencilState.h"
#include "dawn_native/Device.h"
#include "dawn_native/InputState.h"
#include "dawn_native/PipelineLayout.h"
#include "dawn_native/ShaderModule.h"
2017-05-31 00:03:44 +00:00
namespace backend {
// PipelineBase
PipelineBase::PipelineBase(PipelineBuilder* builder)
2017-11-23 18:32:51 +00:00
: mStageMask(builder->mStageMask), mLayout(std::move(builder->mLayout)) {
if (!mLayout) {
2018-07-18 09:38:11 +00:00
dawn::PipelineLayoutDescriptor descriptor;
descriptor.numBindGroupLayouts = 0;
descriptor.bindGroupLayouts = nullptr;
mLayout = builder->GetParentBuilder()->GetDevice()->CreatePipelineLayout(&descriptor);
// Remove the external ref objects are created with
mLayout->Release();
2017-05-31 00:03:44 +00:00
}
auto FillPushConstants = [](const ShaderModuleBase* module, PushConstantInfo* info) {
const auto& moduleInfo = module->GetPushConstants();
info->mask = moduleInfo.mask;
for (uint32_t i = 0; i < moduleInfo.names.size(); i++) {
uint32_t size = moduleInfo.sizes[i];
2017-05-31 00:03:44 +00:00
if (size == 0) {
continue;
}
for (uint32_t offset = 0; offset < size; offset++) {
info->types[i + offset] = moduleInfo.types[i];
}
i += size - 1;
}
};
2017-11-23 18:32:51 +00:00
for (auto stageBit : IterateStages(builder->mStageMask)) {
if (!builder->mStages[stageBit].module->IsCompatibleWithPipelineLayout(mLayout.Get())) {
builder->GetParentBuilder()->HandleError("Stage not compatible with layout");
2017-05-31 00:03:44 +00:00
return;
}
2017-11-23 18:32:51 +00:00
FillPushConstants(builder->mStages[stageBit].module.Get(), &mPushConstants[stageBit]);
2017-05-31 00:03:44 +00:00
}
}
2017-11-24 18:59:42 +00:00
const PipelineBase::PushConstantInfo& PipelineBase::GetPushConstants(
2018-07-18 09:38:11 +00:00
dawn::ShaderStage stage) const {
2017-11-23 18:32:51 +00:00
return mPushConstants[stage];
2017-05-31 00:03:44 +00:00
}
2018-07-18 09:38:11 +00:00
dawn::ShaderStageBit PipelineBase::GetStageMask() const {
2017-11-23 18:32:51 +00:00
return mStageMask;
2017-05-31 00:03:44 +00:00
}
PipelineLayoutBase* PipelineBase::GetLayout() {
2017-11-23 18:32:51 +00:00
return mLayout.Get();
2017-05-31 00:03:44 +00:00
}
// PipelineBuilder
PipelineBuilder::PipelineBuilder(BuilderBase* parentBuilder)
2018-07-18 09:38:11 +00:00
: mParentBuilder(parentBuilder), mStageMask(static_cast<dawn::ShaderStageBit>(0)) {
2017-05-31 00:03:44 +00:00
}
2018-07-18 09:38:11 +00:00
const PipelineBuilder::StageInfo& PipelineBuilder::GetStageInfo(dawn::ShaderStage stage) const {
2017-11-23 18:32:51 +00:00
ASSERT(mStageMask & StageBit(stage));
return mStages[stage];
2017-05-31 00:03:44 +00:00
}
BuilderBase* PipelineBuilder::GetParentBuilder() const {
2017-11-23 18:32:51 +00:00
return mParentBuilder;
}
2017-05-31 00:03:44 +00:00
void PipelineBuilder::SetLayout(PipelineLayoutBase* layout) {
2017-11-23 18:32:51 +00:00
mLayout = layout;
2017-05-31 00:03:44 +00:00
}
2018-07-18 09:38:11 +00:00
void PipelineBuilder::SetStage(dawn::ShaderStage stage,
2017-11-24 18:59:42 +00:00
ShaderModuleBase* module,
const char* entryPoint) {
2017-05-31 00:03:44 +00:00
if (entryPoint != std::string("main")) {
2017-11-23 18:32:51 +00:00
mParentBuilder->HandleError("Currently the entry point has to be main()");
2017-05-31 00:03:44 +00:00
return;
}
if (stage != module->GetExecutionModel()) {
2017-11-23 18:32:51 +00:00
mParentBuilder->HandleError("Setting module with wrong execution model");
2017-05-31 00:03:44 +00:00
return;
}
2018-07-18 09:38:11 +00:00
dawn::ShaderStageBit bit = StageBit(stage);
2017-11-23 18:32:51 +00:00
if (mStageMask & bit) {
mParentBuilder->HandleError("Setting already set stage");
2017-05-31 00:03:44 +00:00
return;
}
2017-11-23 18:32:51 +00:00
mStageMask |= bit;
2017-05-31 00:03:44 +00:00
2017-11-23 18:32:51 +00:00
mStages[stage].module = module;
mStages[stage].entryPoint = entryPoint;
2017-05-31 00:03:44 +00:00
}
2017-11-24 18:59:42 +00:00
} // namespace backend