Accessing multiple textures in shader

Started by
4 comments, last by h3ro 14 years, 1 month ago
Hallo, I am having a problem with accessing two textures at the same time in a shader that I am working on and was wondering if anyone could take a look? This works fine, it draws my sprite on the screen which makes me believe that the texture is fine.

  render.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
            render.SpriteBatch.Draw(m_blurBuffer.GetTexture(), new Rectangle(0, 0, m_blurBuffer.GetTexture().Width, m_blurBuffer.GetTexture().Height), Color.White);
            render.SpriteBatch.End();
If I replace the above code with this, I can no longer access the texture. I can access the texture that is rendered using sprite.render (its just a wrapper for spritebatch.draw()), but there is no texture in second texture slot.

            render.Graphics.Textures[1] = m_blurBuffer.GetTexture();

            render.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
            m_rimLightEffect.Begin();
            m_rimLightEffect.CurrentTechnique.Passes[0].Begin();
            sprite.render(render.SpriteBatch, cameraPos);
            render.SpriteBatch.End();
            m_rimLightEffect.CurrentTechnique.Passes[0].End();
            m_rimLightEffect.End();
Here is my texture samplers from the shader:

sampler TextureSampler0 : register(s0);  
sampler TextureSampler1 : register(s1);  
Thanks :)
Advertisement
Does it work if you set the texture after calling SpriteBatch.Begin?
No, it makes no difference.

It might be related to the problem, not sure, but when I change rendertarget, my screen goes purple and everything I have drawn up until that point gets removed.
I would suggest using PIX to look at what's happening in the frame. You can view the device state (including which textures are bound) at any point in the frame, and also just look for SetTexture calls to see when the state is changing. The tutorial in my signature can help you get started.

As for the RenderTarget issue, is this also your thread? Either way I explained the issue there, so that should help you out.
I looked at it in PIX as you suggested, and it all looks fine to me (but then again, I have never used it before)

Here is all my render targets. (Why is there so many? I only create two)
[image]http://i.imagehost.org/0245/renderTargetDebug.jpg[/image]

And by looking though the callstack in PIX I have found this line.
When I look at the texture it is setting, it has my blurred image.
IDirect3DDevice9::SetTexture(1, 0x0607AFD8)

Here is my rendering code. If I change the showBlurBuffer to show the blurbuffer, it shows fine, but I can still not access it in my shader
m_graphics.GraphicsDevice.Clear(new Color(0, 0, 0, 0));// Change render target so that we draw all our textures onto the rendertarget.// This way we dont need to resolve the backbufferm_graphics.GraphicsDevice.SetRenderTarget(0, m_renderTarget);// Draw all the sprites that has a rimlight applid to them first, and then blur themint renderIndex = (int)RenderBatch.Rim_light;SpriteBatch.Begin();for (int i = 0; i < m_renderList[renderIndex].Count; i++){    BaseSprite sprite = m_renderList[renderIndex].Sprite;    sprite.render(SpriteBatch, cameraPosition); }SpriteBatch.End();m_graphics.GraphicsDevice.SetRenderTarget(0, null);RenderTarget2D blurredRimLight;m_blurEffect.Render(m_renderTarget.GetTexture(), out blurredRimLight);// Draw all the sprites that should be effected by the bloom filter.// Set texture1 to be the blurred rimlight sprites, so that the sprites // that has a rimlight can use it.m_graphics.GraphicsDevice.SetRenderTarget(0, null);    if (!showBlurBuffer){    renderIndex = (int)RenderBatch.Rim_light;    Graphics.Textures[1] = blurredRimLight.GetTexture(); // This should make the texture available in the shader?    SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);    for (int i = 0; i < m_renderList[renderIndex].Count; i++)    {        BaseSprite sprite = m_renderList[renderIndex].Sprite;        SpriteEffectBase effect = m_renderList[renderIndex].Effect;        effect.Begin();        effect.Draw(sprite, cameraPosition, this, m_renderTarget);        effect.End();    }    SpriteBatch.End();}else{    SpriteBatch.Begin();    SpriteBatch.Draw(blurredRimLight.GetTexture(), new Rectangle(0, 0, ScreenWidth, ScreenHeight), Color.White);    SpriteBatch.End();}return;


My shader. TextureSampler0 has data, but TextureSampler1 does not.
sampler TextureSampler0 : register(s0);  sampler TextureSampler1 : register(s1);  float4 PixelShaderFunction(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0  {   	return tex2D(TextureSampler0, texCoord);}


Any ideas?

[Edited by - h3ro on March 7, 2010 9:35:44 AM]
Hey I have been working on this the whole day and I have made some progress. It looks like the textures are fine and I can access both of them. The reason why I could not see the second one is that it was not in the "correct" position.

First I render some of my sprites to a rendertarget, then I am trying to access them when I draw the sprite the second time. I think the problem is that when I am drawing the second time, I am doing the usual Draw(sprite, pos, sourceRect...) and sourceRect is where my sprite is on the sprite sheet, but I am guessing that this will be used to get the texel from the second texture as well? If that is the case, how do I get around it?

Regards,

This topic is closed to new replies.

Advertisement