Getting vertices from a buffer (HLSL) on XNA

Hi and sorry for the obscure name:} I will try to explain the best I can.

First of all, I am new to HLSL, but I understand the assembly line and materials that come from the wizarding world. What I'm trying to do is use gpu for general computing (GPGPU).

What I don't know: how can I read * vertices (which were converted using vertex shaders) back to my xna application? I read something about using gpu texture memory, but I can not find anything solid ...

Thanks in advance for any information / feedback! :-)

* not sure if possible, due to the rasterizer and pixel shader (if any), I mean, in the end, it's all about pixels, right?

+3
source share
2 answers

As far as I know, this is not possible at all.

What exactly are you trying to do? Perhaps there is another solution

EDIT :: Considering the comment. If all you want to do is general GPU vector computing, try doing them in a pixel shader and not in a vertex shader.

So, for example, let's say you want to make two vectors cross, first we need to write the data to the texture

//Data must be in the 0-1 range before writing into the texture, so you'll need to scale everything appropriately
Vector4 a = new Vector4(1, 0, 1, 1);
Vector4 b = new Vector4(0, 1, 0, 0);

Texture2D dataTexture = new Texture2D(device, 2, 1);
dataTexture.SetData<Vector4>(new Vector4[] { a, b });

So now we have a 2 * 1 texture with data that renders the texture just using spritebatch and effect:

Effect gpgpu;
gpgpu.Begin();
gpgpu.CurrentTechnique = gpgpu.Techniques["DotProduct"];
gpgpu.CurrentTechnique.Begin();
spriteBatch.Begin();
gpgpu.CurrentTechnique.Passes[0].Begin();
spriteBatch.Draw(dataTexture, new Rectangle(0,0,2,1), Color.White);
spriteBatch.end();
gpgpu.CurrentTechnique.Passes[0].End();
gpgpu.CurrentTechnique.End();

Now we need the gpgpu effect, which I showed above. This is just a standard shader post processor that looks something like this:

sampler2D DataSampler = sampler_state
{
    MinFilter = Point;
    MagFilter = Point;
    MipFilter = Point;
    AddressU = Clamp;
    AddressV = Clamp;
};

float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
{
    float4 A = tex2D(s, texCoord);
    float4 B = tex2D(s, texCoord + float2(0.5, 0); //0.5 is the size of 1 pixel, 1 / textureWidth
    float d = dot(a, b)
    return float4(d, 0, 0, 0);
}

technique DotProduct
{
    pass Pass1
    {
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}

A B , B B - . ( )

Vector4[] v = new Vector4[2];
dataTexture.GetData(v);
float dotOfAandB = v[0];
float dotOfBandB = v[1];

! , , :)

+2
0

Source: https://habr.com/ru/post/1722918/


All Articles