@@ -117,6 +117,40 @@ wgpu::BindGroupEntry ToWGPU(const Replay& replay, const schema::BindGroupEntry&
117117 };
118118}
119119
120+ wgpu::StencilFaceState ToWGPU (const schema::StencilFaceState& state) {
121+ return wgpu::StencilFaceState{
122+ .compare = state.compare ,
123+ .failOp = state.failOp ,
124+ .depthFailOp = state.depthFailOp ,
125+ .passOp = state.passOp ,
126+ };
127+ }
128+
129+ wgpu::BlendComponent ToWGPU (const schema::BlendComponent& component) {
130+ return wgpu::BlendComponent{
131+ .operation = component.operation ,
132+ .srcFactor = component.srcFactor ,
133+ .dstFactor = component.dstFactor ,
134+ };
135+ }
136+
137+ wgpu::BlendState ToWGPU (const schema::BlendState& state) {
138+ return wgpu::BlendState{
139+ .color = ToWGPU (state.color ),
140+ .alpha = ToWGPU (state.alpha ),
141+ };
142+ }
143+
144+ bool IsBlendComponentEnabled (const wgpu::BlendComponent& component) {
145+ return component.operation != wgpu::BlendOperation::Add ||
146+ component.srcFactor != wgpu::BlendFactor::One ||
147+ component.dstFactor != wgpu::BlendFactor::Zero;
148+ }
149+
150+ bool IsBlendEnabled (const wgpu::BlendState& blend) {
151+ return IsBlendComponentEnabled (blend.color ) || IsBlendComponentEnabled (blend.alpha );
152+ }
153+
120154MaybeError ReadContentIntoBuffer (ReadHead& readHead,
121155 wgpu::Device device,
122156 wgpu::Buffer buffer,
@@ -230,6 +264,92 @@ ResultOrError<wgpu::ComputePipeline> CreateComputePipeline(const Replay& replay,
230264 return {computePipeline};
231265}
232266
267+ template <typename F>
268+ ResultOrError<wgpu::RenderPipeline> CreateRenderPipeline (const Replay& replay,
269+ wgpu::Device device,
270+ ReadHead& readHead,
271+ const std::string& label,
272+ F func) {
273+ schema::RenderPipeline pipeline;
274+ DAWN_TRY (Deserialize (readHead, &pipeline));
275+
276+ std::vector<wgpu::ConstantEntry> vertexConstants = ToWGPU (pipeline.vertex .program .constants );
277+ std::vector<wgpu::ConstantEntry> fragmentConstants =
278+ ToWGPU (pipeline.fragment .program .constants );
279+ std::vector<wgpu::ColorTargetState> colorTargets;
280+ std::vector<wgpu::BlendState> blendStates (pipeline.fragment .targets .size ());
281+
282+ wgpu::FragmentState* fragment = nullptr ;
283+ wgpu::FragmentState fragmentState;
284+ if (pipeline.fragment .program .moduleId ) {
285+ fragment = &fragmentState;
286+ fragmentState.module =
287+ replay.GetObjectById <wgpu::ShaderModule>(pipeline.fragment .program .moduleId );
288+ fragmentState.entryPoint = wgpu::StringView (pipeline.fragment .program .entryPoint );
289+ fragmentState.constantCount = fragmentConstants.size ();
290+ fragmentState.constants = fragmentConstants.data ();
291+ for (const auto & target : pipeline.fragment .targets ) {
292+ wgpu::BlendState& blend = blendStates[colorTargets.size ()];
293+ blend = ToWGPU (target.blend );
294+ colorTargets.push_back ({
295+ .format = target.format ,
296+ .blend = IsBlendEnabled (blend) ? &blend : nullptr ,
297+ .writeMask = target.writeMask ,
298+ });
299+ }
300+ fragmentState.targetCount = colorTargets.size ();
301+ fragmentState.targets = colorTargets.data ();
302+ }
303+
304+ wgpu::DepthStencilState* depthStencil = nullptr ;
305+ wgpu::DepthStencilState depthStencilState;
306+ if (pipeline.depthStencil .format != wgpu::TextureFormat::Undefined) {
307+ depthStencil = &depthStencilState;
308+ depthStencilState.format = pipeline.depthStencil .format ;
309+ depthStencilState.depthWriteEnabled = pipeline.depthStencil .depthWriteEnabled ;
310+ depthStencilState.depthCompare = pipeline.depthStencil .depthCompare ;
311+ depthStencilState.stencilFront = ToWGPU (pipeline.depthStencil .stencilFront );
312+ depthStencilState.stencilBack = ToWGPU (pipeline.depthStencil .stencilBack );
313+ depthStencilState.stencilReadMask = pipeline.depthStencil .stencilReadMask ;
314+ depthStencilState.stencilWriteMask = pipeline.depthStencil .stencilWriteMask ;
315+ depthStencilState.depthBias = pipeline.depthStencil .depthBias ;
316+ depthStencilState.depthBiasSlopeScale = pipeline.depthStencil .depthBiasSlopeScale ;
317+ depthStencilState.depthBiasClamp = pipeline.depthStencil .depthBiasClamp ;
318+ }
319+
320+ wgpu::RenderPipelineDescriptor desc{
321+ .label = wgpu::StringView (label),
322+ .layout = replay.GetObjectById <wgpu::PipelineLayout>(pipeline.layoutId ),
323+ .vertex =
324+ {
325+ .module =
326+ replay.GetObjectById <wgpu::ShaderModule>(pipeline.vertex .program .moduleId ),
327+ .entryPoint = wgpu::StringView (pipeline.vertex .program .entryPoint ),
328+ .constantCount = vertexConstants.size (),
329+ .constants = vertexConstants.data (),
330+ },
331+ .primitive =
332+ {
333+ .topology = pipeline.primitive .topology ,
334+ .stripIndexFormat = pipeline.primitive .stripIndexFormat ,
335+ .frontFace = pipeline.primitive .frontFace ,
336+ .cullMode = pipeline.primitive .cullMode ,
337+ .unclippedDepth = pipeline.primitive .unclippedDepth ,
338+ },
339+ .depthStencil = depthStencil,
340+ .multisample =
341+ {
342+ .count = pipeline.multisample .count ,
343+ .mask = pipeline.multisample .mask ,
344+ .alphaToCoverageEnabled = pipeline.multisample .alphaToCoverageEnabled ,
345+ },
346+ .fragment = fragment,
347+ };
348+ wgpu::RenderPipeline renderPipeline = device.CreateRenderPipeline (&desc);
349+ func (renderPipeline, pipeline.groupIndexIds );
350+ return {renderPipeline};
351+ }
352+
233353ResultOrError<wgpu::ShaderModule> CreateShaderModule (wgpu::Device device,
234354 ReadHead& readHead,
235355 const std::string& label) {
@@ -554,6 +674,25 @@ MaybeError Replay::CreateResource(wgpu::Device device, ReadHead& readHead) {
554674 return {};
555675 }
556676
677+ case schema::ObjectType::RenderPipeline: {
678+ wgpu::RenderPipeline renderPipeline;
679+ DAWN_TRY_ASSIGN (
680+ renderPipeline,
681+ CreateRenderPipeline (
682+ *this , device, readHead, resource.label ,
683+ [this ](wgpu::RenderPipeline& renderPipeline,
684+ const std::vector<schema::BindGroupLayoutIndexIdPair>& groupIndexIds) {
685+ // Register any implicit bindgroups.
686+ for (const auto & groupIndexId : groupIndexIds) {
687+ wgpu::BindGroupLayout bgl =
688+ renderPipeline.GetBindGroupLayout (groupIndexId.groupIndex );
689+ mResources .insert ({groupIndexId.bindGroupLayoutId , {" " , bgl}});
690+ }
691+ }));
692+ mResources .insert ({resource.id , {resource.label , renderPipeline}});
693+ return {};
694+ }
695+
557696 case schema::ObjectType::ShaderModule: {
558697 wgpu::ShaderModule shaderModule;
559698 DAWN_TRY_ASSIGN (shaderModule, CreateShaderModule (device, readHead, resource.label ));
@@ -629,8 +768,7 @@ MaybeError Replay::Play() {
629768 break ;
630769 }
631770 default : {
632- // UNIMPLEMENTED();
633- break ;
771+ return DAWN_INTERNAL_ERROR (" unimplemented root command" );
634772 }
635773 }
636774 }
0 commit comments