Home Game Development c# – Off-Screen Image processing with SharpDX

c# – Off-Screen Image processing with SharpDX

0
c# – Off-Screen Image processing with SharpDX

[ad_1]

Hey mate so there are just a few methods to attract on display screen, you’ll be able to draw a full display screen triangle and do some fancy shader math or you’ll be able to render a full quad.

Ill present you find out how to do each.

Full Screen Quad Render:

This is the total quad renderer class, its a little bit of setup however it works effectively, I pulled it out of my code base so you’ll have to take away all my stuff.

Public Class QuadRenderer
Private _vertexBuffer As VertexPositionTexture() = Nothing
Private _indexBuffer As Integer() = Nothing
Private instdata As DataStream
Private vertexBuffer As Buffer
Private indexBuffer As Buffer
Private QuadInputlayout As InputLayout
Private QuadVertexBinding As VertexBufferBinding()


Public Sub New(_device As LowLevelDevice, ShaderInput As ShaderBytecode)
  'organising the indexbuffer
    _indexBuffer = New Integer() {0, 3, 2, 0, 1, 3}
    instdata = New DataStream(_indexBuffer.Count * 4, True, True)
    instdata.WriteRange(_indexBuffer, 0, _indexBuffer.Count)
    instdata.Position = 0
    indexBuffer = _device.CreateBuffer(instdata, _indexBuffer.Count * 4, ResourceUsage.Immutable, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0)

'organising the quad verts
    _vertexBuffer = New VertexPositionTexture(3) {}
    _vertexBuffer(0) = New VertexPositionTexture(New Vector3(-1.0!, 1.0!, 0!), New Vector2(0!, 0!))
    _vertexBuffer(1) = New VertexPositionTexture(New Vector3(1.0!, 1.0!, 0!), New Vector2(1.0!, 0!))
    _vertexBuffer(2) = New VertexPositionTexture(New Vector3(-1.0!, -1.0!, 0!), New Vector2(0!, 1.0!))
    _vertexBuffer(3) = New VertexPositionTexture(New Vector3(1.0!, -1.0!, 0!), New Vector2(1.0!, 1.0!))

  'organising the vertexbuffer
    instdata = New DataStream(_vertexBuffer.Count * VertexPositionTexture.Stride, True, True)
    instdata.WriteRange(_vertexBuffer, 0, _vertexBuffer.Count)
    instdata.Position = 0

    vertexBuffer = _device.CreateBuffer(instdata, New BufferDescription(_vertexBuffer.Count * VertexPositionTexture.Stride, ResourceUsage.Immutable, BindFlags.VertexBuffer, 0, 0, 0))
    QuadVertexBinding = New VertexBufferBinding() {New VertexBufferBinding(vertexBuffer, VertexPositionTexture.Stride, 0)}

  'organising the inputlayout
    Dim PosColor As InputElement() = {New InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, EnterClassification.PerVertexData,
    0), New InputElement("TEXCOORD", 0, Format.R32G32_Float, InputElement.AppendAligned, 0, EnterClassification.PerVertexData, 0)}

    QuadInputlayout = _device.CreateInputLayout(ShaderInput, PosColor)
End Sub



Public Sub RenderQuad(ByRef _Context As DeviceContext,srv as shaderresourceview)
  'setting renderstate
    _Context.PixelShader.SetShaderResource(0, srv)' srv= the feel you wish to draw
    _Context.EnterAssembler.InputLayout = QuadInputlayout
    _Context.EnterAssembler.PrimitiveTopology = PrimitiveTopology.TriangleChecklist
    _Context.EnterAssembler.SetVertexBuffers(0, QuadVertexBinding)
    _Context.EnterAssembler.SetIndexBuffer(indexBuffer, Format.R32_UInt, 0)
    _Context.DrawListed(6, 0, 0)
End Sub

End Class

_device.CreateInputLayout is a customized inputlayout builder so that you simply cross it a vertex shader and it offers you the right inputlay however you’ll have to begin a brand new query for that.

HLSL:
This is the shader for the total display screen quad.

Texture2D colorMap; //DeviceContext.PixelShader.SetShaderResource(0, srv) units this on the cpu
sampler shadeSampler = sampler_state
{

AddressU = Clamp;
AddressV = Clamp;
Filter = MIN_MAG_MIP_POINT;
};
struct VertexInput
{
float3 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};
VertexShaderOutput VSQuad2(VertexInput enter)
{
VertexShaderOutput consequence;

consequence.Position = float4(enter.Position,1); //studying vertex information from the stream
consequence.TexCoord = enter.TexCoord; 

return consequence;
}
PixelShaderOutput PShader(VertexShaderOutput enter) 
{
 PixelShaderOutput output = (PixelShaderOutput)0;  

 float4 Color= colorMap.Sample(shadeSampler , enter.TexCoord);//the feel you wish to draw


 return output;
}

Full Screen Triangle:

The cool factor about this one is that there isn’t any CPU aspect arrange, the DX runtime will generate the vertex information for you.

Use this little bit of code to attract the total display screen triangle.

    DeviceContext.EnterAssembler.PrimitiveTopology = PrimitiveTopology.TriangleChecklist
    DeviceContext.PixelShader.SetShaderResource(0, srv)' srv= the feel you wish to draw
    DeviceContext.EnterAssembler.SetVertexBuffers(Nothing)
    DeviceContext.EnterAssembler.SetIndexBuffer(Nothing, Nothing, Nothing)
    DeviceContext.Rasterizer.State = Nothing
    DeviceContext.Draw(3, 0)

HLSL:

This is the place the magic occurs, you are taking your 3 verts after which place them in a manner that covers the entire view port.

Texture2D colorMap; //DeviceContext.PixelShader.SetShaderResource(0, srv) units this on the cpu
sampler shadeSampler = sampler_state
{

AddressU = Clamp;
AddressV = Clamp;
Filter = MIN_MAG_MIP_POINT;
};



struct VertexShaderOutput
{
float4 Position : SV_Position;
float2 TexCoord : TEXCOORD0;

};
struct PixelShaderOutput
{
float4 Color : SV_Target0;

};



// outputs a full display screen triangle with screen-space coordinates
// enter: three empty vertices
VertexShaderOutput VSQuad( uint vertexID : SV_VertexID )
{
VertexShaderOutput consequence;
consequence.TexCoord = float2((vertexID << 1) & 2, vertexID & 2);
consequence.Position  = float4(consequence.TexCoord  * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f); // compute the place information
return consequence;
}


PixelShaderOutput PShader(VertexShaderOutput enter) 
{
 PixelShaderOutput output = (PixelShaderOutput)1;
float4 Color = colorMap.Sample(shadeSampler, enter.TexCoord); //the feel you wish to draw
output.Color =Color;    
 return output;
}


technique11 Render
{
cross P0
{

    SetVertexShader( CompileShader( vs_5_0, VSQuad() ) );
    SetPixelShader( CompileShader( ps_5_0, PShader() ) );
}
}

I simply had a take a look at the opposite query you requested and I must say that the man who wrote the reply there explains of the total display screen triangle stuff works significantly better.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here