diff --git a/Sources/GateEngine/System/Rendering/SystemShaders.swift b/Sources/GateEngine/System/Rendering/SystemShaders.swift index b99bea31..b1de63e9 100644 --- a/Sources/GateEngine/System/Rendering/SystemShaders.swift +++ b/Sources/GateEngine/System/Rendering/SystemShaders.swift @@ -79,8 +79,7 @@ extension FragmentShader { public static let textureSample: FragmentShader = { let fsh = FragmentShader() fsh.output.color = fsh.channel(0).texture.sample( - at: fsh.input["texCoord0"], - filter: .nearest + at: fsh.input["texCoord0"] ) return fsh }() @@ -88,8 +87,7 @@ extension FragmentShader { public static let textureSampleDiscardZeroAlpha: FragmentShader = { let fsh = FragmentShader() let sample = fsh.channel(0).texture.sample( - at: fsh.input["texCoord0"], - filter: .nearest + at: fsh.input["texCoord0"] ) fsh.output.color = sample.discard(if: sample.a <= 0) return fsh @@ -99,8 +97,7 @@ extension FragmentShader { let fsh = FragmentShader() let opacity: Scalar = fsh.uniforms["opacity"] fsh.output.color = fsh.channel(0).texture.sample( - at: fsh.input["texCoord0"], - filter: .nearest + at: fsh.input["texCoord0"] ) * opacity return fsh }() @@ -121,8 +118,7 @@ extension FragmentShader { public static let textureSampleTintColor: FragmentShader = { let fsh = FragmentShader() let sample = fsh.channel(0).texture.sample( - at: fsh.input["texCoord0"], - filter: .nearest + at: fsh.input["texCoord0"] ) fsh.output.color = sample * fsh.channel(0).color return fsh @@ -135,7 +131,7 @@ extension FragmentShader { let fsh = FragmentShader() let factor: Scalar = fsh.uniforms["factor"] let sample1 = fsh.channel(0).texture.sample(at: fsh.input["texCoord0"]) - let sample2 = fsh.channel(1).texture.sample(at: fsh.input["texCoord1"], filter: .nearest) + let sample2 = fsh.channel(1).texture.sample(at: fsh.input["texCoord1"]) fsh.output.color = sample1.lerp(to: sample2, factor: factor) return fsh }() diff --git a/Sources/Shaders/Generators/CodeGenerator.swift b/Sources/Shaders/Generators/CodeGenerator.swift index 5cd63b92..b441a8cc 100644 --- a/Sources/Shaders/Generators/CodeGenerator.swift +++ b/Sources/Shaders/Generators/CodeGenerator.swift @@ -130,7 +130,7 @@ public class CodeGenerator { case .lerp(let factor): self.declareVariableIfNeeded(factor) mainOutput += scopeIndentation + "\(type(for: value)) \(variable(for: value)) = " + function(value: value, operation: operation) + ";\n" - case .add, .subtract, .multiply, .divide, .compare(_), .sampler2D(_): + case .add, .subtract, .multiply, .divide, .compare(_), .sampler2D: self.declareVariableIfNeeded(operation.value1) self.declareVariableIfNeeded(operation.value2) mainOutput += scopeIndentation + "\(type(for: value)) \(variable(for: value)) = " + function(value: value, operation: operation) + ";\n" @@ -305,7 +305,7 @@ public class CodeGenerator { fatalError() case .branch(comparing: _): fatalError() - case .sampler2D(filter: _): + case .sampler2D: fatalError() case .sampler2DSize: fatalError() @@ -410,6 +410,21 @@ public class CodeGenerator { preconditionFailure("Must override") } + func materialIndex(for variable: some ShaderValue) -> UInt8 { + switch variable.valueRepresentation { + case let .channelAttachment(index: index): + return index + case let .channelScale(index): + return index + case let .channelOffset(index): + return index + case let .channelColor(index): + return index + default: + fatalError() + } + } + final func generateMain(from vertexShader: VertexShader) { pushScope() if let position = vertexShader.output.position { diff --git a/Sources/Shaders/Generators/GLSLCodeGenerator.swift b/Sources/Shaders/Generators/GLSLCodeGenerator.swift index 60a6d8f4..25c1e230 100644 --- a/Sources/Shaders/Generators/GLSLCodeGenerator.swift +++ b/Sources/Shaders/Generators/GLSLCodeGenerator.swift @@ -151,7 +151,7 @@ public final class GLSLCodeGenerator: CodeGenerator { fatalError() case .discard(comparing: _): return "discard" - case .sampler2D(filter: _): + case .sampler2D: return "texture(" + variable(for: operation.value1) + "," + variable(for: operation.value2) + ")" case .sampler2DSize: return "\(scopeIndentation)\(variable(for: value)) = textureSize(\(variable(for: operation.value1)),0);\n" diff --git a/Sources/Shaders/Generators/HLSLCodeGenerator.swift b/Sources/Shaders/Generators/HLSLCodeGenerator.swift index d7a85a93..011a7ec7 100644 --- a/Sources/Shaders/Generators/HLSLCodeGenerator.swift +++ b/Sources/Shaders/Generators/HLSLCodeGenerator.swift @@ -163,8 +163,8 @@ public final class HLSLCodeGenerator: CodeGenerator { fatalError() case .discard(comparing: _): return "discard" - case let .sampler2D(filter: filter): - return "\(variable(for: operation.value1)).Sample(\(filter == .nearest ? "nearestSampler" : "linearSampler"),\(variable(for: operation.value2)))" + case .sampler2D: + return "\(variable(for: operation.value1)).Sample(materials[\(materialIndex(for: operation.value1))].sampleFilter == 2 ? nearestSampler : linearSampler),\(variable(for: operation.value2)))" case .sampler2DSize: return "\(scopeIndentation)\(variable(for: operation.value1)).GetDimensions(\(variable(for: value)).x, \(variable(for: value)).y;\n" case let .lerp(factor: factor): diff --git a/Sources/Shaders/Generators/MSLCodeGenerator.swift b/Sources/Shaders/Generators/MSLCodeGenerator.swift index 1397cd0e..af28c097 100644 --- a/Sources/Shaders/Generators/MSLCodeGenerator.swift +++ b/Sources/Shaders/Generators/MSLCodeGenerator.swift @@ -133,8 +133,8 @@ public final class MSLCodeGenerator: CodeGenerator { fatalError() case .switch(cases: _): fatalError() - case let .sampler2D(filter: filter): - return "\(variable(for: operation.value1)).sample(\(filter == .nearest ? "nearestSampler" : "linearSampler"),\(variable(for: operation.value2)))" + case .sampler2D: + return "\(variable(for: operation.value1)).sample(materials[\(materialIndex(for: operation.value1))].sampleFilter == 2 ? nearestSampler : linearSampler,\(variable(for: operation.value2)))" case .sampler2DSize: return """ \(scopeIndentation)\(variable(for: value)).x = \(variable(for: operation.value1)).get_width(); @@ -210,10 +210,7 @@ public final class MSLCodeGenerator: CodeGenerator { var fragmentTextureList: String = "" for index in fragmentShader.channels.indices { - if fragmentTextureList.isEmpty == false { - fragmentTextureList += ",\n" - } - fragmentTextureList += " texture2d tex\(index) [[texture(\(index))]]" + fragmentTextureList += ",\n texture2d tex\(index) [[texture(\(index))]]" } return """ @@ -259,8 +256,7 @@ fragment \(type(for: .float4)) fragment\(UInt(bitPattern: fragmentShader.id.hash constant Uniforms & uniforms [[buffer(0)]], constant Material *materials [[buffer(1)]], sampler linearSampler [[sampler(0)]], - sampler nearestSampler [[sampler(1)]], -\(fragmentTextureList)) { + sampler nearestSampler [[sampler(1)]]\(fragmentTextureList)) { \(type(for: .float4)) \(variable(for: .fragmentOutColor)); \(fragmentMain) return \(variable(for: .fragmentOutColor)); diff --git a/Sources/Shaders/ShaderDocument/Values/Operation.swift b/Sources/Shaders/ShaderDocument/Values/Operation.swift index 655251d9..e2994e78 100644 --- a/Sources/Shaders/ShaderDocument/Values/Operation.swift +++ b/Sources/Shaders/ShaderDocument/Values/Operation.swift @@ -56,7 +56,7 @@ public final class Operation: ShaderElement { case branch(comparing: Scalar) case `switch`(cases: [_SwitchCase]) case discard(comparing: Scalar) - case sampler2D(filter: Sampler2D.Filter) + case sampler2D case sampler2DSize case lerp(factor: Scalar) @@ -80,8 +80,8 @@ public final class Operation: ShaderElement { var values: [Int] = [5_106] values.append(contentsOf: comparing.documentIdentifierInputData()) return values - case .sampler2D(filter: let filter): - return [5_107, filter.identifier] + case .sampler2D: + return [5_107] case .sampler2DSize: return [5_108] case .lerp(factor: let factor): diff --git a/Sources/Shaders/ShaderDocument/Values/Sampler2D.swift b/Sources/Shaders/ShaderDocument/Values/Sampler2D.swift index 8ec9313c..1534e830 100644 --- a/Sources/Shaders/ShaderDocument/Values/Sampler2D.swift +++ b/Sources/Shaders/ShaderDocument/Values/Sampler2D.swift @@ -29,8 +29,8 @@ public final class Sampler2D: ShaderValue { } } - public func sample(at textCoord: Vec2, filter: Filter = .linear) -> Vec4 { - return Vec4(Operation(lhs: self, rhs: textCoord, operator: .sampler2D(filter: filter))) + public func sample(at textCoord: Vec2) -> Vec4 { + return Vec4(Operation(lhs: self, rhs: textCoord, operator: .sampler2D)) } public var size: Vec2 {