Skip to content

Commit

Permalink
First set of feedback changes
Browse files Browse the repository at this point in the history
  • Loading branch information
colincornaby committed Nov 4, 2023
1 parent 36062dd commit 3a4333c
Show file tree
Hide file tree
Showing 25 changed files with 345 additions and 447 deletions.
16 changes: 8 additions & 8 deletions Sources/Plasma/Apps/plClient/Mac-Cocoa/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@

// System Frameworks
#import <Cocoa/Cocoa.h>
#if PLASMA_PIPELINE_GL
#ifdef PLASMA_PIPELINE_GL
#import <OpenGL/gl.h>
#endif
#if PLASMA_PIPELINE_METAL
#ifdef PLASMA_PIPELINE_METAL
#import <Metal/Metal.h>
#endif
#import <QuartzCore/QuartzCore.h>
Expand All @@ -69,11 +69,11 @@
#include "plCmdParser.h"
#include "pfConsoleCore/pfConsoleEngine.h"
#include "pfGameGUIMgr/pfGameGUIMgr.h"
#if PLASMA_PIPELINE_GL
#ifdef PLASMA_PIPELINE_GL
#include "pfGLPipeline/plGLPipeline.h"
#endif
#include "plInputCore/plInputDevice.h"
#if PLASMA_PIPELINE_METAL
#ifdef PLASMA_PIPELINE_METAL
#include "pfMetalPipeline/plMetalPipeline.h"
#endif
#include "plMessage/plDisplayScaleChangedMsg.h"
Expand Down Expand Up @@ -468,12 +468,12 @@ - (void)startClient
gClient.SetClientWindow((hsWindowHndl)(__bridge void*)self.window);
gClient.SetClientDisplay((hsWindowHndl)NULL);

#if PLASMA_PIPELINE_METAL
#ifdef PLASMA_PIPELINE_METAL
plMetalPipeline *pipeline = (plMetalPipeline *)gClient->GetPipeline();
pipeline->currentDrawableCallback = [self] (MTL::Device* device) {
id< CAMetalDrawable > drawable;
id<MTLDevice> metalDevice = (__bridge id<MTLDevice>)device;
if(((CAMetalLayer *) _renderLayer).device != metalDevice) {
if (((CAMetalLayer *) _renderLayer).device != metalDevice) {
((CAMetalLayer *) _renderLayer).device = metalDevice;
dispatch_async(dispatch_get_main_queue(), ^{
[self updateWindowTitle];
Expand All @@ -488,12 +488,12 @@ - (void)startClient
if (!gClient) {
exit(0);
}

self.eventMonitor = [[PLSKeyboardEventMonitor alloc] initWithView:self.window.contentView
inputManager:&gClient];
((PLSView*)self.window.contentView).inputManager = gClient->GetInputManager();
[self.window makeFirstResponder:self.window.contentView];

// Main loop
if (gClient && !gClient->GetDone()) {
[self startRunLoop];
Expand Down
10 changes: 4 additions & 6 deletions Sources/Plasma/FeatureLib/pfMetalPipeline/ShaderSrc/Clear.metal
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,14 @@ vertex ClearVertexOut clearVertex(ClearVertexIn in [[ stage_in ]])
return out;
}

fragment ClearFragmentOut clearFragment(
constant half4& clearColor [[ buffer(0), function_constant(ShouldClearColor) ]],
constant float& clearDepth [[ buffer(1), function_constant(ShouldClearDepth) ]]
)
fragment ClearFragmentOut clearFragment(constant half4& clearColor [[ buffer(0), function_constant(ShouldClearColor) ]],
constant float& clearDepth [[ buffer(1), function_constant(ShouldClearDepth) ]])
{
ClearFragmentOut out;
if(ShouldClearDepth) {
if (ShouldClearDepth) {
out.depth = clearDepth;
}
if(ShouldClearColor) {
if (ShouldClearColor) {
out.color = clearColor;
}
return out;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ struct FragmentShaderArguments {
texturecube<half> cubicTexture8 [[ texture(FragmentShaderArgumentAttributeCubicTextures + 7), function_constant(hasCubicTexture8) ]];
const constant plMetalFragmentShaderArgumentBuffer* bufferedUniforms [[ buffer(FragmentShaderArgumentUniforms) ]];
half4 sampleLayer(const size_t index, const half4 vertexColor, const uint8_t passType, float3 sampleCoord) const;
//number of layers is variable, so have to declare these samplers the ugly way
// number of layers is variable, so have to declare these samplers the ugly way
sampler samplers [[ sampler(0), function_constant(hasLayer1) ]];
sampler sampler2 [[ sampler(1), function_constant(hasLayer2) ]];
sampler sampler3 [[ sampler(2), function_constant(hasLayer3) ]];
Expand All @@ -160,10 +160,8 @@ typedef struct
float3 texCoord6 [[function_constant(hasLayer6)]];
float3 texCoord7 [[function_constant(hasLayer7)]];
float3 texCoord8 [[function_constant(hasLayer8)]];
//float4 normal;
half4 vtxColor [[ centroid_perspective ]];
half4 fogColor;
//float4 vCamNormal;
} ColorInOut;


Expand All @@ -179,31 +177,30 @@ vertex ColorInOut pipelineVertexShader(Vertex in [[stage_in]],
constant float4x4 & blendMatrix1 [[ buffer(VertexShaderArgumentBlendMatrix1), function_constant(temp_hasOnlyWeight1) ]])
{
ColorInOut out;
//we should have been able to swizzle, but it didn't work in Xcode beta? Try again later.
// we should have been able to swizzle, but it didn't work in Xcode beta? Try again later.
const half4 inColor = half4(in.color.b, in.color.g, in.color.r, in.color.a) / half4(255.0f);

const half3 MAmbient = mix(inColor.rgb, uniforms.ambientCol, uniforms.ambientSrc);
const half4 MDiffuse = mix(inColor, uniforms.diffuseCol, uniforms.diffuseSrc);
const half3 MEmissive = mix(inColor.rgb, uniforms.emissiveCol, uniforms.emissiveSrc);
//const half4 MSpecular = half4(mix(inColor, uniforms.specularCol, uniforms.specularSrc));

half3 LAmbient = half3(0.0, 0.0, 0.0);
half3 LDiffuse = half3(0.0, 0.0, 0.0);

const float3 Ndirection = normalize(float4(in.normal, 0.0) * uniforms.localToWorldMatrix).xyz;

float4 position = (float4(in.position, 1.0) * uniforms.localToWorldMatrix);
if(temp_hasOnlyWeight1) {
if (temp_hasOnlyWeight1) {
const float4 position2 = blendMatrix1 * float4(in.position, 1.0);
position = (in.weight1 * position) + ((1.0f - in.weight1) * position2);
}

for (size_t i = 0; i < lights.count; i++) {
constant const plMetalShaderLightSource *lightSource = &lights.lampSources[i];
if(lightSource->scale == 0.0h)
if (lightSource->scale == 0.0h)
continue;

//w is attenation
// w is attenation
float4 direction;

if (lightSource->position.w == 0.0) {
Expand All @@ -220,9 +217,9 @@ vertex ColorInOut pipelineVertexShader(Vertex in [[stage_in]],
if (lightSource->spotProps.x > 0.0) {
// Spot Light with cone falloff
const float theta = dot(direction.xyz, normalize(-lightSource->direction).xyz);
//inner cutoff
// inner cutoff
const float gamma = lightSource->spotProps.y;
//outer cutoff
// outer cutoff
const float phi = lightSource->spotProps.z;
const float epsilon = (gamma - phi);
const float intensity = clamp((theta - phi) / epsilon, 0.0, 1.0);
Expand All @@ -243,14 +240,13 @@ vertex ColorInOut pipelineVertexShader(Vertex in [[stage_in]],

out.vtxColor = half4(material.rgb, abs(uniforms.invVtxAlpha - MDiffuse.a));
const float4 vCamPosition = position * uniforms.worldToCameraMatrix;
//out.vCamNormal = uniforms.worldToCameraMatrix * (uniforms.localToWorldMatrix * float4(in.position, 0.0));

//Fog
// Fog
out.fogColor = uniforms.calcFog(vCamPosition);

const float4 normal = (uniforms.localToWorldMatrix * float4(in.normal, 0.0)) * uniforms.worldToCameraMatrix;

for(size_t layer=0; layer<num_layers; layer++) {
for (size_t layer=0; layer<num_layers; layer++) {
(&out.texCoord1)[layer] = uniforms.sampleLocation(layer, &in.texCoord1, normal, vCamPosition);
}

Expand All @@ -266,7 +262,7 @@ float3 VertexUniforms::sampleLocation(size_t index, thread float3 *texCoords, co
const uint32_t UVWSrc = uvTransforms[index].UVWSrc;
float4x4 matrix = uvTransforms[index].transform;
const uint32_t flags = miscFlags[index];
//Note: If we want to require newer versions of Metal/newer hardware we could pass function pointers instead of doing these ifs.
// Note: If we want to require newer versions of Metal/newer hardware we could pass function pointers instead of doing these ifs.
if (flags & (kMiscUseReflectionXform | kMiscUseRefractionXform)) {
matrix = cameraToWorldMatrix;
matrix[0][3] = matrix[1][3] = matrix[2][3] = 0;
Expand Down Expand Up @@ -398,15 +394,15 @@ half4 VertexUniforms::calcFog(float4 camPosition) constant {

half4 FragmentShaderArguments::sampleLayer(const size_t index, const half4 vertexColor, const uint8_t passType, float3 sampleCoord) const {

if(passType == PassTypeColor) {
if (passType == PassTypeColor) {
return vertexColor;
} else {

if (miscFlags[index] & kMiscPerspProjection) {
sampleCoord.xy /= sampleCoord.z;
}

//do the actual sample
// do the actual sample
if(passType == PassTypeTexture) {
return (&textures)[index].sample((&samplers)[index], sampleCoord.xy);
} else if(passType == PassTypeCubicTexture) {
Expand Down Expand Up @@ -434,13 +430,13 @@ fragment half4 pipelineFragmentShader(ColorInOut in [[stage_in]],
Note: For loop should be unrolled by the compiler, but it is very sensitive.
Always use size_t for the loop interator type.
*/
for(size_t layer=0; layer<passCount; layer++) {
for (size_t layer=0; layer<passCount; layer++) {

float3 sampleCoord = (&in.texCoord1)[layer];

half4 color = fragmentShaderArgs.sampleLayer(layer, in.vtxColor, sourceTypes[layer], sampleCoord);

if(layer==0) {
if (layer==0) {
//only blend if there is a texture to blend into
if(sourceTypes[0] != PassTypeColor) {
blendFirst(color, currentColor, blendModes[layer]);
Expand All @@ -455,7 +451,9 @@ fragment half4 pipelineFragmentShader(ColorInOut in [[stage_in]],

currentColor.rgb = mix(in.fogColor.rgb, currentColor.rgb, (float)clamp(in.fogColor.a, 0.0h, 1.0h));

if (currentColor.a < fragmentShaderArgs.bufferedUniforms->alphaThreshold) { discard_fragment(); }
if (currentColor.a < fragmentShaderArgs.bufferedUniforms->alphaThreshold) {
discard_fragment();
}

return currentColor;
}
Expand Down Expand Up @@ -516,7 +514,7 @@ constexpr void blend(half4 srcSample, thread half4 &destSample, const uint32_t b
srcSample.a = srcSample.a;
}

switch( blendFlags & ( kBlendAlphaAdd | kBlendAlphaMult ) ) {
switch (blendFlags & ( kBlendAlphaAdd | kBlendAlphaMult )) {
case 0:
destSample.a = destSample.a;
break;
Expand Down Expand Up @@ -642,12 +640,12 @@ vertex ColorInOut shadowCastVertexShader(Vertex in [[stage_in]],

const float4 vCamPosition = position * uniforms.worldToCameraMatrix;

//Fog
// Fog
out.fogColor = uniforms.calcFog(vCamPosition);

const float4 normal = (uniforms.localToWorldMatrix * float4(in.normal, 0.0)) * uniforms.worldToCameraMatrix;

for(size_t layer=0; layer<num_layers; layer++) {
for (size_t layer=0; layer<num_layers; layer++) {
(&out.texCoord1)[layer] = uniforms.sampleLocation(layer, &in.texCoord1, normal, vCamPosition);
}

Expand All @@ -663,7 +661,7 @@ fragment half4 shadowCastFragmentShader(ColorInOut in [[stage_in]],
constant int & alphaSrc [[ buffer(FragmentShaderArgumentShadowCastAlphaSrc) ]])
{
float3 sampleCoords = in.texCoord1;
if(fragmentUniforms.pointLightCast) {
if (fragmentUniforms.pointLightCast) {
sampleCoords.xy /= sampleCoords.z;
}
const sampler colorSample = sampler( mag_filter::linear,
Expand All @@ -679,24 +677,24 @@ fragment half4 shadowCastFragmentShader(ColorInOut in [[stage_in]],
currentColor.rgb = (1.0h - LUTColor.rgb) * currentColor.rgb;
currentColor.a = LUTColor.a - currentColor.a;

//only possible alpha sources are layers 0 or 1
if(alphaSrc == 0 && passCount > 0) {
// only possible alpha sources are layers 0 or 1
if (alphaSrc == 0 && passCount > 0) {

half4 layerColor = layers.sampleLayer(0, in.vtxColor,sourceTypes[0], in.texCoord3);

currentColor.rgb *= layerColor.a;
currentColor.rgb *= in.vtxColor.a;
} else if(alphaSrc == 1 && passCount > 1) {
} else if (alphaSrc == 1 && passCount > 1) {

half4 layerColor = layers.sampleLayer(1, in.vtxColor, sourceTypes[1], in.texCoord3);

currentColor.rgb *= layerColor.a;
currentColor.rgb *= in.vtxColor.a;
}

//alpha blend goes here
//a lpha blend goes here

if(currentColor.a <= 0.0h)
if (currentColor.a <= 0.0h)
discard_fragment();

return currentColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ const constant sampler lutSampler = sampler(
filter::nearest
);

fragment half4 gammaCorrectFragment(
GammaVertexOut in [[stage_in]],
texture2d<float> inputTexture [[texture(0)]],
texture1d_array<ushort> LUT [[texture(1)]]
)
fragment half4 gammaCorrectFragment(GammaVertexOut in [[stage_in]],
texture2d<float> inputTexture [[texture(0)]],
texture1d_array<ushort> LUT [[texture(1)]])
{
float4 color = inputTexture.read(ushort2(in.position.xy));
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ using namespace metal;

#include "ShaderVertex.h"

//ignoring the int and pi constants here and using whats built in
//but reserving space for them in the buffer
// ignoring the int and pi constants here and using whats built in
// but reserving space for them in the buffer
typedef struct {
matrix_float4x4 Local2NDC;
float4 intConstants;
Expand Down Expand Up @@ -80,7 +80,7 @@ fragment half4 ps_GrassShader(vs_GrassInOut in [[stage_in]],

half4 out = t0.sample(colorSampler, in.texCoord.xy);
out *= half4(in.color);
if(out.a <= 0.1)
if (out.a <= 0.1)
discard_fragment();
return out;
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ typedef struct

typedef struct
{
//transformation
// transformation
matrix_float4x4 projectionMatrix;
matrix_float4x4 localToWorldMatrix;
matrix_float4x4 cameraToWorldMatrix;
matrix_float4x4 worldToCameraMatrix;

//lighting
// lighting
half4 globalAmb;
half3 ambientCol;
uint8_t ambientSrc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ vertex ColorInOut textFontVertexShader(constant Vertex *in [[ buffer(0) ]],
}

fragment half4 textFontFragmentShader(ColorInOut in [[stage_in]],
texture2d<half> colorMap [[ texture(0) ]])
texture2d<half> colorMap [[ texture(0) ]])
{
constexpr sampler colorSampler(mip_filter::nearest,
mag_filter::nearest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ vertex vs_WaveFixedFin7InOut vs_WaveFixedFin7(Vertex in [[stage_in]],
float3 D = r5.xyz;
float3 F = uniforms.EnvAdjust.xyz;
float G = uniforms.EnvAdjust.w;
//METAL NOTE: HLSL 1.1 always applies an abs operation to values it's about to sqrt
// METAL NOTE: HLSL 1.1 always applies an abs operation to values it's about to sqrt
float3 t = dot(D.xyz, F.xyz) + sqrt(abs(pow(abs(dot(D.xyz, F.xyz)), 2) - G));// r10.z = D dot F + SQRT((D dot F)^2 - G)
r0.xyz = (D * t) - F; // r0.xyz = D * t - (envCenter - camPos)
}
Expand Down
Loading

0 comments on commit 3a4333c

Please sign in to comment.