@@ -428,6 +428,28 @@ static bool Bind(PassBindingsCache& pass,
428428 : MTLWindingCounterClockwise ];
429429 [encoder setCullMode: MTLCullModeNone ];
430430 [encoder setStencilReferenceValue: command.stencil_reference];
431+ if (command.viewport .has_value ()) {
432+ auto v = command.viewport .value ();
433+ MTLViewport viewport = {
434+ .originX = v.rect .origin .x ,
435+ .originY = v.rect .origin .y ,
436+ .width = v.rect .size .width ,
437+ .height = v.rect .size .height ,
438+ .znear = v.znear ,
439+ .zfar = v.zfar ,
440+ };
441+ [encoder setViewport: viewport];
442+ }
443+ if (command.scissor .has_value ()) {
444+ auto s = command.scissor .value ();
445+ MTLScissorRect scissor = {
446+ .x = static_cast <NSUInteger >(s.origin .x ),
447+ .y = static_cast <NSUInteger >(s.origin .y ),
448+ .width = static_cast <NSUInteger >(s.size .width ),
449+ .height = static_cast <NSUInteger >(s.size .height ),
450+ };
451+ [encoder setScissorRect: scissor];
452+ }
431453 if (!bind_stage_resources (command.vertex_bindings , ShaderStage::kVertex )) {
432454 return false ;
433455 }
@@ -472,6 +494,15 @@ static bool Bind(PassBindingsCache& pass,
472494 return false ;
473495 }
474496
497+ if (command.scissor .has_value ()) {
498+ auto target_rect = IRect ({}, render_target_.GetRenderTargetSize ());
499+ if (!target_rect.Contains (command.scissor .value ())) {
500+ VALIDATION_LOG << " Cannot apply a scissor that lies outside the bounds "
501+ " of the render target." ;
502+ return false ;
503+ }
504+ }
505+
475506 commands_.emplace_back (std::move (command));
476507 return true ;
477508}
0 commit comments