Skip to content

Commit 1cc912b

Browse files
authored
Merge pull request #21 from aras-p/update-2023
Update various projects to current versions
2 parents 3e8c668 + e793e9e commit 1cc912b

37 files changed

+792
-1010
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- uses: actions/checkout@v1
1717
- name: Windows VS2019
1818
run: |
19-
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && msbuild.exe Cpp/Windows/TestCpu.sln /p:Configuration=Release /p:CL_MPCount=2
19+
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat" && msbuild.exe Cpp/Windows/ToyPathTracer.sln /p:Configuration=Release /p:CL_MPCount=2
2020
shell: cmd
2121

2222
apple:
@@ -29,6 +29,6 @@ jobs:
2929
- name: macOS Xcode 13
3030
run: |
3131
echo "**** Building for macOS..."
32-
xcodebuild -project Cpp/Apple/Test.xcodeproj -configuration Release -scheme 'Test Mac' build
32+
xcodebuild -project Cpp/Apple/ToyPathTracer.xcodeproj -configuration Release -scheme 'ToyPathTracerMac' build
3333
echo "**** Building for iOS..."
34-
xcodebuild -project Cpp/Apple/Test.xcodeproj -configuration Release -scheme 'Test iOS' build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGN_ENTITLEMENTS="" CODE_SIGNING_ALLOWED="NO"
34+
xcodebuild -project Cpp/Apple/ToyPathTracer.xcodeproj -configuration Release -scheme 'ToyPathTraceriOS' build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGN_ENTITLEMENTS="" CODE_SIGNING_ALLOWED="NO"

Cpp/Apple/Renderer.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
#import <MetalKit/MetalKit.h>
2-
1+
#import <MetalKit/MetalKit.h>
2+
33
@interface Renderer : NSObject <MTKViewDelegate>
44

55
#if TARGET_OS_IPHONE
6-
-(nonnull instancetype)initWithMetalKitView:(nonnull MTKView *)view;
6+
-(nonnull instancetype)initWithMetalKitView:(nonnull MTKView *)view withLabel:(nonnull UILabel*) label;
77
#else
88
-(nonnull instancetype)initWithMetalKitView:(nonnull MTKView *)view withLabel:(nonnull NSTextField*) label;
99
#endif
10-
11-
@end
10+
11+
-(void)toggleGPU;
12+
-(void)toggleAnimation;
13+
-(void)toggleProgressive;
14+
15+
@end

Cpp/Apple/Renderer.mm

Lines changed: 83 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "../Source/Maths.h"
77
#include "../Source/Test.h"
88

9-
109
static const NSUInteger kMaxBuffersInFlight = 3;
1110

1211
#if TARGET_OS_IPHONE
@@ -15,7 +14,6 @@
1514
#define kMetalBufferMode MTLResourceStorageModeManaged
1615
#endif
1716

18-
#if DO_COMPUTE_GPU
1917
// Metal on Mac needs buffer offsets to be 256-byte aligned
2018
static int AlignedSize(int sz)
2119
{
@@ -34,7 +32,6 @@ static int AlignedSize(int sz)
3432
float lerpFac;
3533
int emissiveCount;
3634
};
37-
#endif
3835

3936

4037
@implementation Renderer
@@ -45,7 +42,8 @@ @implementation Renderer
4542

4643
id <MTLRenderPipelineState> _pipelineState;
4744
id <MTLDepthStencilState> _depthState;
48-
#if DO_COMPUTE_GPU
45+
46+
// GPU tracing things:
4947
id <MTLComputePipelineState> _computeState;
5048
// all the data in separate buffers
5149
id <MTLBuffer> _computeSpheres;
@@ -57,30 +55,29 @@ @implementation Renderer
5755
int _objSize;
5856
int _matSize;
5957
int _uniformBufferIndex;
60-
#endif
6158

6259
id <MTLTexture> _backbuffer, _backbuffer2;
6360
int _backbufferIndex;
6461
float* _backbufferPixels;
6562

