Output resolution parameter

This commit is contained in:
Lior Halphon 2018-06-15 18:44:22 +03:00
parent da7c32cb10
commit 4466a55de6
2 changed files with 18 additions and 4 deletions

View File

@ -25,6 +25,8 @@ static const vector_float2 rect[] =
id<MTLRenderPipelineState> pipeline_state; id<MTLRenderPipelineState> pipeline_state;
id<MTLCommandQueue> command_queue; id<MTLCommandQueue> command_queue;
id<MTLBuffer> mix_previous_buffer; id<MTLBuffer> mix_previous_buffer;
id<MTLBuffer> output_resolution_buffer;
vector_float2 output_resolution;
} }
- (void)createInternalView - (void)createInternalView
@ -47,12 +49,15 @@ static const vector_float2 rect[] =
length:sizeof(rect) length:sizeof(rect)
options:MTLResourceStorageModeShared]; options:MTLResourceStorageModeShared];
static const bool default_mix_value = false; static const bool default_mix_value = false;
mix_previous_buffer = [device newBufferWithBytes:&default_mix_value mix_previous_buffer = [device newBufferWithBytes:&default_mix_value
length:sizeof(default_mix_value) length:sizeof(default_mix_value)
options:MTLResourceStorageModeShared]; options:MTLResourceStorageModeShared];
output_resolution_buffer = [device newBufferWithBytes:&default_mix_value
length:sizeof(default_mix_value)
options:MTLResourceStorageModeShared];
[self loadShader]; [self loadShader];
} }
@ -92,6 +97,7 @@ static const vector_float2 rect[] =
- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size - (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size
{ {
output_resolution = (vector_float2){size.width, size.height};
} }
- (void)drawInMTKView:(nonnull MTKView *)view - (void)drawInMTKView:(nonnull MTKView *)view
@ -113,12 +119,14 @@ static const vector_float2 rect[] =
if(render_pass_descriptor != nil) if(render_pass_descriptor != nil)
{ {
*(bool *)[mix_previous_buffer contents] = [self shouldBlendFrameWithPrevious]; *(bool *)[mix_previous_buffer contents] = [self shouldBlendFrameWithPrevious];
*(vector_float2 *)[output_resolution_buffer contents] = output_resolution;
id<MTLRenderCommandEncoder> render_encoder = id<MTLRenderCommandEncoder> render_encoder =
[command_buffer renderCommandEncoderWithDescriptor:render_pass_descriptor]; [command_buffer renderCommandEncoderWithDescriptor:render_pass_descriptor];
[render_encoder setViewport:(MTLViewport){0.0, 0.0, [render_encoder setViewport:(MTLViewport){0.0, 0.0,
view.bounds.size.width * view.window.backingScaleFactor, output_resolution.x,
view.bounds.size.height * view.window.backingScaleFactor, output_resolution.y,
-1.0, 1.0}]; -1.0, 1.0}];
[render_encoder setRenderPipelineState:pipeline_state]; [render_encoder setRenderPipelineState:pipeline_state];
@ -131,6 +139,10 @@ static const vector_float2 rect[] =
offset:0 offset:0
atIndex:0]; atIndex:0];
[render_encoder setFragmentBuffer:output_resolution_buffer
offset:0
atIndex:1];
[render_encoder setFragmentTexture:texture [render_encoder setFragmentTexture:texture
atIndex:0]; atIndex:0];

View File

@ -36,10 +36,12 @@ static inline float4 texture(texture2d<half> texture, float2 pos)
return float4(texture.sample(texture_sampler, pos)); return float4(texture.sample(texture_sampler, pos));
} }
fragment float4 fragment_shader(rasterizer_data in [[stage_in]], fragment float4 fragment_shader(rasterizer_data in [[stage_in]],
texture2d<half> image [[ texture(0) ]], texture2d<half> image [[ texture(0) ]],
texture2d<half> previous_image [[ texture(1) ]], texture2d<half> previous_image [[ texture(1) ]],
constant bool *mix_previous [[ buffer(0) ]]) constant bool *mix_previous [[ buffer(0) ]],
constant float2 *output_resolution [[ buffer(1) ]])
{ {
in.texcoords.y = 1 - in.texcoords.y; in.texcoords.y = 1 - in.texcoords.y;
if (*mix_previous) { if (*mix_previous) {