files/en-us/web/api/gpudevice/createshadermodule/index.md
{{APIRef("WebGPU API")}}{{SecureContext_Header}}{{AvailableInWorkers}}
The createShaderModule() method of the
{{domxref("GPUDevice")}} interface creates a {{domxref("GPUShaderModule")}} from a string of WGSL source code.
createShaderModule(descriptor)
descriptor
code
hints {{optional_inline}}
: A sequence of record types, with the structure ("string", compilationHint). These behave like ordered maps. In each case, the "string" is a key used to identify or select the record, and the compilationHint is either a {{domxref("GPUPipelineLayout")}} object instance or an enumerated value of "auto".
The point of hints is to provide information about the pipeline layout as early as possible to improve performance. The idea is to maximize the amount of compilation that can be done once by createShaderModule(), rather than multiple times in multiple calls to {{domxref("GPUDevice.createComputePipeline()")}} and {{domxref("GPUDevice.createRenderPipeline()")}}.
[!NOTE] Different implementations may handle
hintsin different ways, including possibly ignoring them entirely. Providing hints does not guarantee improved shader compilation performance on all browsers/systems.
label {{optional_inline}}
sourceMap {{optional_inline}}
: A source map definition to provide developer tool integration such as source-language debugging. WGSL names (identifiers) in source maps should follow the rules defined in WGSL identifier comparison. If defined, the source map may be interpreted as a source-map-v3 format.
[!NOTE] Different implementations may handle
sourceMaps in different ways, including possibly ignoring them entirely.
A {{domxref("GPUShaderModule")}} object instance.
The following criteria must be met when calling createShaderModule(), otherwise a {{domxref("GPUValidationError")}} is generated and an invalid {{domxref("GPUShaderModule")}} object is returned:
f16, it includes enable f16; at the top, and the associated {{domxref("GPUDevice")}} is created with the shader-f16 feature enabled.In our basic render demo, our shader module is created using the following code:
const shaders = `
struct VertexOut {
@builtin(position) position : vec4f,
@location(0) color : vec4f
}
@vertex
fn vertex_main(@location(0) position: vec4f,
@location(1) color: vec4f) -> VertexOut
{
var output : VertexOut;
output.position = position;
output.color = color;
return output;
}
@fragment
fn fragment_main(fragData: VertexOut) -> @location(0) vec4f
{
return fragData.color;
}
`;
async function init() {
if (!navigator.gpu) {
throw Error("WebGPU not supported.");
}
const adapter = await navigator.gpu.requestAdapter();
if (!adapter) {
throw Error("Couldn't request WebGPU adapter.");
}
const device = await adapter.requestDevice();
// …
// later on
const shaderModule = device.createShaderModule({
code: shaders,
});
// …
}
{{Specifications}}
{{Compat}}