6663
mach_timebase_info_data_t _clock_timebase;
67-
#if !TARGET_OS_IPHONE
64+
#if TARGET_OS_IPHONE
65+
UILabel* _label;
66+
#else
6867
NSTextField* _label;
6968
#endif
7069
}
7170

7271
#if TARGET_OS_IPHONE
73-
-(nonnull instancetype)initWithMetalKitView:(nonnull MTKView *)view;
72+
-(nonnull instancetype)initWithMetalKitView:(nonnull MTKView *)view withLabel:(nonnull UILabel*) label;
7473
#else
7574
-(nonnull instancetype)initWithMetalKitView:(nonnull MTKView *)view withLabel:(nonnull NSTextField*) label;
7675
#endif
7776
{
7877
self = [super init];
7978
if(self)
8079
{
81-
#if !TARGET_OS_IPHONE
8280
_label = label;
83-
#endif
8481
_device = view.device;
8582
printf("GPU: %s\n", [[_device name] UTF8String]);
8683
_inFlightSemaphore = dispatch_semaphore_create(kMaxBuffersInFlight);
@@ -102,12 +99,12 @@ - (void)_loadMetalWithView:(nonnull MTKView *)view;
10299
id<MTLLibrary> defaultLibrary = [_device newDefaultLibrary];
103100
id <MTLFunction> vertexFunction = [defaultLibrary newFunctionWithName:@"vertexShader"];
104101
id <MTLFunction> fragmentFunction = [defaultLibrary newFunctionWithName:@"fragmentShader"];
105-
#if DO_COMPUTE_GPU
102+
103+
// GPU tracing things:
106104
id <MTLFunction> computeFunction = [defaultLibrary newFunctionWithName:@"TraceGPU"];
107105
_computeState = [_device newComputePipelineStateWithFunction:computeFunction error:&error];
108106
if (!_computeState)
109107
NSLog(@"Failed to created compute pipeline state, error %@", error);
110-
111108
int camSize;
112109
GetObjectCount(_sphereCount, _objSize, _matSize, camSize);
113110
assert(_objSize == 20);
@@ -119,7 +116,6 @@ - (void)_loadMetalWithView:(nonnull MTKView *)view;
119116
_computeEmissives = [_device newBufferWithLength:AlignedSize(_sphereCount*4)*kMaxBuffersInFlight options:kMetalBufferMode];
120117
_computeCounter = [_device newBufferWithLength:AlignedSize(4)*kMaxBuffersInFlight options:MTLStorageModeShared];
121118
_uniformBufferIndex = 0;
122-
#endif
123119

124120
MTLRenderPipelineDescriptor *pipelineStateDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
125121
pipelineStateDescriptor.sampleCount = view.sampleCount;
@@ -144,10 +140,7 @@ - (void)_loadMetalWithView:(nonnull MTKView *)view;
144140

145141
MTLTextureDescriptor* desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA32Float width:kBackbufferWidth height:kBackbufferHeight mipmapped:NO];
146142
desc.usage = MTLTextureUsageShaderRead;
147-
#if DO_COMPUTE_GPU
148143
desc.usage |= MTLTextureUsageShaderWrite;
149-
desc.storageMode = MTLStorageModePrivate;
150-
#endif
151144

152145
_backbuffer = [_device newTextureWithDescriptor:desc];
153146
_backbuffer2 = [_device newTextureWithDescriptor:desc];
@@ -165,22 +158,13 @@ - (void)_loadAssets
165158
static uint64_t _computeStartTime;
166159
static uint64_t _computeDur;
167160
static size_t rayCounter = 0;
168-
unsigned g_TestFlags = kFlagProgressive;
161+
unsigned g_TestFlags = kFlagProgressive | kFlagAnimate;
162+
static bool g_UseGPU = true;
163+
static int totalCounter = 0;
164+
static int frameCounter = 0;
169165

170-
- (void)_doRenderingWith:(id <MTLCommandBuffer>) cmd;
166+
- (void)_drawTestGpu:(id <MTLCommandBuffer>) cmd;
171167
{
172-
static int totalCounter = 0;
173-
static int frameCounter = 0;
174-
static uint64_t frameTime = 0;
175-
uint64_t time1 = mach_absolute_time();
176-
_computeStartTime = time1;
177-
178-
uint64_t curNs = (time1 * _clock_timebase.numer) / _clock_timebase.denom;
179-
float curT = float(curNs * 1.0e-9f);
180-
181-
UpdateTest(curT, totalCounter, kBackbufferWidth, kBackbufferHeight, g_TestFlags);
182-
183-
#if DO_COMPUTE_GPU
184168
_backbufferIndex = 1-_backbufferIndex;
185169
_uniformBufferIndex = (_uniformBufferIndex + 1) % kMaxBuffersInFlight;
186170
uint8_t* dataSpheres = (uint8_t*)[_computeSpheres contents];
@@ -227,40 +211,62 @@ - (void)_doRenderingWith:(id <MTLCommandBuffer>) cmd;
227211
MTLSize groupCount = {kBackbufferWidth/groupSize.width, kBackbufferHeight/groupSize.height, 1};
228212
[enc dispatchThreadgroups:groupCount threadsPerThreadgroup:groupSize];
229213
[enc endEncoding];
230-
#else
231-
int rayCount;
232-
DrawTest(curT, totalCounter, kBackbufferWidth, kBackbufferHeight, _backbufferPixels, rayCount, g_TestFlags);
233-
rayCounter += rayCount;
234-
#endif
214+
}
215+
216+
- (void)_doRenderingWith:(id <MTLCommandBuffer>) cmd;
217+
{
218+
static uint64_t frameTime = 0;
219+
uint64_t time1 = mach_absolute_time();
220+
_computeStartTime = time1;
221+
222+
uint64_t curNs = (time1 * _clock_timebase.numer) / _clock_timebase.denom;
223+
float curT = float(curNs * 1.0e-9f);
224+
225+
UpdateTest(curT, totalCounter, kBackbufferWidth, kBackbufferHeight, g_TestFlags);
226+
227+
if (g_UseGPU)
228+
{
229+
[self _drawTestGpu: cmd];
230+
}
231+
else
232+
{
233+
int rayCount;
234+
DrawTest(curT, totalCounter, kBackbufferWidth, kBackbufferHeight, _backbufferPixels, rayCount, g_TestFlags);
235+
rayCounter += rayCount;
236+
}
235237

236238
uint64_t time2 = mach_absolute_time();
237239
(void)time2;
238240
++frameCounter;
239241
++totalCounter;
240-
#if !DO_COMPUTE_GPU
241-
frameTime += (time2-time1);
242-
#else
243-
frameTime += _computeDur;
244-
#endif
242+
if (g_UseGPU)
243+
frameTime += _computeDur;
244+
else
245+
frameTime += (time2-time1);
245246
if (frameCounter > 10)
246247
{
247248
uint64_t ns = (frameTime * _clock_timebase.numer) / _clock_timebase.denom;
248249
float s = (float)(ns * 1.0e-9) / frameCounter;
249-
char buffer[200];
250-
snprintf(buffer, 200, "%.2fms (%.1f FPS) %.1fMrays/s %.2fMrays/frame frames %i", s * 1000.0f, 1.f / s, rayCounter / frameCounter / s * 1.0e-6f, rayCounter / frameCounter * 1.0e-6f, totalCounter);
250+
char buffer[500];
251+
snprintf(buffer, 200, "%s: %.2fms (%.1f FPS) %.1fMrays/s %.2fMrays/frame frames %i",
252+
g_UseGPU ? "GPU" : "CPU",
253+
s * 1000.0f, 1.f / s, rayCounter / frameCounter / s * 1.0e-6f, rayCounter / frameCounter * 1.0e-6f, totalCounter);
251254
puts(buffer);
252-
#if !TARGET_OS_IPHONE
253255
NSString* str = [[NSString alloc] initWithUTF8String:buffer];
256+
#if TARGET_OS_IPHONE
257+
_label.text = str;
258+
#else
254259
_label.stringValue = str;
255260
#endif
256261
frameCounter = 0;
257262
frameTime = 0;
258263
rayCounter = 0;
259264
}
260-
261-
#if !DO_COMPUTE_GPU
262-
[_backbuffer replaceRegion:MTLRegionMake2D(0,0,kBackbufferWidth,kBackbufferHeight) mipmapLevel:0 withBytes:_backbufferPixels bytesPerRow:kBackbufferWidth*16];
263-
#endif
265+
266+
if (!g_UseGPU)
267+
{
268+
[_backbuffer replaceRegion:MTLRegionMake2D(0,0,kBackbufferWidth,kBackbufferHeight) mipmapLevel:0 withBytes:_backbufferPixels bytesPerRow:kBackbufferWidth*16];
269+
}
264270
}
265271

266272
- (void)drawInMTKView:(nonnull MTKView *)view
@@ -270,22 +276,21 @@ - (void)drawInMTKView:(nonnull MTKView *)view
270276
id <MTLCommandBuffer> cmd = [_commandQueue commandBuffer];
271277

272278
__block dispatch_semaphore_t block_sema = _inFlightSemaphore;
273-
#if DO_COMPUTE_GPU
274279
int counterIndex = (_uniformBufferIndex+1)%kMaxBuffersInFlight;
275280
id <MTLBuffer> counterBuffer = _computeCounter;
276-
#endif
277281
[cmd addCompletedHandler:^(id<MTLCommandBuffer> buffer)
278282
{
279-
#if DO_COMPUTE_GPU
280-
// There's no easy/proper way to do GPU timing on Metal (or at least I couldn't find any),
281-
// so I'm timing CPU side, from beginning of command buffer invocation to when we get the
282-
// callback that the GPU is done with it. Not 100% proper, but gets similar results to
283-
// what Xcode reports for the GPU duration.
284-
uint64_t time2 = mach_absolute_time();
285-
_computeDur = (time2 - _computeStartTime);
286-
int rayCount = *(const int*)(((const uint8_t*)[counterBuffer contents]) + counterIndex*AlignedSize(4));
287-
rayCounter += rayCount;
288-
#endif
283+
if (g_UseGPU)
284+
{
285+
// There's no easy/proper way to do GPU timing on Metal (or at least I couldn't find any),
286+
// so I'm timing CPU side, from beginning of command buffer invocation to when we get the
287+
// callback that the GPU is done with it. Not 100% proper, but gets similar results to
288+
// what Xcode reports for the GPU duration.
289+
uint64_t time2 = mach_absolute_time();
290+
_computeDur = (time2 - _computeStartTime);
291+
int rayCount = *(const int*)(((const uint8_t*)[counterBuffer contents]) + counterIndex*AlignedSize(4));
292+
rayCounter += rayCount;
293+
}
289294

290295
dispatch_semaphore_signal(block_sema);
291296
}];
@@ -315,4 +320,24 @@ - (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size
315320
//printf("View size %ix%i\n", (int)size.width, (int)size.height);
316321
}
317322

323+
-(void)toggleGPU
324+
{
325+
g_UseGPU = !g_UseGPU;
326+
frameCounter = 0;
327+
totalCounter = 0;
328+
}
329+
-(void)toggleAnimation
330+
{
331+
g_TestFlags ^= kFlagAnimate;
332+
frameCounter = 0;
333+
totalCounter = 0;
334+
}
335+
-(void)toggleProgressive
336+
{
337+
g_TestFlags ^= kFlagProgressive;
338+
frameCounter = 0;
339+
totalCounter = 0;
340+
}
341+
342+
318343
@end
File renamed without changes.

0 commit comments

Comments
 (0)