Hash :
c4274d54
Author :
Date :
2024-04-29T11:47:34
WebGPU: Add pipeline creation and caching. Bug: angleproject:342213825 Change-Id: I303f193d30fd6b9820efaefcae64e11042888009 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5497535 Reviewed-by: Liza Burakova <liza@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Geoff Lang <geofflang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
//
// Copyright 2024 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "libANGLE/renderer/wgpu/wgpu_pipeline_state.h"
#include "common/aligned_memory.h"
#include "common/hash_utils.h"
#include "libANGLE/Error.h"
#include "libANGLE/renderer/wgpu/ContextWgpu.h"
#include "libANGLE/renderer/wgpu/wgpu_utils.h"
namespace rx
{
namespace webgpu
{
// Can pack the index format into 1 bit since it has 2 values and Undefined is not used.
static_assert(static_cast<uint32_t>(wgpu::IndexFormat::Uint32) == 2U,
"Max wgpu::IndexFormat is not 2");
static_assert(static_cast<uint32_t>(wgpu::IndexFormat::Undefined) == 0,
"wgpu::IndexFormat::Undefined unexpected value");
constexpr uint32_t PackIndexFormat(wgpu::IndexFormat unpackedFormat)
{
ASSERT(static_cast<uint32_t>(unpackedFormat) > 0);
return static_cast<uint32_t>(unpackedFormat) - 1;
}
constexpr wgpu::IndexFormat UnpackIndexFormat(uint32_t packedIndexFormat)
{
return static_cast<wgpu::IndexFormat>(packedIndexFormat + 1);
}
// Can pack the front face into 1 bit since it has 2 values and Undefined is not used.
static_assert(static_cast<uint32_t>(wgpu::FrontFace::CW) == 2U, "Max wgpu::FrontFace is not 2");
static_assert(static_cast<uint32_t>(wgpu::FrontFace::Undefined) == 0,
"wgpu::FrontFace::Undefined unexpected value");
constexpr uint32_t PackFrontFace(wgpu::FrontFace unpackedFrontFace)
{
ASSERT(static_cast<uint32_t>(unpackedFrontFace) > 0);
return static_cast<uint32_t>(unpackedFrontFace) - 1;
}
constexpr wgpu::FrontFace UnpackFrontFace(uint32_t packedFrontFace)
{
return static_cast<wgpu::FrontFace>(packedFrontFace + 1);
}
// GraphicsPipelineDesc implementation.
RenderPipelineDesc::RenderPipelineDesc()
{
(void)mPad0;
memset(this, 0, sizeof(RenderPipelineDesc));
}
RenderPipelineDesc::~RenderPipelineDesc() = default;
RenderPipelineDesc::RenderPipelineDesc(const RenderPipelineDesc &other)
{
*this = other;
}
RenderPipelineDesc &RenderPipelineDesc::operator=(const RenderPipelineDesc &other)
{
memcpy(this, &other, sizeof(*this));
return *this;
}
bool RenderPipelineDesc::setPrimitiveMode(gl::PrimitiveMode primitiveMode,
gl::DrawElementsType indexTypeOrInvalid)
{
bool changed = false;
wgpu::PrimitiveTopology topology = gl_wgpu::GetPrimitiveTopology(primitiveMode);
if (mPrimitiveState.topology != static_cast<uint8_t>(topology))
{
SetBitField(mPrimitiveState.topology, topology);
changed = true;
}
uint32_t indexFormat = webgpu::IsStripPrimitiveTopology(topology)
? PackIndexFormat(gl_wgpu::GetIndexFormat(indexTypeOrInvalid))
: 0;
if (mPrimitiveState.stripIndexFormat != static_cast<uint8_t>(indexFormat))
{
SetBitField(mPrimitiveState.stripIndexFormat, indexFormat);
changed = true;
}
return changed;
}
void RenderPipelineDesc::setFrontFace(GLenum frontFace)
{
SetBitField(mPrimitiveState.frontFace, PackFrontFace(gl_wgpu::GetFrontFace(frontFace)));
}
void RenderPipelineDesc::setCullMode(gl::CullFaceMode cullMode, bool cullFaceEnabled)
{
SetBitField(mPrimitiveState.cullMode, gl_wgpu::GetCullMode(cullMode, cullFaceEnabled));
}
bool RenderPipelineDesc::setColorAttachmentFormat(size_t colorIndex, wgpu::TextureFormat format)
{
if (mColorTargetStates[colorIndex].format == static_cast<uint8_t>(format))
{
return false;
}
SetBitField(mColorTargetStates[colorIndex].format, format);
return true;
}
bool RenderPipelineDesc::setDepthStencilAttachmentFormat(wgpu::TextureFormat format)
{
if (mDepthStencilState.format == static_cast<uint8_t>(format))
{
return false;
}
SetBitField(mDepthStencilState.format, format);
return true;
}
size_t RenderPipelineDesc::hash() const
{
return angle::ComputeGenericHash(this, sizeof(*this));
}
angle::Result RenderPipelineDesc::createPipeline(ContextWgpu *context,
const wgpu::PipelineLayout &pipelineLayout,
const gl::ShaderMap<wgpu::ShaderModule> &shaders,
wgpu::RenderPipeline *pipelineOut) const
{
wgpu::RenderPipelineDescriptor pipelineDesc;
pipelineDesc.layout = pipelineLayout;
pipelineDesc.vertex.module = shaders[gl::ShaderType::Vertex];
pipelineDesc.vertex.entryPoint = "main";
pipelineDesc.vertex.constantCount = 0;
pipelineDesc.vertex.constants = nullptr;
pipelineDesc.vertex.bufferCount = 0;
pipelineDesc.vertex.buffers = nullptr;
pipelineDesc.primitive.topology =
static_cast<wgpu::PrimitiveTopology>(mPrimitiveState.topology);
if (webgpu::IsStripPrimitiveTopology(pipelineDesc.primitive.topology))
{
pipelineDesc.primitive.stripIndexFormat =
UnpackIndexFormat(mPrimitiveState.stripIndexFormat);
}
else
{
pipelineDesc.primitive.stripIndexFormat = wgpu::IndexFormat::Undefined;
}
pipelineDesc.primitive.frontFace = UnpackFrontFace(mPrimitiveState.frontFace);
pipelineDesc.primitive.cullMode = static_cast<wgpu::CullMode>(mPrimitiveState.cullMode);
// TODO: setup the vertex attribute pipeline state
(void)mVertexAttributes;
wgpu::FragmentState fragmentState;
std::array<wgpu::ColorTargetState, gl::IMPLEMENTATION_MAX_DRAW_BUFFERS> colorTargets;
std::array<wgpu::BlendState, gl::IMPLEMENTATION_MAX_DRAW_BUFFERS> blendStates;
if (shaders[gl::ShaderType::Fragment])
{
fragmentState.module = shaders[gl::ShaderType::Fragment];
fragmentState.entryPoint = "main";
fragmentState.constantCount = 0;
fragmentState.constants = nullptr;
size_t colorTargetCount = 0;
for (size_t colorTargetIndex = 0; colorTargetIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS;
++colorTargetIndex)
{
const webgpu::PackedColorTargetState &packedColorTarget =
mColorTargetStates[colorTargetIndex];
wgpu::ColorTargetState &outputColorTarget = colorTargets[colorTargetIndex];
outputColorTarget.format = static_cast<wgpu::TextureFormat>(packedColorTarget.format);
if (packedColorTarget.blendEnabled)
{
blendStates[colorTargetIndex].color.srcFactor =
static_cast<wgpu::BlendFactor>(packedColorTarget.colorBlendSrcFactor);
blendStates[colorTargetIndex].color.dstFactor =
static_cast<wgpu::BlendFactor>(packedColorTarget.colorBlendDstFactor);
blendStates[colorTargetIndex].color.operation =
static_cast<wgpu::BlendOperation>(packedColorTarget.colorBlendOp);
blendStates[colorTargetIndex].alpha.srcFactor =
static_cast<wgpu::BlendFactor>(packedColorTarget.alphaBlendSrcFactor);
blendStates[colorTargetIndex].alpha.dstFactor =
static_cast<wgpu::BlendFactor>(packedColorTarget.alphaBlendDstFactor);
blendStates[colorTargetIndex].alpha.operation =
static_cast<wgpu::BlendOperation>(packedColorTarget.alphaBlendOp);
}
outputColorTarget.writeMask =
static_cast<wgpu::ColorWriteMask>(packedColorTarget.writeMask);
if (outputColorTarget.format != wgpu::TextureFormat::Undefined)
{
colorTargetCount = colorTargetIndex + 1;
}
}
fragmentState.targetCount = colorTargetCount;
fragmentState.targets = colorTargets.data();
pipelineDesc.fragment = &fragmentState;
}
wgpu::DepthStencilState depthStencilState;
if (static_cast<wgpu::TextureFormat>(mDepthStencilState.format) !=
wgpu::TextureFormat::Undefined)
{
const webgpu::PackedDepthStencilState &packedDepthStencilState = mDepthStencilState;
depthStencilState.format = static_cast<wgpu::TextureFormat>(packedDepthStencilState.format);
depthStencilState.depthWriteEnabled =
static_cast<bool>(packedDepthStencilState.depthWriteEnabled);
depthStencilState.depthCompare =
static_cast<wgpu::CompareFunction>(packedDepthStencilState.depthCompare);
depthStencilState.stencilFront.compare =
static_cast<wgpu::CompareFunction>(packedDepthStencilState.stencilFrontCompare);
depthStencilState.stencilFront.failOp =
static_cast<wgpu::StencilOperation>(packedDepthStencilState.stencilFrontFailOp);
depthStencilState.stencilFront.depthFailOp =
static_cast<wgpu::StencilOperation>(packedDepthStencilState.stencilFrontDepthFailOp);
depthStencilState.stencilFront.passOp =
static_cast<wgpu::StencilOperation>(packedDepthStencilState.stencilFrontPassOp);
depthStencilState.stencilBack.compare =
static_cast<wgpu::CompareFunction>(packedDepthStencilState.stencilBackCompare);
depthStencilState.stencilBack.failOp =
static_cast<wgpu::StencilOperation>(packedDepthStencilState.stencilBackFailOp);
depthStencilState.stencilBack.depthFailOp =
static_cast<wgpu::StencilOperation>(packedDepthStencilState.stencilBackDepthFailOp);
depthStencilState.stencilBack.passOp =
static_cast<wgpu::StencilOperation>(packedDepthStencilState.stencilBackPassOp);
depthStencilState.stencilReadMask = packedDepthStencilState.stencilReadMask;
depthStencilState.stencilWriteMask = packedDepthStencilState.stencilWriteMask;
depthStencilState.depthBias = packedDepthStencilState.depthBias;
depthStencilState.depthBiasSlopeScale = packedDepthStencilState.depthBiasSlopeScalef;
depthStencilState.depthBiasClamp = packedDepthStencilState.depthBiasClamp;
pipelineDesc.depthStencil = &depthStencilState;
}
wgpu::Device device = context->getDevice();
*pipelineOut = device.CreateRenderPipeline(&pipelineDesc);
return angle::Result::Continue;
}
bool operator==(const RenderPipelineDesc &lhs, const RenderPipelineDesc &rhs)
{
return memcmp(&lhs, &rhs, sizeof(RenderPipelineDesc)) == 0;
}
// PipelineCache implementation.
PipelineCache::PipelineCache() = default;
PipelineCache::~PipelineCache() = default;
angle::Result PipelineCache::getRenderPipeline(ContextWgpu *context,
const RenderPipelineDesc &desc,
const wgpu::PipelineLayout &pipelineLayout,
const gl::ShaderMap<wgpu::ShaderModule> &shaders,
wgpu::RenderPipeline *pipelineOut)
{
auto iter = mRenderPipelines.find(desc);
if (iter != mRenderPipelines.end())
{
*pipelineOut = iter->second;
return angle::Result::Continue;
}
ANGLE_TRY(desc.createPipeline(context, pipelineLayout, shaders, pipelineOut));
mRenderPipelines.insert(std::make_pair(desc, *pipelineOut));
return angle::Result::Continue;
}
} // namespace webgpu
} // namespace rx