Skip to content

Commit c58ba41

Browse files
committed
Add scissor rect
1 parent 4497640 commit c58ba41

File tree

14 files changed

+122
-29
lines changed

14 files changed

+122
-29
lines changed

Dependencies/OpenGL/OpenGL_GateEngine/OpenGL_GateEngine.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,12 @@ public func glUniform4f(location: GLint, value1: Float, value2: Float, value3: F
10411041
_glDeleteVertexArrays(GLsizei(arrays.count), arrays)
10421042
}
10431043

1044-
@inlinable @inline(__always) public func glViewport<T: BinaryInteger>(x: T, y: T, width: T, height: T) {
1045-
_glViewport(GLint(x), GLint(y), GLsizei(width), GLsizei(height))
1044+
@inlinable @inline(__always) public func glViewport(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
1045+
_glViewport(x, y, width, height)
1046+
}
1047+
1048+
@inlinable @inline(__always) public func glScissor(x: GLint, y: GLint, width: GLsizei, height: GLsizei) {
1049+
_glScissor(x, y, width, height)
10461050
}
10471051

10481052
@inlinable @inline(__always) public func glGetString(describing name: OpenGL.Description) throws -> String {

Dependencies/OpenGL/OpenGL_GateEngine/Platforms/Apple/GLKit.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ public typealias GLdouble = GLKit.GLdouble
456456
GLKit.glViewport(x, y, width, height)
457457
}
458458

459+
@_transparent @usableFromInline internal func _glScissor(_ x: GLint, _ y: GLint, _ width: GLsizei, _ height: GLsizei) {
460+
GLKit.glScissor(x, y, width, height)
461+
}
462+
459463
@_transparent @usableFromInline internal func _glGetString(_ name: GLenum) -> UnsafePointer<GLubyte>? {
460464
return GLKit.glGetString(name)
461465
}

Dependencies/OpenGL/OpenGL_GateEngine/Platforms/GLEW/GLEW_Linux.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,10 @@ public func glewInit() {
455455
GLEW.__glewViewport(x, y, width, height)
456456
}
457457

458+
@_transparent @usableFromInline internal func _glScissor(_ x: GLint, _ y: GLint, _ width: GLsizei, _ height: GLsizei) {
459+
GLEW.__glewScissor(x, y, width, height)
460+
}
461+
458462
@_transparent @usableFromInline internal func _glGetString(_ name: GLenum) -> UnsafePointer<GLubyte>? {
459463
return GLEW.__glewGetString(name)
460464
}

Dependencies/OpenGL/OpenGL_GateEngine/Platforms/GLEW/GLEW_Windows.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ public func glewInit() {
448448
GLEW.glViewport(x, y, width, height)
449449
}
450450

451+
@_transparent @usableFromInline internal func _glScissor(_ x: GLint, _ y: GLint, _ width: GLsizei, _ height: GLsizei) {
452+
GLEW.glScissor(x, y, width, height)
453+
}
454+
451455
@_transparent @usableFromInline internal func _glGetString(_ name: GLenum) -> UnsafePointer<GLubyte>? {
452456
return GLEW.glGetString(name)
453457
}

Dependencies/OpenGL/OpenGL_GateEngine/Platforms/Linux/OpenGLLinux.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ public typealias GLdouble = OpenGL_Linux.GLdouble
454454
OpenGL_Linux.glViewport(x, y, width, height)
455455
}
456456

457+
@_transparent @usableFromInline internal func _glScissor(_ x: GLint, _ y: GLint, _ width: GLsizei, _ height: GLsizei) {
458+
OpenGL_Linux.glScissor(x, y, width, height)
459+
}
460+
457461
@_transparent @usableFromInline internal func _glGetString(_ name: GLenum) -> UnsafePointer<GLubyte>? {
458462
return OpenGL_Linux.glGetString(name)
459463
}

Dependencies/OpenGL/OpenGL_GateEngine/Platforms/Unsupported/Unsupported.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ public typealias GLdouble = Double
456456
fatalError("OpenGL Not Supported")
457457
}
458458

459+
@_transparent @usableFromInline internal func _glScissor(_ x: GLint, _ y: GLint, _ width: GLsizei, _ height: GLsizei) {
460+
fatalError("OpenGL Not Supported")
461+
}
462+
459463
@_transparent @usableFromInline internal func _glGetString(_ name: GLenum) -> UnsafePointer<GLubyte>? {
460464
fatalError("OpenGL Not Supported")
461465
}

Dependencies/OpenGL/OpenGL_GateEngine/Platforms/Windows/OpenGLWindows.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ public typealias GLdouble = OpenGL_Windows.GLdouble
453453
OpenGL_Windows.glViewport(x, y, width, height)
454454
}
455455

456+
@_transparent @usableFromInline internal func _glScissor(_ x: GLint, _ y: GLint, _ width: GLsizei, _ height: GLsizei) {
457+
OpenGL_Windows.glScissor(x, y, width, height)
458+
}
459+
456460
@_transparent @usableFromInline internal func _glGetString(_ name: GLenum) -> UnsafePointer<GLubyte>? {
457461
return OpenGL_Windows.glGetString(name)
458462
}

Sources/GateEngine/System/Rendering/Drawables/Canvas.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99
@MainActor public struct Canvas {
1010
let interfaceScale: Float
1111
internal var viewOrigin: Position2? = nil
12-
internal var clipRect: Rect? = nil
12+
internal var viewport: Rect? = nil
13+
private var _scissorRect: Rect? = nil
14+
internal var scissorRect: Rect? {
15+
if let _scissorRect {
16+
return _scissorRect * interfaceScale
17+
}
18+
return nil
19+
}
1320

1421
internal var size: Size2? = nil
1522
internal var camera: Camera? = nil
@@ -37,13 +44,17 @@
3744
self.viewOrigin = viewOrigin
3845
}
3946

47+
public mutating func setViewport(_ viewport: Rect?) {
48+
self.viewport = viewport
49+
}
50+
4051
/**
4152
Applys a clip rectangle to all content in this Canvas.
4253

4354
- parameter clipRect: The area within this Rect will be drawn.
4455
*/
45-
public mutating func setClipRect(_ clipRect: Rect?) {
46-
self.clipRect = clipRect
56+
public mutating func setScissorRect(_ scissorRect: Rect?) {
57+
self._scissorRect = scissorRect
4758
}
4859

4960
public mutating func insert(
@@ -163,6 +174,7 @@
163174
scale: Size2 = .one,
164175
depth: Float = 0,
165176
opacity: Float = 1,
177+
sampleFilter: Material.Channel.SampleFilter = .linear,
166178
flags: CanvasElementSpriteFlags = .default
167179
) {
168180
let position = Position3(position.x, position.y, depth * -1)
@@ -173,6 +185,7 @@
173185
let material = Material { material in
174186
material.channel(0) { channel in
175187
channel.texture = texture
188+
channel.sampleFilter = sampleFilter
176189
if let subRect {
177190
channel.offset = Position2(
178191
(subRect.position.x + 0.001) / Float(texture.size.width),

Sources/GateEngine/System/Rendering/Drawables/Scene.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
@MainActor public struct Scene {
1010
@usableFromInline internal var camera: Camera
1111
@usableFromInline internal var viewport: Rect?
12+
@usableFromInline internal var scissorRect: Rect?
1213

1314
@usableFromInline internal var pointLights: Set<ScenePointLight> = []
1415
@usableFromInline internal var spotLights: Set<SceneSpotLight> = []
@@ -36,6 +37,11 @@
3637
public mutating func setViewport(_ viewport: Rect?) {
3738
self.viewport = viewport
3839
}
40+
41+
@inlinable @inline(__always)
42+
public mutating func setScissorRect(_ scissorRect: Rect?) {
43+
self.scissorRect = scissorRect
44+
}
3945

4046
/** Adds geometry to the scene for rendering.
4147
- parameter geometry: The geometry to draw.
@@ -345,7 +351,7 @@
345351
return false
346352
}
347353

348-
public init(camera: Camera, viewport: Rect? = nil, estimatedCommandCount: Int = 10) {
354+
public init(camera: Camera, viewport: Rect? = nil, clipRect: Rect? = nil, estimatedCommandCount: Int = 10) {
349355
self.camera = camera
350356
self.viewport = viewport
351357

Sources/GateEngine/System/Rendering/Platforms/DirectX12/DX12RenderTarget.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class DX12RenderTarget: RenderTargetBackend {
246246
renderer.cachedContent.removeAll(keepingCapacity: true)
247247
}
248248

249-
func willBeginContent(matrices: Matrices?, viewport: GameMath.Rect?) {
249+
func willBeginContent(matrices: Matrices?, viewport: GameMath.Rect?, scissorRect: GameMath.Rect?) {
250250
do {
251251
try self.commandList.reset(
252252
usingOriginalAllocator: commandAllocator,
@@ -267,16 +267,20 @@ class DX12RenderTarget: RenderTargetBackend {
267267
self.commandList.setViewports([
268268
D3DViewport(width: viewport.size.width, height: viewport.size.height)
269269
])
270+
} else {
271+
self.commandList.setViewports([D3DViewport(width: size.width, height: size.height)])
272+
}
273+
274+
if let scissorRect: Rect = scissorRect {
270275
self.commandList.setScissorRects([
271276
D3DRect(
272-
x: Int(viewport.position.x),
273-
y: Int(viewport.position.y),
274-
width: Int(viewport.size.width),
275-
height: Int(viewport.size.height)
277+
x: Int(scissorRect.position.x),
278+
y: Int(scissorRect.position.y),
279+
width: Int(scissorRect.size.width),
280+
height: Int(scissorRect.size.height)
276281
)
277282
])
278283
} else {
279-
self.commandList.setViewports([D3DViewport(width: size.width, height: size.height)])
280284
self.commandList.setScissorRects([
281285
D3DRect(x: 0, y: 0, width: Int(size.width), height: Int(size.height))
282286
])

0 commit comments

Comments
 (0)