简体   繁体   中英

HLSL Perlin Noise and XNA

i need a real time Perlin Noise generation for a 2d game i am implementing. The CPU noise runs fine, but needs ~1 sec for half my screen size (1920 x 1080). I can scale it up about 4 times without too many artifacts but still that is a little slow assuming, that my cpu is pretty fast. So i want to implement it on the GPU. THe problem is i have no experience with HLSL and there are not too many tutorials on it out there... I have tried to implement this (i pretty much copied it all) and added this:

void SpriteVertexShader(inout float4 color    : COLOR0,
                        inout float2 texCoord : TEXCOORD0,
                        inout float4 position : SV_Position)
{
    position = mul(position, MatrixTransform);
}

technique Technique1
{
    pass Pass1
    {
        // TODO: set renderstates here.
        VertexShader = compile vs_3_0 SpriteVertexShader();
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}

at the end. I Call it like this:

Matrix projection = Matrix.CreateOrthographicOffCenter(0,
  GraphicsDevice.Viewport.Width,
  GraphicsDevice.Viewport.Height, 0, 0, 1);
Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
perlin.Parameters["MatrixTransform"].SetValue(halfPixelOffset * projection);
perlin.CurrentTechnique = perlin.Techniques["Technique1"];

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
perlin.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(new Texture2D(GraphicsDevice,1000,1000),
                               new Rectangle(0, 0, 500, 500), Color.White);
spriteBatch.End();

and i finally get a black texture. What am i doing wrong? I don't understand the vertex shader part... But here they say i have to change it so that i won't get the error: "that is too complex for the target shader model"...

Seems XNA doesn't support textures which were created like that:

texture permTexture
<
  string texturetype = "2D";
  string format = "l8";
  string function = "GeneratePermTexture";
  int width = 256, height = 1;
>;

float4 GeneratePermTexture(float p : POSITION) : COLOR
{
  return permutation[p * 256] / 255.0;
}

sampler permSampler = sampler_state
{
  texture = <permTexture>;
  AddressU  = Wrap;
  AddressV  = Clamp;
  MAGFILTER = POINT;
  MINFILTER = POINT;
  MIPFILTER = NONE;
};

You need to create perm texture and perm gradient outside of shader and then pass them as parameters into shader.

I started to write code but already found an example here: XNA 4.0 perlin noise using the gpu .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM