import 'dart:typed_data';
import 'package:flutter_gpu/gpu.dart';
void main() {
final commandBuffer = gpuContext.createCommandBuffer();
final vertexShader = shaderLibrary["LineVertex"]!;
final fragmentShader = shaderLibrary["LineFragment"]!;
final renderTarget = _setupRenderTarget();
final renderPass = commandBuffer.createRenderPass(renderTarget);
final pipeline = gpuContext.createRenderPipeline(vertexShader, fragmentShader);
renderPass.bindPipeline(pipeline);
setupVertices(renderPass);
setupUniform(renderPass, fragmentShader);
renderPass.draw();
commandBuffer.submit();
}
void setupUniform(RenderPass renderPass, Shader fragmentShader) {
final uniform = Float32List.fromList([1.0, 0.0, 0.0, 1.0]).buffer.asByteData();
final transientBuffer = gpuContext.createHostBuffer();
final uniformBufferView = transientBuffer.emplace(uniform);
final uniformSlot = fragmentShader.getUniformSlot('Paint');
renderPass.bindUniform(uniformSlot, uniformBufferView);
}
void setupVertices(RenderPass renderPass) {
final vertices = Float32List.fromList([
-1, -1, 0,
1, -1, 0,
-1, 1, 0,
1, -1, 0,
1, 1, 0,
-1, 1, 0,
]);
final vertexBuffer = gpuContext.createDeviceBufferWithCopy(ByteData.sublistView(vertices));
final vertexBufferView = BufferView(
vertexBuffer,
offsetInBytes: 0,
lengthInBytes: vertices.buffer.lengthInBytes,
);
renderPass.bindVertexBuffer(vertexBufferView, vertices.length ~/ 3);
}
_setupRenderTarget() {
final outputTexture = gpuContext.createTexture(StorageMode.hostVisible, 256, 256);
return RenderTarget.singleColor(
ColorAttachment(texture: outputTexture),
);
}
const String _shaderBundlePath =
'build/shaderbundles/demo_shaders.shaderbundle';
ShaderLibrary? _shaderLibrary;
ShaderLibrary get shaderLibrary {
if (_shaderLibrary != null) {
return _shaderLibrary!;
}
_shaderLibrary = ShaderLibrary.fromAsset(_shaderBundlePath);
if (_shaderLibrary != null) {
return _shaderLibrary!;
}
throw Exception("Failed to load shader bundle! ($_shaderBundlePath)");
}
in vec3 position;
void main() {
gl_Position = vec4(position, 1.0);
}
uniform Paint {
vec4 color;
}
paint;
out vec4 frag_color;
void main() {
frag_color = paint.color;
}
{
"LineVertex": {
"type": "vertex",
"file": "shaders/line.vert"
},
"LineFragment": {
"type": "fragment",
"file": "shaders/line.frag"
}
}
Steps to reproduce
Expected results
A custom shader added to a shader bundle should maintain its defined behaviour when it is retrieved for rendering. Developers using the flutter_gpu library should not need to be aware of internal Impeller shader filenames while authoring shaders.
Actual results
Attempting to retrieve a shader from a user-defined shader bundle will return an internal Impeller shader if there exists one with the same filename. The compiled shaderbundle file appears correct, but the shader loaded at runtime is incorrect.
Code sample
Code sample
line.vert:
line.frag:
demo_shaders.shaderbundle.json:
Screenshots or Video
Screenshots / Video demonstration
Logs
Console commands & Logs
Output:
Flutter Doctor output
Doctor output