Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 7cb9531

Browse files
committed
Added support to set scissor.
1 parent e2254cd commit 7cb9531

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

lib/gpu/lib/src/render_pass.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ base class SamplerOptions {
150150
SamplerAddressMode heightAddressMode;
151151
}
152152

153+
base class Scissor {
154+
Scissor({this.x = 0, this.y = 0, this.width = 0, this.height = 0});
155+
156+
int x, y, width, height;
157+
}
158+
153159
base class RenderTarget {
154160
const RenderTarget(
155161
{this.colorAttachments = const <ColorAttachment>[],
@@ -214,6 +220,9 @@ base class RenderPass extends NativeFieldWrapperClass1 {
214220
if (error != null) {
215221
throw Exception(error);
216222
}
223+
224+
final texture = renderTarget.depthStencilAttachment!.texture;
225+
_setScissor(0, 0, texture.width, texture.height);
217226
}
218227
error = _begin(commandBuffer);
219228
if (error != null) {
@@ -326,6 +335,10 @@ base class RenderPass extends NativeFieldWrapperClass1 {
326335
targetFace.index);
327336
}
328337

338+
void setScissor(Scissor scissor) {
339+
_setScissor(scissor.x, scissor.y, scissor.width, scissor.height);
340+
}
341+
329342
void setCullMode(CullMode cullMode) {
330343
_setCullMode(cullMode.index);
331344
}
@@ -477,6 +490,14 @@ base class RenderPass extends NativeFieldWrapperClass1 {
477490
int readMask,
478491
int writeMask,
479492
int target_face);
493+
494+
@Native<Void Function(Pointer<Void>, Int, Int, Int, Int)>(
495+
symbol: 'InternalFlutterGpu_RenderPass_SetScissor')
496+
external void _setScissor(
497+
int x,
498+
int y,
499+
int width,
500+
int height);
480501

481502
@Native<Void Function(Pointer<Void>, Int)>(
482503
symbol: 'InternalFlutterGpu_RenderPass_SetCullMode')

lib/gpu/render_pass.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ bool RenderPass::Draw() {
206206
render_pass_->SetElementCount(element_count);
207207

208208
render_pass_->SetStencilReference(stencil_reference);
209+
render_pass_->SetScissor(scissor);
209210

210211
bool result = render_pass_->Draw().ok();
211212

@@ -536,6 +537,11 @@ void InternalFlutterGpu_RenderPass_SetStencilReference(
536537
wrapper->stencil_reference = static_cast<uint32_t>(stencil_reference);
537538
}
538539

540+
void InternalFlutterGpu_RenderPass_SetScissor(
541+
flutter::gpu::RenderPass* wrapper, int x, int y, int width, int height) {
542+
wrapper->scissor = impeller::TRect<int64_t>::MakeXYWH(x, y, width, height);
543+
}
544+
539545
void InternalFlutterGpu_RenderPass_SetStencilConfig(
540546
flutter::gpu::RenderPass* wrapper,
541547
int stencil_compare_operation,

lib/gpu/render_pass.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class RenderPass : public RefCountedDartWrappable<RenderPass> {
7474
size_t element_count = 0;
7575

7676
uint32_t stencil_reference = 0;
77+
impeller::TRect<int64_t> scissor;
7778

7879
// Helper flag to determine whether the vertex_count should override the
7980
// element count. The index count takes precedent.
@@ -234,6 +235,14 @@ extern void InternalFlutterGpu_RenderPass_SetStencilConfig(
234235
int write_mask,
235236
int target);
236237

238+
FLUTTER_GPU_EXPORT
239+
extern void InternalFlutterGpu_RenderPass_SetScissor(
240+
flutter::gpu::RenderPass* wrapper,
241+
int x,
242+
int y,
243+
int width,
244+
int height);
245+
237246
FLUTTER_GPU_EXPORT
238247
extern void InternalFlutterGpu_RenderPass_SetCullMode(
239248
flutter::gpu::RenderPass* wrapper,

testing/dart/gpu_test.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,4 +736,50 @@ void main() async {
736736
await comparer.addGoldenImage(
737737
image, 'flutter_gpu_test_hexgon_line_strip.png');
738738
}, skip: !impellerEnabled);
739+
740+
// Renders the middle part triangle using scissor.
741+
test('Can render portion of the triangle using scissor', () async {
742+
final state = createSimpleRenderPass();
743+
744+
final gpu.RenderPipeline pipeline = createUnlitRenderPipeline();
745+
state.renderPass.bindPipeline(pipeline);
746+
747+
// Configure blending with defaults (just to test the bindings).
748+
state.renderPass.setColorBlendEnable(true);
749+
state.renderPass.setColorBlendEquation(gpu.ColorBlendEquation());
750+
751+
// Set primitive type.
752+
state.renderPass.setPrimitiveType(gpu.PrimitiveType.triangle);
753+
754+
// Set scissor.
755+
state.renderPass.setScissor(gpu.Scissor(x: 25, y: 0, width: 50, height: 100));
756+
757+
final gpu.HostBuffer transients = gpu.gpuContext.createHostBuffer();
758+
final gpu.BufferView vertices = transients.emplace(float32(<double>[
759+
-1.0,
760+
-1.0,
761+
0.0,
762+
1.0,
763+
1.0,
764+
-1.0]));
765+
final gpu.BufferView vertInfoData = transients.emplace(float32(<double>[
766+
1, 0, 0, 0, // mvp
767+
0, 1, 0, 0, // mvp
768+
0, 0, 1, 0, // mvp
769+
0, 0, 0, 1, // mvp
770+
0, 1, 0, 1, // color
771+
]));
772+
state.renderPass.bindVertexBuffer(vertices, 3);
773+
774+
final gpu.UniformSlot vertInfo =
775+
pipeline.vertexShader.getUniformSlot('VertInfo');
776+
state.renderPass.bindUniform(vertInfo, vertInfoData);
777+
state.renderPass.draw();
778+
779+
state.commandBuffer.submit();
780+
781+
final ui.Image image = state.renderTexture.asImage();
782+
await comparer.addGoldenImage(
783+
image, 'flutter_gpu_test_scissor.png');
784+
}, skip: !impellerEnabled);
739785
}

0 commit comments

Comments
 (0)