Jump to content
  • Advertisement
Sign in to follow this  
jdub

Problem rendering multiple effects with SlimDX

This topic is 2024 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using two effects.  One handles the rendering of a 2D font from a bitmap on the screen while another is used to render textured primitives in 3D space.  The issue I am having is that when trying to render a textured quad (using my textured primitive effect) and a piece of text (using the font effect) the outputted quads appear correctly on the screen.  However, the quad is rendered with the font texture mapped to it and the font is rendered with the quad texture mapped to it.

 

Here is the main rendering function:

 

public void Render()
        {
            this.GraphicsPipeline.Context.ClearRenderTargetView(this.GraphicsPipeline.RenderTargetView, new SlimDX.Color4(Color.Green));

            this.GraphicsPipeline.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            this.EffectType = EffectMode.Textured;

            this.GraphicsPipeline.Projection = Matrix.OrthoLH(this.form.Bounds.Width, this.form.Bounds.Height, 0.0f, 1.0f);
            //this.GraphicsPipeline.World = Matrix.Identity;
            this.GraphicsPipeline.View = Matrix.Identity;

            this.images[0].Transform = Matrix.Identity;
            this.images[0].Render(this.GraphicsPipeline.Context);
            //start rendering all font

            this.EffectType = EffectMode.Font;

            this.GraphicsPipeline.Projection = Matrix.OrthoLH(this.form.Bounds.Width, this.form.Bounds.Height, 0.0f, 1.0f);
            this.GraphicsPipeline.World = Matrix.Identity;

            this.fontEngine.Begin();
            this.fontEngine.DrawString("ABCDEFG", new Point(450, 450));
            this.fontEngine.End(this.GraphicsPipeline.Context);

            this.GraphicsPipeline.Present(0, SlimDX.DXGI.PresentFlags.None);
        }

Applying an effect is done by setting the EffectType property with the following relevant code:

set 
            {
                this.effectType = value;

                InputElement[] inputElements = 
                { 
                    new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0), 
                    new InputElement("TEXCOORD", 0, Format.R32G32_Float,0),
                    new InputElement("NORMAL", 0, Format.R32G32B32_Float, 0) 
                };

                if (value == EffectMode.Textured)
                {
                    this.technique = this.Effect.GetTechniqueByName("RenderTextured");
                    
                    this.GraphicsPipeline.Context.InputAssembler.InputLayout = new InputLayout(
                        this.GraphicsPipeline.Device,
                        this.Effect.GetTechniqueByName("RenderTextured").GetPassByIndex(0).Description.Signature,
                        inputElements);
                }
else if (value == EffectMode.Font)
                {
                    this.technique = this.Effect.GetTechniqueByName("RenderFont");

                    this.GraphicsPipeline.Context.InputAssembler.InputLayout = new InputLayout(
                        this.GraphicsPipeline.Device,
                        this.Effect.GetTechniqueByName("RenderFont").GetPassByIndex(0).Description.Signature,
                        inputElements);
                }

                System.Diagnostics.Debug.Assert(this.technique.GetPassByIndex(0).IsValid);

                this.technique.GetPassByIndex(0).Apply(this.GraphicsPipeline.Context);
            }

Here is the code which renders a textured quad:  Bitmap.Render()

public void Render(DeviceContext context)
        {
            this.renderer.Effect.GetVariableByName("Texture").AsResource().SetResource(this.textureRsrcView);

            EffectSamplerVariable samplerVariable = this.renderer.Effect.GetVariableByName("SampleType").AsSampler();
            samplerVariable.SetSamplerState(0, this.sampler);

            Matrix temp = this.renderer.GraphicsPipeline.World;

            this.renderer.GraphicsPipeline.World = this.Transform;

            //pipeline.ToggleZBuffer(false);

            context.InputAssembler.SetVertexBuffers(0, this.vBufferBinding);
            context.InputAssembler.SetIndexBuffer(this.iBuffer, SlimDX.DXGI.Format.R16_UInt, 0);
            context.DrawIndexed(6, 0, 0);

            this.renderer.GraphicsPipeline.World = temp;
            //pipeline.ToggleZBuffer(true);
        }

Here is the font rendering code:

public void End(DeviceContext context)
        {
            if (!this.isRendering)
                throw new InvalidOperationException();
          
            this.renderer.Effect.GetVariableByName("Texture").AsResource().SetResource(this.textureResourceView);

            context.InputAssembler.SetVertexBuffers(0, this.vBufferBinding);
            context.InputAssembler.SetIndexBuffer(this.iBuffer, Format.R16_UInt, 0);

            foreach (BatchedString batchedString in this.batchedText)
            {
                Point screenDrawPos = batchedString.screenPos;

                //draw each character as a quad
                foreach (char chr in batchedString.text)
                {
                    Vector4 drawPos = Bitmap.GetD3DCoords(screenDrawPos, this.screenBounds);
                    drawPos.Z = 0.0f;

                    int iBufferReadPos = (Convert.ToInt16(chr) - 32)*6;

                    this.renderer.GraphicsPipeline.World = Matrix.Translation(drawPos.X, drawPos.Y, drawPos.Z) * Matrix.Scaling(3.0f, 3.0f, 1.0f); 
                    this.renderer.GraphicsPipeline.Context.DrawIndexed(6, iBufferReadPos, 0);

                    screenDrawPos.X += charSet[Convert.ToInt16(chr) - 32].pixelWidth;
                }
            }

            //when finished clear batched text
            this.batchedText.Clear();
            this.isRendering = false;
        }

...and lastly the relevant effect code:

SamplerState SampleType;
Texture2D Texture;

cbuffer worldBuffer
{
	matrix world;	
};

cbuffer projectionBuffer
{
	matrix projection;
};

cbuffer viewBuffer
{
	matrix view;
};

cbuffer LightConstantBuffer
{
	float3 light_dir;
	float4 light_color;
	float padding;
};

struct VS_INPUT
{
	float4 Position : POSITION0;
	float2 TexCoord : TEXCOORD0;
	float3 Normal : NORMAL; //world space normal
};

struct PS_INPUT
{
	float4 Position : SV_POSITION;
	float2 TexCoord : TEXCOORD0;
	float3 Normal : NORMAL; //view-space normal
};

PS_INPUT FontVShader(VS_INPUT input) 
{
	PS_INPUT output = (PS_INPUT)0;

	output.TexCoord = input.TexCoord;
	output.Position = mul(input.Position, world);
	output.Position = mul(output.Position, projection);

	return output;
}

float4 FontPShader(PS_INPUT input) : SV_TARGET
{
	float3 color = Texture.Sample(SampleType, input.TexCoord);

	if(color.x == 0 && color.y == 0 && color.z == 0) // black is the alpha key for the font texture
		return float4(0.0f, 0.0f, 0.0f, 1.0f);

	return float4(color.x, color.y, color.z, 0.5f);
}

technique11 RenderFont
{
	pass P0
	{
		SetVertexShader( CompileShader( vs_4_0, FontVShader() ) );
		SetGeometryShader( NULL );
		SetPixelShader( CompileShader( ps_4_0, FontPShader() ) );
	}
}

technique11 RenderTextured
{
	pass P0
	{
		SetVertexShader( CompileShader( vs_4_0, VShader() ) );
		SetGeometryShader( NULL );
		SetPixelShader( CompileShader( ps_4_0, PShader(false, true) ) );
	}
}

I know it's a lot of code but this problem has me stumped and I'd greatly appreciate any help and/or suggestions you guys have to give!

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!