Merge pull request #1131 from davidgyu/mtl_viewer_options

Thanks, David
This commit is contained in:
Barry Fowler 2019-06-18 16:23:47 -07:00 committed by GitHub
commit 7e229cf095
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 20 deletions

Binary file not shown.

View File

@ -87,12 +87,17 @@ enum {
-(void)keyDown:(NSEvent *)event {
const auto key = [event.charactersIgnoringModifiers characterAtIndex:0];
if(key == '=') {
if (hud.KeyDown(key)) {
return;
} else if(key == '=') {
_controller.osdRenderer.tessellationLevel = std::min(_controller.osdRenderer.tessellationLevel + 1, 16);
} else if (key == '-') {
_controller.osdRenderer.tessellationLevel = std::max(_controller.osdRenderer.tessellationLevel - 1, 0);
} else if(!hud.KeyDown(key))
} else if (key == 'f') {
[_controller.osdRenderer fitFrame];
} else {
[super keyDown:event];
}
}
-(void)scrollWheel:(NSEvent *)event {
@ -300,8 +305,8 @@ enum {
hud.AddCheckBox("Fractional spacing (T)", _osdRenderer.useFractionalTessellation,
10, y, callbackCheckbox, kHUD_CB_FRACTIONAL_SPACING, 't');
y += 20;
hud.AddCheckBox("Frustum Patch Culling (F)", _osdRenderer.usePatchClipCulling,
10, y, callbackCheckbox, kHUD_CB_PATCH_CULL, 'f');
hud.AddCheckBox("Frustum Patch Culling (P)", _osdRenderer.usePatchClipCulling,
10, y, callbackCheckbox, kHUD_CB_PATCH_CULL, 'p');
y += 20;
hud.AddCheckBox("Backface Culling (B)", _osdRenderer.usePatchBackfaceCulling,
10, y, callbackCheckbox, kHUD_CB_BACK_CULL, 'b');
@ -392,7 +397,7 @@ enum {
hud.AddPullDownButton(endcap_pulldown, "Gregory",
kEndCapGregoryBasis,
_osdRenderer.endCapMode == kEndCapGregoryBasis);
if (true || _osdRenderer.legacyGregoryEnabled) {
if (_osdRenderer.legacyGregoryEnabled) {
hud.AddPullDownButton(endcap_pulldown, "LegacyGregory",
kEndCapLegacyGregory,
_osdRenderer.endCapMode == kEndCapLegacyGregory);
@ -402,7 +407,7 @@ enum {
for (int i = 1; i < 11; ++i) {
char level[16];
sprintf(level, "Lv. %d", i);
hud.AddRadioButton(3, level, i==2, 10, 310+i*20, callbackLevel, i, '0'+(i%10));
hud.AddRadioButton(3, level, i==_osdRenderer.refinementLevel, 10, 310+i*20, callbackLevel, i, '0'+(i%10));
}
int shapes_pulldown = hud.AddPullDown("Shape (N)", -300, 10, 300, callbackModel, 'n');

View File

@ -86,6 +86,8 @@ typedef struct {
-(id<MTLRenderCommandEncoder>)drawFrame:(id<MTLCommandBuffer>)commandBuffer;
-(void)fitFrame;
@property (readonly, nonatomic) id<OSDRendererDelegate> delegate;
@property (nonatomic) unsigned refinementLevel;
@ -109,6 +111,7 @@ typedef struct {
@property (nonatomic) bool useStageIn;
@property (nonatomic) bool usePrimitiveBackfaceCulling;
@property (nonatomic) bool useAdaptive;
@property (nonatomic) bool yup;
@property (nonatomic) bool freeze;
@property (nonatomic) bool animateVertices;
@property (nonatomic) bool displayControlMeshEdges;

View File

@ -47,11 +47,13 @@
#import <opensubdiv/osd/mtlComputeEvaluator.h>
#import <opensubdiv/osd/mtlPatchShaderSource.h>
#import "../common/simple_math.h"
#import "../../regression/common/far_utils.h"
#import "init_shapes.h"
#import "../common/argOptions.h"
#import "../common/mtlUtils.h"
#import "../common/mtlControlMeshDisplay.h"
#import "../common/simple_math.h"
#import "../common/viewerArgsUtils.h"
#import "init_shapes.h"
#define VERTEX_BUFFER_INDEX 0
#define PATCH_INDICES_BUFFER_INDEX 1
@ -188,6 +190,7 @@ using PerFrameBuffer = MTLRingBuffer<DataType, FRAME_LAG>;
bool _needsRebuild;
NSString* _osdShaderSource;
simd::float3 _meshCenter;
float _meshSize;
NSMutableArray<NSString*>* _loadedModels;
int _patchCounts[DISPATCHSLOTS];
}
@ -301,6 +304,38 @@ struct PipelineConfig {
return config;
}
-(void)_processArgs {
NSEnumerator *argsArray =
[[[NSProcessInfo processInfo] arguments] objectEnumerator];
std::vector<char *> argsVector;
for (id arg in argsArray) {
argsVector.push_back((char *)[arg UTF8String]);
}
ArgOptions args;
args.Parse(argsVector.size(), argsVector.data());
// Parse remaining args
const std::vector<const char *> &rargs = args.GetRemainingArgs();
for (size_t i = 0; i < rargs.size(); ++i) {
if (!strcmp(rargs[i], "-lg")) {
self.legacyGregoryEnabled = true;
} else {
args.PrintUnrecognizedArgWarning(rargs[i]);
}
}
self.yup = args.GetYUp();
self.useAdaptive = args.GetAdaptive();
self.refinementLevel = args.GetLevel();
ViewerArgsUtils::PopulateShapes(args, &g_defaultShapes);
}
-(instancetype)initWithDelegate:(id<OSDRendererDelegate>)delegate {
self = [super init];
if (self) {
@ -316,12 +351,15 @@ struct PipelineConfig {
self.usePatchBackfaceCulling = false;
self.usePrimitiveBackfaceCulling = false;
self.useAdaptive = true;
self.yup = false;
self.kernelType = kMetal;
self.refinementLevel = 2;
self.tessellationLevel = 1;
self.shadingMode = kShadingPatchType;
self.displayStyle = kDisplayStyleWireOnShaded;
self.legacyGregoryEnabled = true;
self.legacyGregoryEnabled = false;
[self _processArgs];
_frameCount = 0;
_animationFrames = 0;
@ -411,6 +449,10 @@ struct PipelineConfig {
return renderEncoder;
}
-(void)fitFrame {
_cameraData.dollyDistance = _meshSize;
}
-(void)_renderMesh:(id<MTLRenderCommandEncoder>)renderCommandEncoder {
auto patchVertexBuffer = _mesh->BindVertexBuffer();
@ -675,7 +717,6 @@ struct PipelineConfig {
auto shapeDesc = &g_defaultShapes[[_loadedModels indexOfObject:_currentModel]];
_shape.reset(Shape::parseObj(shapeDesc->data.c_str(), shapeDesc->scheme));
const auto scheme = shapeDesc->scheme;
// create Far mesh (topology)
Sdc::SchemeType sdctype = GetSdcType(*_shape);
@ -753,7 +794,6 @@ struct PipelineConfig {
}
_vertexData.resize(refBaseLevel.GetNumVertices() * numElements);
_meshCenter = simd::float3{0,0,0};
for(int i = 0; i < refBaseLevel.GetNumVertices(); ++i)
{
@ -762,14 +802,24 @@ struct PipelineConfig {
_vertexData[i * numElements + 2] = _shape->verts[i * 3 + 2];
}
for(auto vertexIdx = 0; vertexIdx < refBaseLevel.GetNumVertices(); ++vertexIdx)
{
_meshCenter[0] += _vertexData[vertexIdx * numElements + 0];
_meshCenter[1] += _vertexData[vertexIdx * numElements + 1];
_meshCenter[2] += _vertexData[vertexIdx * numElements + 2];
// compute model bounding
float min[3] = { FLT_MAX, FLT_MAX, FLT_MAX};
float max[3] = {-FLT_MAX,-FLT_MAX,-FLT_MAX};
for (int i = 0; i < refBaseLevel.GetNumVertices(); ++i) {
for (int j = 0; j < 3; ++j) {
float v = _vertexData[i*numElements+j];
min[j] = std::min(min[j], v);
max[j] = std::max(max[j], v);
}
}
_meshCenter /= (_shape->verts.size() / 3);
_meshSize = 0.0f;
for (int j = 0; j < 3; ++j) {
_meshCenter[j] = (min[j] + max[j]) * 0.5f;
_meshSize += (max[j]-min[j])*(max[j]-min[j]);
}
_meshSize = sqrt(_meshSize);
_mesh->UpdateVertexBuffer(_vertexData.data(), 0, refBaseLevel.GetNumVertices());
_mesh->Refine();
_mesh->Synchronize();
@ -1306,14 +1356,15 @@ struct PipelineConfig {
translate(pData->ModelViewMatrix, 0, 0, -_cameraData.dollyDistance);
rotate(pData->ModelViewMatrix, _cameraData.rotationY, 1, 0, 0);
rotate(pData->ModelViewMatrix, _cameraData.rotationX, 0, 1, 0);
rotate(pData->ModelViewMatrix, -90, 1, 0, 0); // z-up model
if (!_yup) {
rotate(pData->ModelViewMatrix, -90, 1, 0, 0);
}
translate(pData->ModelViewMatrix, -_meshCenter[0], -_meshCenter[1], -_meshCenter[2]);
inverseMatrix(pData->ModelViewInverseMatrix, pData->ModelViewMatrix);
identity(pData->ProjectionMatrix);
perspective(pData->ProjectionMatrix, 45.0, _cameraData.aspectRatio, 0.01f, 500.0);
multMatrix(pData->ModelViewProjectionMatrix, pData->ModelViewMatrix, pData->ProjectionMatrix);
}
-(void)_initializeBuffers {
@ -1322,9 +1373,9 @@ struct PipelineConfig {
}
-(void)_initializeCamera {
_cameraData.dollyDistance = 4;
_cameraData.rotationY = 0;
_cameraData.rotationX = 0;
_cameraData.dollyDistance = 5;
_cameraData.aspectRatio = 1;
}