Skip to content

Commit

Permalink
SGMetal: Update.
Browse files Browse the repository at this point in the history
  • Loading branch information
libobjc committed Jun 27, 2019
1 parent 42a8895 commit 24a0950
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 136 deletions.
80 changes: 40 additions & 40 deletions SGPlayer.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion SGPlayer/Classes/Core/SGMetal/SGMetal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#import "SGMetalTypes.h"
#import "SGMetalRenderer.h"
#import "SGMetalViewport.h"
#import "SGMetalUtilities.h"
#import "SGMetalProjection.h"
#import "SGMetalTextureLoader.h"

#import "SGMetalModel.h"
Expand Down
20 changes: 0 additions & 20 deletions SGPlayer/Classes/Core/SGMetal/SGMetalProjection.h

This file was deleted.

34 changes: 0 additions & 34 deletions SGPlayer/Classes/Core/SGMetal/SGMetalProjection.m

This file was deleted.

5 changes: 3 additions & 2 deletions SGPlayer/Classes/Core/SGMetal/SGMetalRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
// Copyright © 2019 Single. All rights reserved.
//

#import <simd/simd.h>
#import <Metal/Metal.h>
#import "SGMetalModel.h"
#import "SGMetalProjection.h"
#import "SGMetalRenderPipeline.h"

@interface SGMetalRenderer : NSObject

- (instancetype)initWithDevice:(id<MTLDevice>)device;

- (id<MTLCommandBuffer>)drawModel:(SGMetalModel *)model
viewports:(MTLViewport[])viewports
uniforms:(NSArray<id<MTLBuffer>> *)uniforms
pipeline:(SGMetalRenderPipeline *)pipeline
projection:(SGMetalProjection *)projection
inputTextures:(NSArray<id<MTLTexture>> *)inputTextures
outputTexture:(id<MTLTexture>)outputTexture;

Expand Down
23 changes: 11 additions & 12 deletions SGPlayer/Classes/Core/SGMetal/SGMetalRenderer.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
@interface SGMetalRenderer ()

@property (nonatomic, strong) id<MTLDevice> device;
@property (nonatomic, strong) id<MTLBuffer> uniformBuffer;
@property (nonatomic, strong) id<MTLCommandQueue> commandQueue;
@property (nonatomic, strong) MTLRenderPassDescriptor *renderPassDescriptor;

Expand All @@ -25,7 +24,6 @@ - (instancetype)initWithDevice:(id<MTLDevice>)device
if (self = [super init]) {
self.device = device;
self.commandQueue = [self.device newCommandQueue];
self.uniformBuffer = [self.device newBufferWithLength:sizeof(SGMetalUniforms) options:MTLResourceStorageModeShared];
self.renderPassDescriptor = [[MTLRenderPassDescriptor alloc] init];
self.renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 1);
self.renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
Expand All @@ -35,29 +33,30 @@ - (instancetype)initWithDevice:(id<MTLDevice>)device
}

