iOS金属-渲染完整的三角形
我正在尝试一个关于金属的教程 这个问题只适用于本教程的第一部分,其中渲染了三角形,之后渲染了立方体 我似乎已经完成了前面提到的所有工作,创建了顶点结构、节点类、三角形(然后是立方体子类)、重构了视图控制器和着色器 应用程序渲染,但仅渲染到屏幕的一半,如下所示: 这是我的ViewController:iOS金属-渲染完整的三角形,ios,swift,metal,Ios,Swift,Metal,我正在尝试一个关于金属的教程 这个问题只适用于本教程的第一部分,其中渲染了三角形,之后渲染了立方体 我似乎已经完成了前面提到的所有工作,创建了顶点结构、节点类、三角形(然后是立方体子类)、重构了视图控制器和着色器 应用程序渲染,但仅渲染到屏幕的一半,如下所示: 这是我的ViewController: import UIKit import Metal import QuartzCore class ViewController: UIViewController { var devi
import UIKit
import Metal
import QuartzCore
class ViewController: UIViewController {
var device : MTLDevice! = nil
var metalLayer : CAMetalLayer! = nil
var vertexBuffer : MTLBuffer! = nil
var pipelineState : MTLRenderPipelineState! = nil
var commandQueue: MTLCommandQueue! = nil
var timer: CADisplayLink! = nil
var objectToDraw: Triangle!
override func viewDidLoad() {
super.viewDidLoad()
initialize_2()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func initialize_2(){
device = MTLCreateSystemDefaultDevice()
metalLayer = CAMetalLayer()
metalLayer.device = device
metalLayer.pixelFormat = .BGRA8Unorm
metalLayer.framebufferOnly = true
metalLayer.frame = view.layer.frame
view.layer.addSublayer(metalLayer)
objectToDraw = Triangle(device: device)
let defaultLibrary = device.newDefaultLibrary()
let fragmentProgram = defaultLibrary!.newFunctionWithName("basic_fragment")
let vertexProgram = defaultLibrary!.newFunctionWithName("basic_vertex")
let pipelineStateDescriptor = MTLRenderPipelineDescriptor()
pipelineStateDescriptor.vertexFunction = vertexProgram
pipelineStateDescriptor.fragmentFunction = fragmentProgram
pipelineStateDescriptor.colorAttachments[0].pixelFormat = .BGRA8Unorm
do{
try pipelineState = device.newRenderPipelineStateWithDescriptor(pipelineStateDescriptor)
}catch is NSError{
print("Failed to create pipeline state")
}
commandQueue = device.newCommandQueue()
timer = CADisplayLink(target: self, selector: #selector(ViewController.gameloop_2))
timer.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)
}
func gameloop_2() {
autoreleasepool {
self.render_2()
}
}
func render_2(){
var drawable = metalLayer.nextDrawable()
objectToDraw.render(commandQueue, pipelineState: pipelineState, drawable: drawable!, clearColor: nil)
}
}
这是我的顶点类:
import Foundation
struct Vertex{
var x, y, z : Float // position data
var r, g, b, a : Float // color data
func floatBuffer() -> [Float]{
return [x,y,x,r,g,b,a]
}
}
和节点类:
import Foundation
import Metal
import QuartzCore
class Node {
let name: String
var vertexCount: Int
var vertexBuffer: MTLBuffer
var device: MTLDevice
init(name: String, vertices: Array<Vertex>, device: MTLDevice){
// 1
var vertexData = Array<Float>()
for vertex in vertices{
vertexData += vertex.floatBuffer()
}
// 2
let dataSize = vertexData.count * sizeofValue(vertexData[0])
vertexBuffer = device.newBufferWithBytes(vertexData, length: dataSize, options: [])
// 3
self.name = name
self.device = device
vertexCount = vertices.count
}
func render(commandQueue: MTLCommandQueue, pipelineState: MTLRenderPipelineState, drawable: CAMetalDrawable, clearColor: MTLClearColor?){
let renderPassDescriptor = MTLRenderPassDescriptor()
renderPassDescriptor.colorAttachments[0].texture = drawable.texture
renderPassDescriptor.colorAttachments[0].loadAction = .Clear
renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColor(red: 0.0, green: 104.0/255.0, blue: 5.0/255.0, alpha: 1.0)
renderPassDescriptor.colorAttachments[0].storeAction = .Store
let commandBuffer = commandQueue.commandBuffer()
let renderEncoderOpt : MTLRenderCommandEncoder! = commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor)
if let renderEncoder = renderEncoderOpt {
renderEncoder.setRenderPipelineState(pipelineState)
renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, atIndex: 0)
renderEncoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: vertexCount, instanceCount: vertexCount/3)
renderEncoder.endEncoding()
}
commandBuffer.presentDrawable(drawable)
commandBuffer.commit()
}
}
和我的着色器文件:
#include <metal_stdlib>
using namespace metal;
struct VertexIn{
packed_float3 position;
packed_float4 color;
};
struct VertexOut{
float4 position [[position]];
float4 color;
};
vertex VertexOut basic_vertex(
const device VertexIn* vertex_array [[ buffer(0) ]],
unsigned int vid [[ vertex_id ]]) {
VertexIn VertexIn = vertex_array[vid];
VertexOut VertexOut;
VertexOut.position = float4(VertexIn.position,1);
VertexOut.color = VertexIn.color;
return VertexOut;
}
fragment half4 basic_fragment(VertexOut interpolated [[stage_in]]) {
return half4(interpolated.color[0], interpolated.color[1], interpolated.color[2], interpolated.color[3]);
}
#包括
使用金属;
结构蛋白{
3位填料;
包装4色;
};
结构顶点输出{
浮动4位[[位]];
4色;
};
顶点顶点输出基本顶点(
常量设备VertexIn*顶点数组[[缓冲区(0)],
无符号整数vid[[vertex_id]]{
VertexIn VertexIn=顶点数组[vid];
顶点输出顶点输出;
VertexOut.position=float4(顶点位置,1);
VertexOut.color=VertexIn.color;
返回顶点;
}
片段半4基本片段(顶点外插[[stage_in]]){
返回half4(插值.color[0],插值.color[1],插值.color[2],插值.color[3]);
}
我试图理解为什么着色器不渲染完整的三角形。我也是(非常)新的图形编程,所以任何帮助或指导将不胜感激。本教程的链接是:
编辑:我在iPad air上运行这个,如果这会有什么不同的话。我还尝试更改Triangle.swift中的坐标,但显然我做错了什么。我可以看到顶点设置中的一个小错误,这可能是导致此问题的原因
func floatBuffer() -> [Float]{
return [x,y,x,r,g,b,a]
}
啊啊啊,救了我一天!
func floatBuffer() -> [Float]{
return [x,y,x,r,g,b,a]
}