-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert new style Pinwheel to use Metal for rendering
- Loading branch information
Showing
9 changed files
with
453 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
|
||
#include "MetalComputeUtilities.hpp" | ||
#include "MetalEffects.hpp" | ||
#include "MetalEffectDataTypes.h" | ||
|
||
#include "../../RenderBuffer.h" | ||
|
||
#include <array> | ||
|
||
class MetalPinwheelEffectData { | ||
public: | ||
MetalPinwheelEffectData() { | ||
for (auto &f : functions) { | ||
f = nil; | ||
} | ||
functions[0] = MetalComputeUtilities::INSTANCE.FindComputeFunction("PinwheelEffectStyle0"); | ||
//functions[1] = MetalComputeUtilities::INSTANCE.FindComputeFunction("PinwheelEffectStyle0"); | ||
//functions[2] = MetalComputeUtilities::INSTANCE.FindComputeFunction("PinwheelEffectStyle0"); | ||
//functions[3] = MetalComputeUtilities::INSTANCE.FindComputeFunction("PinwheelEffectStyle0"); | ||
//functions[4] = MetalComputeUtilities::INSTANCE.FindComputeFunction("PinwheelEffectStyle0"); | ||
//functions[5] = MetalComputeUtilities::INSTANCE.FindComputeFunction("PinwheelEffectStyle0"); | ||
} | ||
~MetalPinwheelEffectData() { | ||
for (auto &f : functions) { | ||
if (f != nil) { | ||
[f release]; | ||
} | ||
} | ||
} | ||
bool canRenderStyle(int style) { | ||
return style < functions.size() && functions[style] != nil; | ||
} | ||
|
||
bool Render(int idx, MetalPinwheelData &data, RenderBuffer &buffer) { | ||
@autoreleasepool { | ||
MetalRenderBufferComputeData * rbcd = MetalRenderBufferComputeData::getMetalRenderBufferComputeData(&buffer); | ||
|
||
id<MTLCommandBuffer> commandBuffer = rbcd->getCommandBuffer(); | ||
if (commandBuffer == nil) { | ||
return false; | ||
} | ||
|
||
id<MTLBuffer> bufferResult = rbcd->getPixelBuffer(); | ||
if (bufferResult == nil) { | ||
rbcd->abortCommandBuffer(); | ||
return false; | ||
} | ||
id<MTLComputeCommandEncoder> computeEncoder = [commandBuffer computeCommandEncoder]; | ||
if (computeEncoder == nil) { | ||
rbcd->abortCommandBuffer(); | ||
return false; | ||
} | ||
[computeEncoder setLabel:@"PinwheelEffect"]; | ||
[computeEncoder setComputePipelineState:functions[idx]]; | ||
|
||
NSInteger dataSize = sizeof(data); | ||
[computeEncoder setBytes:&data length:dataSize atIndex:0]; | ||
[computeEncoder setBuffer:bufferResult offset:0 atIndex:1]; | ||
|
||
NSInteger maxThreads = functions[idx].maxTotalThreadsPerThreadgroup; | ||
dataSize = data.width * data.height; | ||
NSInteger threads = std::min(dataSize, maxThreads); | ||
MTLSize gridSize = MTLSizeMake(dataSize, 1, 1); | ||
MTLSize threadsPerThreadgroup = MTLSizeMake(threads, 1, 1); | ||
|
||
[computeEncoder dispatchThreads:gridSize | ||
threadsPerThreadgroup:threadsPerThreadgroup]; | ||
|
||
[computeEncoder endEncoding]; | ||
} | ||
return true; | ||
} | ||
std::array<id<MTLComputePipelineState>, 11> functions; | ||
}; | ||
|
||
MetalPinwheelEffect::MetalPinwheelEffect(int i) : PinwheelEffect(i) { | ||
data = new MetalPinwheelEffectData(); | ||
} | ||
MetalPinwheelEffect::~MetalPinwheelEffect() { | ||
if (data) { | ||
delete data; | ||
} | ||
} | ||
|
||
void MetalPinwheelEffect::RenderNewArms(RenderBuffer& buffer, PinwheelEffect::PinwheelData &data) { | ||
|
||
MetalRenderBufferComputeData * rbcd = MetalRenderBufferComputeData::getMetalRenderBufferComputeData(&buffer); | ||
if (rbcd == nullptr || ((buffer.BufferWi * buffer.BufferHt) < 1024) || data.hasSpacial) { | ||
PinwheelEffect::RenderNewArms(buffer, data); | ||
return; | ||
} | ||
|
||
MetalPinwheelData rdata; | ||
rdata.width = buffer.BufferWi; | ||
rdata.height = buffer.BufferHt; | ||
rdata.pinwheel_arms = data.pinwheel_arms; | ||
rdata.xc_adj = data.xc_adj; | ||
rdata.yc_adj = data.yc_adj; | ||
rdata.degrees_per_arm = data.degrees_per_arm; | ||
rdata.pinwheel_twist = data.pinwheel_twist; | ||
rdata.max_radius= data.max_radius; | ||
rdata.poffset = data.poffset; | ||
rdata.pw3dType = data.pw3dType; | ||
rdata.pinwheel_rotation = data.pinwheel_rotation; | ||
rdata.tmax = data.tmax; | ||
rdata.pos = data.pos; | ||
rdata.allowAlpha = buffer.allowAlpha; | ||
|
||
rdata.numColors = data.colorsAsColor.size(); | ||
for (int x = 0; x < rdata.numColors; x++) { | ||
rdata.colorsAsColor[x] = data.colorsAsColor[x].asChar4(); | ||
rdata.colorsAsHSV[x] = {(float)data.colorsAsHSV[x].hue, (float)data.colorsAsHSV[x].saturation, (float)data.colorsAsHSV[x].value}; | ||
} | ||
if (this->data->Render(0, rdata, buffer)) { | ||
return; | ||
} | ||
PinwheelEffect::RenderNewArms(buffer, data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
#include <metal_stdlib> | ||
using namespace metal; | ||
|
||
|
||
#include "MetalEffectDataTypes.h" | ||
|
||
constant simd::float4 K = simd::float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); | ||
|
||
uchar4 hsv2rgb(simd::float3 c) { | ||
simd::float3 p = abs(fract(c.xxx + K.xyz) * 6.0h - K.www); | ||
c = c.z * mix(K.xxx, clamp(p - K.xxx, 0.0h, 1.0h), c.y); | ||
return uchar4(c.r * 255.0h, c.g * 255.0h, c.b * 255.0h, 255); | ||
} | ||
|
||
kernel void PinwheelEffectStyle0(constant MetalPinwheelData &data, | ||
device uchar4* result, | ||
uint index [[thread_position_in_grid]]) | ||
{ | ||
if (index > (data.width * data.height)) return; | ||
int x = index % data.width; | ||
int y = index / data.width; | ||
|
||
float y1 = y - data.yc_adj - ((float)data.height / 2.0f); | ||
float x1 = x - data.xc_adj - ((float)data.width / 2.0f); | ||
float r = sqrt((x1*x1) + (y1*y1)); | ||
|
||
simd::float3 hsv; | ||
if (r <= data.max_radius) { | ||
float degrees_twist = (r / data.max_radius) * data.pinwheel_twist; | ||
float theta = (atan2(x1, y1) * 180 / 3.14159) + degrees_twist; | ||
if (data.pinwheel_rotation == 1) { // do we have CW rotation | ||
theta = data.pos + theta + (data.tmax / 2) + data.poffset; | ||
} else { | ||
theta = data.pos - theta + (data.tmax / 2) + data.poffset; | ||
} | ||
theta = theta + 540.0; | ||
int t2 = (int)theta % data.degrees_per_arm; | ||
if (t2 <= data.tmax) { | ||
float round = (float)t2 / (float)data.tmax; | ||
t2 = abs(t2 - (data.tmax / 2)) * 2; | ||
int ColorIdx2 = ((int)((theta / data.degrees_per_arm))) % data.pinwheel_arms; | ||
simd::uchar4 color = data.colorsAsColor[ColorIdx2]; | ||
if (!data.allowAlpha) { | ||
hsv = data.colorsAsHSV[ColorIdx2]; | ||
} | ||
switch (data.pw3dType) { | ||
case 1: | ||
if (data.allowAlpha) { | ||
color.a = 255.0 * ((data.tmax - t2) / data.tmax); | ||
} else { | ||
hsv.z = hsv.z * ((data.tmax - t2) / data.tmax); | ||
color = hsv2rgb(hsv); | ||
} | ||
break; | ||
case 2: | ||
if (data.allowAlpha) { | ||
color.a = 255.0 * ((t2) / data.tmax); | ||
} else { | ||
hsv.z = hsv.z * ((t2) / data.tmax); | ||
color = hsv2rgb(hsv); | ||
} | ||
break; | ||
case 3: | ||
if (data.allowAlpha) { | ||
color.a = 255.0 * (1.0 - round); | ||
} else { | ||
hsv.z = hsv.z * (1.0 - round); | ||
color = hsv2rgb(hsv); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
result[index] = color; | ||
} | ||
} | ||
} |