- (id<MTLCommandBuffer>)drawModel:(SGMetalModel *)model
viewports:(MTLViewport[])viewports
uniforms:(NSArray<id<MTLBuffer>> *)uniforms
pipeline:(SGMetalRenderPipeline *)pipeline
projection:(SGMetalProjection *)projection
inputTextures:(NSArray<id<MTLTexture>> *)inputTextures
outputTexture:(id<MTLTexture>)outputTexture
{
SGMetalUniforms *uniform = (SGMetalUniforms*)self.uniformBuffer.contents;
uniform->modelViewProjection = projection.modelViewProjection;
self.renderPassDescriptor.colorAttachments[0].texture = outputTexture;
id<MTLCommandBuffer> commandBuffer = [self.commandQueue commandBuffer];
id<MTLRenderCommandEncoder> encoder = [commandBuffer renderCommandEncoderWithDescriptor:self.renderPassDescriptor];
[encoder setCullMode:MTLCullModeNone];
[encoder setViewport:projection.viewport];
[encoder setRenderPipelineState:pipeline.state];
[encoder setVertexBuffer:model.vertexBuffer offset:0 atIndex:0];
[encoder setVertexBuffer:self.uniformBuffer offset:0 atIndex:1];
for (NSUInteger i = 0; i < inputTextures.count; i++) {
[encoder setFragmentTexture:inputTextures[i] atIndex:i];
}
[encoder drawIndexedPrimitives:model.primitiveType
indexCount:model.indexCount
indexType:model.indexType
indexBuffer:model.indexBuffer
indexBufferOffset:0];
for (NSUInteger i = 0; i < uniforms.count; i++) {
[encoder setViewport:viewports[i]];
[encoder setVertexBuffer:uniforms[i] offset:0 atIndex:1];
[encoder drawIndexedPrimitives:model.primitiveType
indexCount:model.indexCount
indexType:model.indexType
indexBuffer:model.indexBuffer
indexBufferOffset:0];
}
[encoder endEncoding];
return commandBuffer;
}
Expand Down
2 changes: 1 addition & 1 deletion SGPlayer/Classes/Core/SGMetal/SGMetalShader.metal
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ vertex ColorInOut vertexShader( uint vertexID [[ vertex
constant SGMetalUniforms & uniforms [[ buffer(1) ]])
{
ColorInOut out;
out.position = uniforms.modelViewProjection * in[vertexID].position;
out.position = uniforms.mvp * in[vertexID].position;
out.texCoord = in[vertexID].texCoord;
return out;
}
Expand Down
2 changes: 1 addition & 1 deletion SGPlayer/Classes/Core/SGMetal/SGMetalTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef struct {
} SGMetalVertex;

typedef struct {
matrix_float4x4 modelViewProjection;
matrix_float4x4 mvp;
} SGMetalUniforms;

#endif /* SGMetalTypes_h */
25 changes: 25 additions & 0 deletions SGPlayer/Classes/Core/SGMetal/SGMetalViewport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// SGMetalViewport.h
// SGPlayer
//
// Created by Single on 2019/6/27.
// Copyright © 2019 single. All rights reserved.
//

#import <Metal/Metal.h>

typedef NS_ENUM(int, SGMetalViewportMode) {
SGMetalViewportModeResize,
SGMetalViewportModeResizeAspect,
SGMetalViewportModeResizeAspectFill,
};

@interface SGMetalViewport : NSObject

+ (MTLViewport)viewportWithLayerSize:(MTLSize)layerSize;
+ (MTLViewport)viewportWithLayerSizeForLeft:(MTLSize)layerSize;
+ (MTLViewport)viewportWithLayerSizeForRight:(MTLSize)layerSize;
+ (MTLViewport)viewportWithLayerSize:(MTLSize)layerSize textureSize:(MTLSize)textureSize mode:(SGMetalViewportMode)mode;


@end
81 changes: 81 additions & 0 deletions SGPlayer/Classes/Core/SGMetal/SGMetalViewport.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// SGMetalViewport.m
// SGPlayer
//
// Created by Single on 2019/6/27.
// Copyright © 2019 single. All rights reserved.
//

#import "SGMetalViewport.h"

@implementation SGMetalViewport

+ (MTLViewport)viewportWithLayerSize:(MTLSize)layerSize
{
MTLViewport viewport = {0, 0, layerSize.width, layerSize.height, 0, 0};
return viewport;
}

+ (MTLViewport)viewportWithLayerSizeForLeft:(MTLSize)layerSize
{
MTLViewport viewport = {0, 0, layerSize.width / 2, layerSize.height, 0, 0};
return viewport;
}

+ (MTLViewport)viewportWithLayerSizeForRight:(MTLSize)layerSize
{
MTLViewport viewport = {layerSize.width / 2, 0, layerSize.width / 2, layerSize.height, 0, 0};
return viewport;
}

+ (MTLViewport)viewportWithLayerSize:(MTLSize)layerSize textureSize:(MTLSize)textureSize mode:(SGMetalViewportMode)mode
{
MTLViewport viewport = {0, 0, layerSize.width, layerSize.height, 0, 0};
switch (mode) {
case SGMetalViewportModeResize:
break;
case SGMetalViewportModeResizeAspect: {
double layerAspect = (double)layerSize.width / layerSize.height;
double textureAspect = (double)textureSize.width / textureSize.height;
if (fabs(layerAspect - textureAspect) <= 0.0001) {

} else if (layerAspect < textureAspect) {
double height = layerSize.width / textureAspect;
viewport.originX = 0;
viewport.originY = (layerSize.height - height) / 2;
viewport.width = layerSize.width;
viewport.height = height;
} else if (layerAspect > textureAspect) {
double width = layerSize.height * textureAspect;
viewport.originX = (layerSize.width - width) / 2;
viewport.originY = 0;
viewport.width = width;
viewport.height = layerSize.height;
}
}
break;
case SGMetalViewportModeResizeAspectFill: {
double layerAspect = (double)layerSize.width / layerSize.height;
double textureAspect = (double)textureSize.width / textureSize.height;
if (fabs(layerAspect - textureAspect) <= 0.0001) {

} else if (layerAspect < textureAspect) {
double width = layerSize.height * textureAspect;
viewport.originX = (layerSize.width - width) / 2;
viewport.originY = 0;
viewport.width = width;
viewport.height = layerSize.height;
} else if (layerAspect > textureAspect) {
double height = layerSize.width / textureAspect;
viewport.originX = 0;
viewport.originY = (layerSize.height - height) / 2;
viewport.width = layerSize.width;
viewport.height = height;
}
}
break;
}
return viewport;
}

@end
Loading

0 comments on commit 24a0950

Please sign in to comment.