Original Post
I tried posting this on the XNA community forums but I got no such luck, just questions as to what I'm attempting. Now, my problem here is that I'm creating a texture, filling it full of noise. Then passing it to the shader as a parameter, however the shader won't sample the texture and return anything but black. I know the texture has been set correctly, I've read back the data and it's the same I put in it. But that doesn't explain why when I pass it the shader acts as if it wasn't set and just returns nothing...
private void UpdateSurfaceNoiseGPU(GameTime gameTime)
{
if (generateList.Count > 0 == false)
return;
// device
GraphicsDevice device = game.GraphicsDevice;
Vector2 viewportSize = new Vector2(blockWidth, blockHeight);
// noise info
Vector4 noiseInfo = generateList[0];
int noiseWidth = (int)noiseInfo.X;
int noiseHeight = (int)noiseInfo.Y;
Texture2D noiseTexture = new Texture2D(device, noiseWidth, noiseHeight, 0, TextureUsage.None, SurfaceFormat.Color);
Color[] noiseData = new Color[noiseWidth * noiseHeight];
for(int x = 0; x < noiseWidth; x++)
{
for(int y = 0; y < noiseHeight; y++)
{
noiseData[x + y * noiseWidth] = new Color( (byte)(Util.ImageProcessing.PerlinNoise.Noise2D(x, y) * 255), 0, 0, 0 );
}
}
noiseTexture.SetData<Color>(noiseData);
// setup shader constants
shaderGenerate.Parameters["ViewportSize"].SetValue(viewportSize);
shaderGenerate.Parameters["Noise"].SetValue(noiseTexture);
// draw on to surface
for (UInt32 x = 0; x < mapBlockWidthNum; x++)
{
for (UInt32 y = 0; y < mapBlockHeightNum; y++)
{
TerrainBlock block = mapBlocks[x, y];
// set the render target, draw and swap
device.SetRenderTarget(0, mapSwapBlock);
batch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None);
shaderGenerate.Begin();
shaderGenerate.CurrentTechnique.Passes[0].Begin();
batch.Draw(block.SurfaceTexture, Vector2.Zero, Color.White);
shaderGenerate.CurrentTechnique.Passes[0].End();
shaderGenerate.End();
batch.End();
device.SetRenderTarget(0, null);
FlipBlockBackBuffer(block);
}
}
generateList.Clear();
noiseTexture.Dispose();
device.SetRenderTarget(0, null);
}
The problem exists here: shaderGenerate.Parameters["Noise"].SetValue(noiseTexture); The shader I'm using is a modified version of the SpriteBatch shader, which works in my other process for terrain painting but not this.
// SpriteBatch default variables
texture Noise;
sampler NoiseSampler = sampler_state { Texture = <Noise>; MipFilter = Linear; MinFilter = Linear; MagFilter = Linear; AddressU = Clamp; AddressV = Clamp; };
// spritebatch vertex code...
void SpritePixelShader(inout float4 color : COLOR0, float2 texCoord : TEXCOORD0)
{
float4 surface = tex2D(TextureSampler, texCoord);
float4 noise = tex2D(NoiseSampler, texCoord);
color = noise;
}
Now, I must be doing something completely wrong. Because my other method works and is similar, I can even pass in textures to the shader. I'm not sure where to approach this because this texture needs to be a shader parameter and not passed in with the Draw call..


