Skip to content

Commit

Permalink
Moved texture rendering completely to VBOs.
Browse files Browse the repository at this point in the history
  • Loading branch information
BradLarson committed May 23, 2017
1 parent 6af1b22 commit 7f9e6a3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
5 changes: 4 additions & 1 deletion framework/Source/Framebuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public class Framebuffer {
let hash:Int64
let textureOverride:Bool

weak var context:OpenGLContext?

public init(context:OpenGLContext, orientation:ImageOrientation, size:GLSize, textureOnly:Bool = false, minFilter:Int32 = GL_LINEAR, magFilter:Int32 = GL_LINEAR, wrapS:Int32 = GL_CLAMP_TO_EDGE, wrapT:Int32 = GL_CLAMP_TO_EDGE, internalFormat:Int32 = GL_RGBA, format:Int32 = GL_BGRA, type:Int32 = GL_UNSIGNED_BYTE, stencil:Bool = false, overriddenTexture:GLuint? = nil) throws {
self.context = context
self.size = size
self.orientation = orientation
self.internalFormat = internalFormat
Expand Down Expand Up @@ -141,7 +144,7 @@ public class Framebuffer {
}

public func texturePropertiesForOutputRotation(_ rotation:Rotation) -> InputTextureProperties {
return InputTextureProperties(textureCoordinates:rotation.textureCoordinates(), texture:texture)
return InputTextureProperties(textureVBO:context!.textureVBO(for:rotation), texture:texture)
}

public func texturePropertiesForTargetOrientation(_ targetOrientation:ImageOrientation) -> InputTextureProperties {
Expand Down
1 change: 1 addition & 0 deletions framework/Source/Mac/OpenGLContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class OpenGLContext: SerialDispatch {
generatedContext.makeCurrentContext()

standardImageVBO = generateVBO(for:standardImageVertices)
generateTextureVBOs()

glDisable(GLenum(GL_DEPTH_TEST))
glEnable(GLenum(GL_TEXTURE_2D))
Expand Down
28 changes: 25 additions & 3 deletions framework/Source/OpenGLRendering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,24 @@

import Foundation

public enum InputTextureStorageFormat {
case textureCoordinates([GLfloat])
case textureVBO(GLuint)
}

public struct InputTextureProperties {
public let textureCoordinates:[GLfloat]
public let textureStorage:InputTextureStorageFormat
public let texture:GLuint

public init(textureCoordinates:[GLfloat]? = nil, textureVBO:GLuint? = nil, texture:GLuint) {
self.texture = texture
switch (textureCoordinates, textureVBO) {
case let (.some(coordinates), .none): self.textureStorage = .textureCoordinates(coordinates)
case let (.none, .some(vbo)): self.textureStorage = .textureVBO(vbo)
case (.none, .none): fatalError("Need to specify either texture coordinates or a VBO to InputTextureProperties")
case (.some, .some): fatalError("Can't specify both texture coordinates and a VBO to InputTextureProperties")
}
}
}

public struct GLSize {
Expand Down Expand Up @@ -73,7 +88,15 @@ public func renderQuadWithShader(_ shader:ShaderProgram, uniformSettings:ShaderU

for (index, inputTexture) in inputTextures.enumerated() {
if let textureCoordinateAttribute = shader.attributeIndex("inputTextureCoordinate".withNonZeroSuffix(index)) {
glVertexAttribPointer(textureCoordinateAttribute, 2, GLenum(GL_FLOAT), 0, 0, inputTexture.textureCoordinates)
switch inputTexture.textureStorage {
case let .textureCoordinates(textureCoordinates):
print("Texture slow path")
glVertexAttribPointer(textureCoordinateAttribute, 2, GLenum(GL_FLOAT), 0, 0, textureCoordinates)
case let .textureVBO(textureVBO):
glBindBuffer(GLenum(GL_ARRAY_BUFFER), textureVBO)
glVertexAttribPointer(textureCoordinateAttribute, 2, GLenum(GL_FLOAT), 0, 0, nil)
glBindBuffer(GLenum(GL_ARRAY_BUFFER), 0)
}
} else if (index == 0) {
fatalError("The required attribute named inputTextureCoordinate was missing from the shader program during rendering.")
}
Expand Down Expand Up @@ -226,7 +249,6 @@ public func generateVBO(for vertices:[GLfloat]) -> GLuint {
var newBuffer:GLuint = 0
glGenBuffers(1, &newBuffer)
glBindBuffer(GLenum(GL_ARRAY_BUFFER), newBuffer)
print("Allocating buffer for size: \(MemoryLayout<GLfloat>.size * vertices.count), at index: \(newBuffer)")
glBufferData(GLenum(GL_ARRAY_BUFFER), MemoryLayout<GLfloat>.size * vertices.count, vertices, GLenum(GL_STATIC_DRAW))
glBindBuffer(GLenum(GL_ARRAY_BUFFER), 0)
return newBuffer
Expand Down
1 change: 1 addition & 0 deletions framework/Source/Operations/LanczosResampling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class LanczosResampling: BasicOperation {
// Shrink the vertical component of the first stage
let inputSize = inputFramebuffer.sizeForTargetOrientation(.portrait)
let firstStageFramebuffer = sharedImageProcessingContext.framebufferCache.requestFramebufferWithProperties(orientation:.portrait, size:GLSize(width:inputSize.width, height:renderFramebuffer.size.height), stencil:false)
firstStageFramebuffer.lock()
firstStageFramebuffer.activateFramebufferForRendering()
clearFramebufferWithColor(backgroundColor)

Expand Down
1 change: 1 addition & 0 deletions framework/Source/iOS/OpenGLContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class OpenGLContext: SerialDispatch {
EAGLContext.setCurrent(generatedContext)

standardImageVBO = generateVBO(for:standardImageVertices)
generateTextureVBOs()

glDisable(GLenum(GL_DEPTH_TEST))
glEnable(GLenum(GL_TEXTURE_2D))
Expand Down

0 comments on commit 7f9e6a3

Please sign in to comment.