metal render points on MTLTexture

Hi all,

I try use a render encoder to draw some points on my texture, but I Found build a pipelinestate with colorAttachment[0].textrue = myTextrue, it is not work .

how to draw points on my texture (had some contents on this texture)



my metal file is like this:


#include <metal_stdlib>

using namespace metal;

struct VertexInOut

{

float4 m_Position [[position]];

float point_size [[point_size]];

float2 m_TexCoord [[user(texturecoord)]];

};

vertex VertexInOut pointSpiritVertex(device float4 *pPosition[[ buffer(0) ]],

constant packed_float2 *pTexCoords[[ buffer(1) ]],

uint vid[[ vertex_id ]] )

{

VertexInOut outVertices;


outVertices.m_Position = pPosition[vid]; /

outVertices.m_TexCoord = pTexCoords[vid];

outVertices.point_size = 100;


return outVertices;

}

fragment half4 pointSpiritFragment(VertexInOut inFrag[[ stage_in ]], texture2d<half> inTex[[texture(0)]], texture2d<half> outTex[[texture(1)]])

{

half4 outColor = half4(1.0, 0.0, 0.0, 1.0);

constexpr sampler qsampler;

half4 srcColor = inTex.sample(qsampler, inFrag.m_TexCoord);

outColor = srcColor ;

return outColor;

}


And encoder like this:

[renderEncoder setVertexBuffer:self.verticsBuffer offset:0 atIndex: 0 ];

[renderEncoder setVertexBuffer:self.coordBuffer offset:0 atIndex: 1];

[renderEncoder setFragmentTexture:firstInputTexture.texture atIndex:0];

[renderEncoder setFragmentTexture:outputTexture.texture atIndex:1];

[renderEncoder setRenderPipelineState:_renderpipelineState];

[renderEncoder drawPrimitives:MTLPrimitiveTypePoint vertexStart:0 vertexCount:4 instanceCount:1];

[renderEncoder endEncoding];

Replies

You should set your output texture to a colorAttachment in the MTLRenderPassDescriptor that you use to create 'renderEncoder.' This enables to your app render to the texture. You will also need need o make sure that the pixelformat in your pipeline's colorAttachement matches you texture's pixel format.