GPUDevice: createRenderPipeline() method
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
Note: This feature is available inWeb Workers.
ThecreateRenderPipeline() method of theGPUDevice interface creates aGPURenderPipeline that can control the vertex and fragment shader stages and be used in aGPURenderPassEncoder orGPURenderBundleEncoder.
In this article
Syntax
createRenderPipeline(descriptor)Parameters
descriptorAn object containing the following properties:
depthStencilOptionalAn object (see
depthStencilobject structure) describing depth-stencil properties including testing, operations, and bias.fragmentOptionalAn object (see
fragmentobject structure) describing the fragment shader entry point of the pipeline and its output colors. If no fragment shader entry point is defined, the pipeline will not produce any color attachment outputs, but it still performs rasterization and produces depth values based on the vertex position output. Depth testing and stencil operations can still be used.labelOptionalA string providing a label that can be used to identify the object, for example in
GPUErrormessages or console warnings.layoutDefines the layout (structure, purpose, and type) of all the GPU resources (buffers, textures, etc.) used during the execution of the pipeline. Possible values are:
- A
GPUPipelineLayoutobject, created usingGPUDevice.createPipelineLayout(), which allows the GPU to figure out how to run the pipeline most efficiently ahead of time. - A string of
"auto", which causes the pipeline to generate an implicit bind group layout based on any bindings defined in the shader code. If"auto"is used, the generated bind group layouts may only be used with the current pipeline.
- A
multisampleOptionalAn object (see
multisampleobject structure) describing how the pipeline interacts with a render pass's multisampled attachments.primitiveOptionalAn object (see
primitiveobject structure) describing how a pipeline constructs and rasterizes primitives from its vertex inputs.vertexAn object (see
vertexobject structure) describing the vertex shader entry point of the pipeline and its input buffer layouts.
depthStencil object structure
ThedepthStencil object can contain the following properties:
depthBiasOptionalA number representing a constant depth bias that is added to each fragment. If omitted,
depthBiasdefaults to 0.Note:The
depthBias,depthBiasClamp, anddepthBiasSlopeScaleproperties must be set to0for line and point topologies, i.e., iftopologyis set to"line-list","line-strip", or"point-list". If not, aGPUValidationErrorwill be generated and the returnedGPURenderPipelinewill be invalid.depthBiasClampOptionalA number representing the maximum depth bias of a fragment. If omitted,
depthBiasClampdefaults to 0.depthBiasSlopeScaleOptionalA number representing a depth bias that scales with the fragment's slope. If omitted,
depthBiasSlopeScaledefaults to 0.depthCompareOptionalAn enumerated value specifying the comparison operation used to test fragment depths against
depthStencilAttachmentdepth values. Possible values are:"never": Comparison tests never pass."less": A provided value passes the comparison test if it is less than the sampled value."equal": A provided value passes the comparison test if it is equal to the sampled value."less-equal": A provided value passes the comparison test if it is less than or equal to the sampled value."greater": A provided value passes the comparison test if it is greater than the sampled value."not-equal": A provided value passes the comparison test if it is not equal to the sampled value."greater-equal": A provided value passes the comparison test if it is greater than or equal to the sampled value."always": Comparison tests always pass.
depthCompareis not required if the specifiedformatdoes not have a depth component, or if the comparison operation is not used.depthWriteEnabledOptionalA boolean. A value of
truespecifies that theGPURenderPipelinecan modifydepthStencilAttachmentdepth values after creation. Setting it tofalsemeans it cannot.depthWriteEnabledis not required if the specifiedformatdoes not have a depth component.formatAn enumerated value specifying the
depthStencilAttachmentformat that theGPURenderPipelinewill be compatible with. See the specification'sTexture Formats section for all the availableformatvalues.stencilBackOptionalAn object that defines how stencil comparisons and operations are performed for back-facing primitives. Its properties can include:
compareOptionalAn enumerated value specifying the comparison operation used when testing fragments against
depthStencilAttachmentstencil values. Possible values are the same as for thedepthCompareproperty; see above. If omitted,comparedefaults to"always".depthFailOpOptionalAn enumerated value specifying the stencil operation performed if the fragment depth comparison described by
depthComparefails. Possible values are:"decrement-clamp": Decrement the current render state stencil value, clamping it to 0."decrement-wrap": Decrement the current render state stencil value, wrapping it to the maximum representable value of thedepthStencilAttachment's stencil aspect if the value goes below 0."invert": Bitwise-invert the current render state stencil value."increment-clamp": Increments the current render state stencil value, clamping it to the maximum representable value of thedepthStencilAttachment's stencil aspect."increment-wrap": Increments the current render state stencil value, wrapping it to zero if the value exceeds the maximum representable value of thedepthStencilAttachment's stencil aspect."keep": Keep the current stencil value."replace": Set the stencil value to the current render state stencil value."zero": Set the stencil value to 0.
If omitted,
depthFailOpdefaults to"keep".Note:The render state stencil value is initialized to 0 at the start of a render pass.
failOpOptionalAn enumerated value specifying the stencil operation performed if the fragment stencil comparison test described by
comparefails. Possible and default values are the same as fordepthFailOp.passOpOptionalAn enumerated value specifying the stencil operation performed if the fragment stencil comparison test described by
comparepasses. Possible and default values are the same as fordepthFailOp.
stencilFrontOptionalAn object that defines how stencil comparisons and operations are performed for front-facing primitives. Its properties are the same as for
stencilBack.stencilReadMaskOptionalA bitmask controlling which
depthStencilAttachmentstencil value bits are read when performing stencil comparison tests. If omitted,stencilReadMaskdefaults to0xFFFFFFFF.stencilWriteMaskOptionalA bitmask controlling which
depthStencilAttachmentstencil value bits are written to when performing stencil operations. If omitted,stencilWriteMaskdefaults to0xFFFFFFFF.
Note:depthStencilAttachment values are specified duringGPUCommandEncoder.beginRenderPass() calls, when theGPURenderPipeline is actually used to perform a render pass.
fragment object structure
Thefragment object contains an array of objects, each of which can contain the following properties:
constantsOptionalA sequence of record types, with the structure
(id, value), representing override values forWGSL constants that can be overridden in the pipeline. These behave likeordered maps. In each case, theidis a key used to identify or select the record, and theconstantis an enumerated value representing a WGSL.Depending on which constant you want to override, the
idmay take the form of the numeric ID of the constant, if one is specified, or otherwise the constant's identifier name.A code snippet providing override values for several overridable constants might look like this:
js({ // … constants: { 0: false, 1200: 3.0, 1300: 2.0, width: 20, depth: -1, height: 15, },});entryPointOptionalThe name of the function in the
modulethat this stage will use to perform its work. The corresponding shader function must have the@fragmentattribute to be identified as this entry point. SeeEntry Point Declaration for more information.You can omit the
entryPointproperty if your shader code contains a single function with the@fragmentattribute set — the browser will use this as the default entry point. IfentryPointis omitted and the browser cannot determine a default entry point, aGPUValidationErroris generated and the resultingGPURenderPipelinewill be invalid.moduleA
GPUShaderModuleobject containing theWGSL code that this programmable stage will execute.targetsan array of objects representing color states that represent configuration details for the colors output by the fragment shader stage. These objects can include the following properties:
blendOptionalAn object that describes a blend mode to be applied to the output color.
blendhas two properties:alphaandcolorboth take an object as a value that can include the following properties:dstFactorOptionalAn enumerated value that defines the blend factor operation to be performed on values from the target attachment. Possible values are:
"constant""dst""dst-alpha""one""one-minus-dst""one-minus-src""one-minus-src1""one-minus-src-alpha""one-minus-src1-alpha""one-minus-dst-alpha""one-minus-constant""src""src1""src-alpha""src1-alpha""src-alpha-saturated""zero"
If omitted,
dstFactordefaults to"zero".Note:The
dual-source-blendingfeature needs to be enabled for thesrc1,one-minus-src1,src1-alpha, andone-minus-src1-alphablend factor operations to be used successfully. If not, aGPUValidationErroris generated.operationOptionalAn enumerated value that defines the algorithm used to combine source and destination blend factors, to calculate the final values written to the target attachment components. Possible values are:
"add""max""min""reverse-subtract""subtract"
If omitted,
operationdefaults to"add".srcFactorOptionalAn enumerated value that defines the blend factor operation to be performed on values from the fragment shader. Possible values are the same as for
dstFactor. If omitted,srcFactordefaults to"one".
Note:For a detailed explanation of the algorithms defined by each
dstFactor/srcFactorandoperationenumerated value, see theBlend State section of the specification.formatAn enumerated value specifying the required format for output colors. See the specification'sTexture Formats section for all the available
formatvalues.writeMaskOptionalOne or morebitwise flags defining the write mask to apply to the color target state. Possible flag values are:
GPUColorWrite.REDGPUColorWrite.GREENGPUColorWrite.BLUEGPUColorWrite.ALPHAGPUColorWrite.ALL
If omitted,
writeMaskdefaults toGPUColorWrite.ALL.Note that multiple flags can be specified by separating values withbitwise OR, for example:
GPUColorWrite.RED | GPUColorWrite.ALPHA.
multisample object structure
Themultisample object can contain the following properties:
alphaToCoverageEnabledOptionalA boolean. A value of
trueindicates that a fragment's alpha channel should be used to generate a sample coverage mask. If omitted,alphaToCoverageEnableddefaults tofalse.countOptionalA number that defines the number of samples per pixel. The pipeline will be compatible only with attachment textures (
colorAttachments anddepthStencilAttachments) with matchingsampleCounts(seeGPUTexture).If omitted,
countdefaults to 1.maskOptionalA bitmask that determines which samples are written to. If omitted,
maskdefaults to0xFFFFFFFF.
Note:colorAttachment anddepthStencilAttachment values are specified duringGPUCommandEncoder.beginRenderPass() calls, when theGPURenderPipeline is actually used to perform a render pass.
primitive object structure
Theprimitive object can contain the following properties:
cullModeOptionalAn enumerated value that defines which polygon orientation will be culled, if any. Possible values are:
"back": Back-facing polygons are culled."front": Front-facing polygons are culled."none": No polygons are culled.
If omitted,
cullModedefaults to"none".frontFaceOptionalAn enumerated value that defines which polygons are considered front-facing. Possible values are:
"ccw": Polygons with vertices whose framebuffer coordinates are given in counter-clockwise order."cw": Polygons with vertices whose framebuffer coordinates are given in clockwise order.
If omitted,
frontFacedefaults to"ccw".Note:The
frontFaceandcullModevalues have no effect on the"point-list","line-list", or"line-strip"topologies.stripIndexFormatOptionalAn enumerated value that determines the index buffer format and primitive restart value in the case of pipelines with strip topologies (
"line-strip"or"triangle-strip"). The primitive restart value specifies which index value indicates that a new primitive should be started rather than continuing to construct the strip with the prior indexed vertices. Possible values are:"uint16": Indicates a byte size of 2 and a primitive restart value of0xFFFF."uint32": Indicates a byte size of 4 and a primitive restart value of0xFFFFFFFF.
GPU primitive states that specify a strip primitive topology must specify a strip index format if they are used for indexed draws (for example, via
GPURenderPassEncoder.drawIndexed()) so that the primitive restart value that will be used is known at pipeline creation time. Pipelines with list primitive topologies ("line-list","point-list", or"triangle-list") should not specify astripIndexFormatvalue. They will instead use the index format passed to, for example,GPURenderPassEncoder.setIndexBuffer()when doing indexed rendering.topologyOptionalAn enumerated value that defines the type of primitive to be constructed from the specified
vertexinputs. Possible values are:"line-list": Each consecutive pair of two vertices defines a line primitive."line-strip": Each vertex after the first defines a line primitive between it and the previous vertex."point-list": Each vertex defines a point primitive."triangle-list": Each consecutive triplet of three vertices defines a triangle primitive."triangle-strip": Each vertex after the first two defines a triangle primitive between it and the previous two vertices.
If omitted,
topologydefaults to"triangle-list".unclippedDepthOptionalA boolean. A value of
trueindicates that depth clipping is disabled. If omitted,unclippedDepthdefaults tofalse. Note that to control depth clipping, thedepth-clip-controlfeature must be enabled in theGPUDevice.Note:The
depth-clip-controlfeature needs to be enabled for theunclippedDepthproperty to be used successfully. If not, aGPUValidationErroris generated.
vertex object structure
Thevertex object can contain the following properties:
constantsOptionalA sequence of record types, with the structure
(id, value), representing override values forWGSL constants that can be overridden in the pipeline. These behave likeordered maps. In each case, theidis a key used to identify or select the record, and theconstantis an enumerated value representing a WGSL.Depending on which constant you want to override, the
idmay take the form of the numeric ID of the constant, if one is specified, or otherwise the constant's identifier name.A code snippet providing override values for several overridable constants might look like this:
js({ // … constants: { 0: false, 1200: 3.0, 1300: 2.0, width: 20, depth: -1, height: 15, },});entryPointOptionalThe name of the function in the
modulethat this stage will use to perform its work. The corresponding shader function must have the@vertexattribute to be identified as this entry point. SeeEntry Point Declaration for more information.You can omit the
entryPointproperty if your shader code contains a single function with the@vertexattribute set — the browser will use this as the default entry point. IfentryPointis omitted and the browser cannot determine a default entry point, aGPUValidationErroris generated and the resultingGPURenderPipelinewill be invalid.moduleA
GPUShaderModuleobject containing theWGSL code that this programmable stage will execute.buffersOptionalAn array of objects, each representing the expected layout of a vertex buffer used in the pipeline. Each object can contain the following properties:
arrayStrideA number representing the stride, in bytes, between the different structures (e.g., vertices) inside the buffer.
attributesAn array of objects defining the layout of the vertex attributes within each structure. Each object has the following properties:
formatAn enumerated value that specifies the format of the vertex. For all the available values, see the
GPUVertexFormatdefinition in the specification.offsetA number specifying the offset, in bytes, from the beginning of the structure to the data for the attribute.
shaderLocationThe numeric location associated with this attribute, which will correspond with a
@locationattribute declared in the WGSL code of the associatedGPUShaderModulereferenced in thevertexobject'smoduleproperty.
stepModeOptionalAn enumerated value that defines whether the separate structures inside the buffer represent vertices or instances. Possible values are:
"instance": Each structure is an instance — the address is advanced byarrayStridefor each instance."vertex": Each structure is a vertex — the address is advanced byarrayStridefor each vertex, and reset between instances.
If omitted,
stepModedefaults to"vertex".
Return value
AGPURenderPipeline object instance.
Validation
The following criteria must be met when callingcreateRenderPipeline(), otherwise aGPUValidationError is generated and an invalidGPURenderPipeline object is returned:
- For
depthStencilobjects:formatis adepth-or-stencilformat.- The
depthBias,depthBiasClamp, anddepthBiasSlopeScaleproperties are set to0for line and point topologies, i.e., iftopologyis set to"line-list","line-strip", or"point-list". - If
depthWriteEnabledistrueordepthCompareis not"always",formathas a depth component. - If
stencilFrontorstencilBack's properties are not at their default values,formathas a stencil component.
- For
fragmentobjects:targets.lengthis less than or equal to theGPUDevice'smaxColorAttachmentslimit.- For each
target,writeMask's numeric equivalent is less than 16. - If any of the used blend factor operations use the source alpha channel (for example
"src-alpha-saturated"), the output has an alpha channel (that is, it must be avec4). - If the
src1,one-minus-src1,src1-alpha, orone-minus-src1-alphablend factor operations are used, thedual-source-blendingfeature is enabled. - If the
entryPointproperty is omitted, the shader code contains a single fragment shader entry point function for the browser to use as the default entry point.
- For
primitiveobjects:- If the
unclippedDepthproperty is used, thedepth-clip-controlfeature is enabled.
- If the
- For
vertexobjects:- If the
entryPointproperty is omitted, the shader code contains a single vertex shader entry point function for the browser to use as the default entry point.
- If the
Examples
Note:TheWebGPU samples feature many more examples.
Basic example
Ourbasic render demo provides an example of the construction of a valid render pipeline descriptor object, which is then used to create aGPURenderPipeline via acreateRenderPipeline() call.
// …const vertexBuffers = [ { attributes: [ { shaderLocation: 0, // position offset: 0, format: "float32x4", }, { shaderLocation: 1, // color offset: 16, format: "float32x4", }, ], arrayStride: 32, stepMode: "vertex", },];const pipelineDescriptor = { vertex: { module: shaderModule, entryPoint: "vertex_main", buffers: vertexBuffers, }, fragment: { module: shaderModule, entryPoint: "fragment_main", targets: [ { format: navigator.gpu.getPreferredCanvasFormat(), }, ], }, primitive: { topology: "triangle-list", }, layout: "auto",};const renderPipeline = device.createRenderPipeline(pipelineDescriptor);// …Specifications
| Specification |
|---|
| WebGPU> # dom-gpudevice-createrenderpipeline> |
Browser compatibility
See also
- TheWebGPU API