2014/10/03

Data type of shader in Metal

I have been trying Metal since I got Xcode 6. At first I tried this tutorial : iOS 8 Metal Tutorial with Swift: Getting Started - Ray Wenderlich This code is written in Swift and I wanted to use Metal in Swift. But I thought I wouldn’t be able to find good sample code in Swift yet, and all of the sample code in Apple’s developer center are written in Objective-C++.

Anyway I tried to draw one triangle as the tutorial above did :
const float vertexData[] = {
    0.5, -0.5, 0.0, 
    -0.5, 0.5, 0.0, 
    -0.5, -0.5, 0.0,
};
This vertex data is just placed float value in order. Apple’s sample code use simd::float3 or simd::float4 but I didn’t, because most of the data I used in OpenGL ES are written like this. Yes, I wanted to use same data.
But it didn’t work…

vertex buffer is created like this :
vertexBuffer = [device newBufferWithBytes:vertexData length:sizeof(vertexData) options:0];
Shader receives them like this :
vertex float4 basic_vertex(device packed_float3 *vertex_array [[ buffer(0) ]] , unsigned int vid [[ vertex_id ]]) {   
	return float4(float3(vertex_array[vid]) , 1.0);
}
I suppose this must work but failed…

But as same as apple’s sample code does, you make struct and use it to receive in shader :
typedef struct {
    packed_float3 position;
} vertex_t;

vertex float4 basic_vertex( device vertex_t *vertex_array [[ buffer(0) ]] , unsigned int vid [[ vertex_id ]]) { 
	return float4(vertex_array[vid].position, 1.0);
}
Then this worked. I suppose both are same. But there must be difference...

screenshot to implement metal
↑Screenshot of this code.
Develop | Comments(1) | Trackback(0)
Comment
Comment is pending approval.
Comment is pending blog author's approval.

管理者のみに表示