Jump to content

  • Log In with Google      Sign In   
  • Create Account

[D3D11] Simple textured triangle won't draw


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 jdub   Members   -  Reputation: 419

Like
0Likes
Like

Posted 20 February 2013 - 07:09 PM

You might have seen my previous post about my textured quad not drawing.  This time, I wanted to just slightly modify the SlimDX SimpleTriangle example to see If I could get a simple textured triangle to draw.  This time, the application compiles and runs, but produces a black triangle instead of one with a texture on it.  Any Ideas on what's wrong?

 

Here's the C# code:

namespace SimpleTriangle
{

    [StructLayout(LayoutKind.Explicit, Size = 20)]
    public struct Vertex
    {
        [FieldOffset(0)]
        public Vector3 Position;

        [FieldOffset(12)]
        public Vector2 TexCoord;
    }

    static class Program
    {
        static void Main()
        {
            Device device;
            SwapChain swapChain;
            ShaderSignature inputSignature;
            VertexShader vertexShader;
            PixelShader pixelShader;

            var form = new RenderForm("Tutorial 3: Simple Triangle");
            var description = new SwapChainDescription()
            {
                BufferCount = 2,
                Usage = Usage.RenderTargetOutput,
                OutputHandle = form.Handle,
                IsWindowed = true,
                ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags = SwapChainFlags.AllowModeSwitch,
                SwapEffect = SwapEffect.Discard
            };

            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, description, out device, out swapChain);

            // create a view of our render target, which is the backbuffer of the swap chain we just created
            RenderTargetView renderTarget;
            using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
                renderTarget = new RenderTargetView(device, resource);

            // setting a viewport is required if you want to actually see anything
            var context = device.ImmediateContext;
            var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);
            context.OutputMerger.SetTargets(renderTarget);
            context.Rasterizer.SetViewports(viewport);

            // load and compile the vertex shader
            using (var bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
            {
                inputSignature = ShaderSignature.GetInputSignature(bytecode);
                vertexShader = new VertexShader(device, bytecode);
            }

            // load and compile the pixel shader
            using (var bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "PShader", "ps_4_0", ShaderFlags.None, EffectFlags.None))
                pixelShader = new PixelShader(device, bytecode);

            //load texture and pass it's ResourceView to the pixel shader
            String file = "C:/Users/Jared/Documents/Visual Studio 2010/Projects/Dx11Test/Dx11Test/Texture.bmp";
            Texture2D texture = Texture2D.FromFile(device, file);
            ShaderResourceView textureView = new ShaderResourceView(device, texture);

            
            // create test vertex data, making sure to rewind the stream afterward
            var vertices = new DataStream(Marshal.SizeOf(typeof(Vertex)) * 3, true, true);
            vertices.Write(new Vertex() { Position = new Vector3(0.0f, 0.5f, 0.5f), TexCoord = new Vector2(0.5f, 0.0f) });
            vertices.Write(new Vertex() { Position = new Vector3(0.5f, -0.5f, 0.5f), TexCoord = new Vector2(1.0f, 1.0f) });
            vertices.Write(new Vertex() { Position = new Vector3(-0.5f, -0.5f, 0.5f), TexCoord = new Vector2(0.0f, 1.0f) });
            vertices.Position = 0;
            
