Added frame blending

This commit is contained in:
Lior Halphon 2018-06-15 13:58:50 +03:00
parent 5b39cacc8a
commit c1fcd1a0c0
2 changed files with 27 additions and 1 deletions

View File

@ -24,6 +24,7 @@ static const vector_float2 rect[] =
id<MTLBuffer> vertices;
id<MTLRenderPipelineState> pipeline_state;
id<MTLCommandQueue> command_queue;
id<MTLBuffer> mix_previous_buffer;
}
- (void)createInternalView
@ -46,6 +47,12 @@ static const vector_float2 rect[] =
length:sizeof(rect)
options:MTLResourceStorageModeShared];
static const bool default_mix_value = false;
mix_previous_buffer = [device newBufferWithBytes:&default_mix_value
length:sizeof(default_mix_value)
options:MTLResourceStorageModeShared];
[self loadShader];
}
@ -93,12 +100,19 @@ static const vector_float2 rect[] =
mipmapLevel:0
withBytes:[self currentBuffer]
bytesPerRow:PITCH];
if ([self shouldBlendFrameWithPrevious]) {
[previous_texture replaceRegion:region
mipmapLevel:0
withBytes:[self previousBuffer]
bytesPerRow:PITCH];
}
MTLRenderPassDescriptor *render_pass_descriptor = view.currentRenderPassDescriptor;
id<MTLCommandBuffer> command_buffer = [command_queue commandBuffer];
if(render_pass_descriptor != nil)
{
*(bool *)[mix_previous_buffer contents] = [self shouldBlendFrameWithPrevious];
id<MTLRenderCommandEncoder> render_encoder =
[command_buffer renderCommandEncoderWithDescriptor:render_pass_descriptor];
@ -113,9 +127,16 @@ static const vector_float2 rect[] =
offset:0
atIndex:0];
[render_encoder setFragmentBuffer:mix_previous_buffer
offset:0
atIndex:0];
[render_encoder setFragmentTexture:texture
atIndex:0];
[render_encoder setFragmentTexture:previous_texture
atIndex:1];
[render_encoder drawPrimitives:MTLPrimitiveTypeTriangleStrip
vertexStart:0
vertexCount:4];

View File

@ -37,9 +37,14 @@ static inline float4 texture(texture2d<half> texture, float2 pos)
}
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) ]],
constant bool *mix_previous [[ buffer(0) ]])
{
in.texcoords.y = 1 - in.texcoords.y;
if (*mix_previous) {
return mix(texture(image, in.texcoords), texture(previous_image, in.texcoords), 0.5);
}
return texture(image, in.texcoords);
}