Skip to content

Commit

Permalink
Remove shader sampleFilters
Browse files Browse the repository at this point in the history
Use Material channels to assign a sample filter
  • Loading branch information
STREGA committed Oct 18, 2023
1 parent 3255189 commit 59ddb29
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 27 deletions.
14 changes: 5 additions & 9 deletions Sources/GateEngine/System/Rendering/SystemShaders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,15 @@ 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
}()
/// Uses material.channel(0).texture to shade objects
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
Expand All @@ -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
}()
Expand All @@ -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
Expand All @@ -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
}()
Expand Down
19 changes: 17 additions & 2 deletions Sources/Shaders/Generators/CodeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -305,7 +305,7 @@ public class CodeGenerator {
fatalError()
case .branch(comparing: _):
fatalError()
case .sampler2D(filter: _):
case .sampler2D:
fatalError()
case .sampler2DSize:
fatalError()
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Shaders/Generators/GLSLCodeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions Sources/Shaders/Generators/HLSLCodeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 4 additions & 8 deletions Sources/Shaders/Generators/MSLCodeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<float> tex\(index) [[texture(\(index))]]"
fragmentTextureList += ",\n texture2d<float> tex\(index) [[texture(\(index))]]"
}

return """
Expand Down Expand Up @@ -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));
Expand Down
6 changes: 3 additions & 3 deletions Sources/Shaders/ShaderDocument/Values/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions Sources/Shaders/ShaderDocument/Values/Sampler2D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 59ddb29

Please sign in to comment.