            // create the vertex layout and buffer
            var elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 0) };
            var layout = new InputLayout(device, inputSignature, elements);
            var vertexBuffer = new SlimDX.Direct3D11.Buffer(device, vertices, Marshal.SizeOf(typeof(Vertex)) * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            // configure the Input Assembler portion of the pipeline with the vertex data
            context.InputAssembler.InputLayout = layout;
            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 20, 0));

            // set the shaders/shader resources
            context.VertexShader.Set(vertexShader);
            context.PixelShader.Set(pixelShader);
            context.VertexShader.SetShaderResource(textureView, 1);


            // set the sampler
            SamplerDescription sampleDescr = new SamplerDescription();
            sampleDescr.Filter = Filter.MinMagMipLinear;
            sampleDescr.AddressU = TextureAddressMode.Wrap;
            sampleDescr.AddressV = TextureAddressMode.Wrap;
            sampleDescr.AddressW = TextureAddressMode.Wrap;
            sampleDescr.MipLodBias = 0.0f;
            sampleDescr.MaximumAnisotropy = 1;
            sampleDescr.ComparisonFunction = Comparison.Always;
            sampleDescr.BorderColor = System.Drawing.Color.White;
            sampleDescr.MinimumLod = 0;
            sampleDescr.MaximumLod = float.MaxValue;

            SamplerState samplerState = SamplerState.FromDescription(device, sampleDescr);

            context.PixelShader.SetSampler(samplerState, 0);

            // prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
            using (var factory = swapChain.GetParent<Factory>())
                factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);

            // handle alt+enter ourselves
            form.KeyDown += (o, e) =>
            {
                if (e.Alt && e.KeyCode == Keys.Enter)
                    swapChain.IsFullScreen = !swapChain.IsFullScreen;
            };

            // handle form size changes
            form.UserResized += (o, e) =>
            {
                renderTarget.Dispose();

                swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
                using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
                    renderTarget = new RenderTargetView(device, resource);

                context.OutputMerger.SetTargets(renderTarget);
            };

            MessagePump.Run(form, () =>
            {
                // clear the render target to a soothing blue
                context.ClearRenderTargetView(renderTarget, new Color4(0.5f, 0.5f, 1.0f));

                // draw the triangle
                context.Draw(3, 0);
                swapChain.Present(0, PresentFlags.None);
            });

            // clean up all resources
            // anything we missed will show up in the debug output
            vertices.Close();
            vertexBuffer.Dispose();
            layout.Dispose();
            inputSignature.Dispose();
            vertexShader.Dispose();
            pixelShader.Dispose();
            renderTarget.Dispose();
            swapChain.Dispose();
            device.Dispose();
        }
    }
}

 

 

Here's my shader code:

SamplerState SamplerType;
Texture2D Texture;

struct VShaderInput
{
	float4 position : POSITION;
	float2 TexCoord : TEXCOORD0;
};

struct PShaderInput
{
	float4 position : SV_POSITION;
	float2 TexCoord : TEXCOORD0;
};

PShaderInput VShader(VShaderInput input)
{
	PShaderInput output = (PShaderInput)0;
	
	output.position = input.position;
	output.TexCoord = input.TexCoord;

	return output;
}

float4 PShader(PShaderInput input) : SV_Target
{
	float4 textureSample = Texture.Sample(SamplerType, input.TexCoord);

	return textureSample;
}

 

 

 


J.W.

Sponsor:

#2 Jason Z   Crossbones+   -  Reputation: 5300

Like
0Likes
Like

Posted 20 February 2013 - 07:47 PM

Usually a black texture result means either that the texture coordinates are out of range, or that the texture resource isn't properly bound to the pipeline.  Have you tried to use PIX and/or the VS2012 Graphics Debugger to inspect your rendered frames?  If not, it is worth the time to get acquainted with these tools, because they will help you understand what is going on with your application (both in times when things aren't working, and also when things are working but you want to collect data about your usage of the API)!



#3 jdub   Members   -  Reputation: 419

Like
0Likes
Like

Posted 20 February 2013 - 10:09 PM

Okay, got it fixed.  Turned out that I had bound the TextureResourceView to the wrong slot inside the shader.

 

I use PIX but I'm not exactly sure how I would read memory to figure this out.  From what I see, PIX only shows which resources I've created, not where/how they are bound to the pipeline.


J.W.

#4 Jason Z   Crossbones+   -  Reputation: 5300

Like
0Likes
Like

Posted 20 February 2013 - 10:12 PM

Okay, got it fixed.  Turned out that I had bound the TextureResourceView to the wrong slot inside the shader.

 

I use PIX but I'm not exactly sure how I would read memory to figure this out.  From what I see, PIX only shows which resources I've created, not where/how they are bound to the pipeline.

If you capture a frame by hitting F12, then go to a draw call in the lower half of the screen, then you will be able to right click on the textures being bound in your Set* calls.  This is a quick and easy way to see if you are really doing what you think you are.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS