(device: GPUDevice, config: RenderPipelineConfig)
| 124 | * - `multisample.count: 1` |
| 125 | */ |
| 126 | export function createRenderPipeline(device: GPUDevice, config: RenderPipelineConfig): GPURenderPipeline { |
| 127 | const layout: GPUPipelineLayout | 'auto' = |
| 128 | config.layout ?? |
| 129 | (config.bindGroupLayouts ? device.createPipelineLayout({ bindGroupLayouts: [...config.bindGroupLayouts] }) : 'auto'); |
| 130 | |
| 131 | const vertexStage = getStageModule(device, config.vertex); |
| 132 | const vertexEntryPoint = vertexStage.entryPoint || DEFAULT_VERTEX_ENTRY; |
| 133 | |
| 134 | let fragment: GPUFragmentState | undefined = undefined; |
| 135 | if (config.fragment) { |
| 136 | const fragmentStage = getStageModule(device, config.fragment); |
| 137 | const fragmentEntryPoint = fragmentStage.entryPoint || DEFAULT_FRAGMENT_ENTRY; |
| 138 | |
| 139 | let targets: readonly GPUColorTargetState[] | undefined = config.fragment.targets; |
| 140 | if (!targets) { |
| 141 | const formats = config.fragment.formats; |
| 142 | if (!formats) { |
| 143 | throw new Error( |
| 144 | "createRenderPipeline(fragment): provide either `fragment.targets` or `fragment.formats` when a fragment stage is present." |
| 145 | ); |
| 146 | } |
| 147 | const formatList = Array.isArray(formats) ? formats : [formats]; |
| 148 | targets = formatList.map((format) => ({ |
| 149 | format, |
| 150 | blend: config.fragment!.blend, |
| 151 | writeMask: config.fragment!.writeMask, |
| 152 | })); |
| 153 | } |
| 154 | |
| 155 | fragment = { |
| 156 | module: fragmentStage.module, |
| 157 | entryPoint: fragmentEntryPoint, |
| 158 | targets: [...targets], |
| 159 | constants: fragmentStage.constants, |
| 160 | }; |
| 161 | } |
| 162 | |
| 163 | const primitive: GPUPrimitiveState = config.primitive ?? { topology: 'triangle-list' }; |
| 164 | const multisample: GPUMultisampleState = config.multisample ?? { count: 1 }; |
| 165 | |
| 166 | return device.createRenderPipeline({ |
| 167 | label: config.label, |
| 168 | layout, |
| 169 | vertex: { |
| 170 | module: vertexStage.module, |
| 171 | entryPoint: vertexEntryPoint, |
| 172 | buffers: config.vertex.buffers ? [...config.vertex.buffers] : [], |
| 173 | constants: vertexStage.constants, |
| 174 | }, |
| 175 | fragment, |
| 176 | primitive, |
| 177 | depthStencil: config.depthStencil, |
| 178 | multisample, |
| 179 | }); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Creates a uniform buffer suitable for `@group/@binding` uniform bindings. |
no test coverage detected