Swift 2.2中未冲洗金属纹理缓冲区
我使用金属来显示具有纹理的3D对象。 第一次加载时,纹理显示良好。但是如果我在运行时用一个新的纹理来改变它,那么新的纹理将与旧的纹理合并 我的代码是:Swift 2.2中未冲洗金属纹理缓冲区,swift,texture-mapping,texture2d,metal,Swift,Texture Mapping,Texture2d,Metal,我使用金属来显示具有纹理的3D对象。 第一次加载时,纹理显示良好。但是如果我在运行时用一个新的纹理来改变它,那么新的纹理将与旧的纹理合并 我的代码是: let texture = MetalTexture(resourceName: "", ext: "", mipmaped: true) // texture.path = "\(NSBundle.mainBundle().bundlePath)/smlroot/models/bodies/FB020.png"
let texture = MetalTexture(resourceName: "", ext: "", mipmaped: true)
// texture.path = "\(NSBundle.mainBundle().bundlePath)/smlroot/models/bodies/FB020.png"
texture.path = pngPath
texture.loadTexture(device: device, commandQ: commandQ, flip: false)
super.readPly(vertices,faces: faces, texture: texture.texture)
在这里,每次更改纹理运行时,我都会创建一个新的纹理对象。然后
使用此代码加载它:
func loadTexture(device device: MTLDevice, commandQ: MTLCommandQueue, flip: Bool){
let image = UIImage(contentsOfFile: path)?.CGImage
let colorSpace = CGColorSpaceCreateDeviceRGB()
width = CGImageGetWidth(image!)
height = CGImageGetHeight(image!)
let rowBytes = width * bytesPerPixel
let context = CGBitmapContextCreate(nil, width, height, bitsPerComponent, rowBytes, colorSpace, CGImageAlphaInfo.PremultipliedLast.rawValue)
let bounds = CGRect(x: 0, y: 0, width: Int(width), height: Int(height))
CGContextClearRect(context!, bounds)
if flip == false{
CGContextTranslateCTM(context!, 0, CGFloat(self.height))
CGContextScaleCTM(context!, 1.0, -1.0)
}
CGContextDrawImage(context!, bounds, image!)
let texDescriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(MTLPixelFormat.RGBA8Unorm, width: Int(width), height: Int(height), mipmapped: isMipmaped)
target = texDescriptor.textureType
texture = device.newTextureWithDescriptor(texDescriptor)
let pixelsData = CGBitmapContextGetData(context!)
let region = MTLRegionMake2D(0, 0, Int(width), Int(height))
texture.replaceRegion(region, mipmapLevel: 0, withBytes: pixelsData, bytesPerRow: Int(rowBytes))
if (isMipmaped == true){
generateMipMapLayersUsingSystemFunc(texture, device: device, commandQ: commandQ, block: { (buffer) -> Void in
print("mips generated")
})
}
print("mipCount:\(texture.mipmapLevelCount)")
}
renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, atIndex: 0)
renderEncoder.setCullMode(.None)
renderEncoder.setFragmentTexture(texture, atIndex: 0)
if let samplerState = samplerState{
renderEncoder.setFragmentSamplerState(samplerState, atIndex: 0)
}
let nodeModelMatrix = self.modelMatrix(rotationY)
nodeModelMatrix.multiplyLeft(parentModelViewMatrix)
let uniformBuffer = bufferProvider.nextUniformsBuffer(projectionMatrix, modelViewMatrix: nodeModelMatrix, light: light)
renderEncoder.setVertexBuffer(uniformBuffer, offset: 0, atIndex: 1)
renderEncoder.setFragmentBuffer(uniformBuffer, offset: 0, atIndex: 1)
//change this.
//renderEncoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: vertexCount)
renderEncoder.drawIndexedPrimitives(.Triangle, indexCount: indexCount, indexType: .UInt32, indexBuffer: indexBuffer, indexBufferOffset: 0)
渲染对象时,我使用下面的管道加载对象:
func loadTexture(device device: MTLDevice, commandQ: MTLCommandQueue, flip: Bool){
let image = UIImage(contentsOfFile: path)?.CGImage
let colorSpace = CGColorSpaceCreateDeviceRGB()
width = CGImageGetWidth(image!)
height = CGImageGetHeight(image!)
let rowBytes = width * bytesPerPixel
let context = CGBitmapContextCreate(nil, width, height, bitsPerComponent, rowBytes, colorSpace, CGImageAlphaInfo.PremultipliedLast.rawValue)
let bounds = CGRect(x: 0, y: 0, width: Int(width), height: Int(height))
CGContextClearRect(context!, bounds)
if flip == false{
CGContextTranslateCTM(context!, 0, CGFloat(self.height))
CGContextScaleCTM(context!, 1.0, -1.0)
}
CGContextDrawImage(context!, bounds, image!)
let texDescriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(MTLPixelFormat.RGBA8Unorm, width: Int(width), height: Int(height), mipmapped: isMipmaped)
target = texDescriptor.textureType
texture = device.newTextureWithDescriptor(texDescriptor)
let pixelsData = CGBitmapContextGetData(context!)
let region = MTLRegionMake2D(0, 0, Int(width), Int(height))
texture.replaceRegion(region, mipmapLevel: 0, withBytes: pixelsData, bytesPerRow: Int(rowBytes))
if (isMipmaped == true){
generateMipMapLayersUsingSystemFunc(texture, device: device, commandQ: commandQ, block: { (buffer) -> Void in
print("mips generated")
})
}
print("mipCount:\(texture.mipmapLevelCount)")
}
renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, atIndex: 0)
renderEncoder.setCullMode(.None)
renderEncoder.setFragmentTexture(texture, atIndex: 0)
if let samplerState = samplerState{
renderEncoder.setFragmentSamplerState(samplerState, atIndex: 0)
}
let nodeModelMatrix = self.modelMatrix(rotationY)
nodeModelMatrix.multiplyLeft(parentModelViewMatrix)
let uniformBuffer = bufferProvider.nextUniformsBuffer(projectionMatrix, modelViewMatrix: nodeModelMatrix, light: light)
renderEncoder.setVertexBuffer(uniformBuffer, offset: 0, atIndex: 1)
renderEncoder.setFragmentBuffer(uniformBuffer, offset: 0, atIndex: 1)
//change this.
//renderEncoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: vertexCount)
renderEncoder.drawIndexedPrimitives(.Triangle, indexCount: indexCount, indexType: .UInt32, indexBuffer: indexBuffer, indexBufferOffset: 0)
我得到了一个重叠的纹理。有人能帮我找出这个问题吗