   [SlimDX] Simple Cube (need help)
  Hi all,I am trying to reproduce a simple cube rendering from the Direct3D 11 Tutorial 4 of last DirectX SDK release. But the render windows doesn't show anything, it's just cleared with blue but no cube ...I really don't understand where I am doing it wrong, it's a quite simple exemple.Here is my code, you juste have to past this into Program.cs of a new project and add a reference to SlimDX in your project.using System;using System.Collections.Generic;using System.Windows.Forms;using SlimDX;using SlimDX.D3DCompiler;using D3D11 = SlimDX.Direct3D11;using DXGI = SlimDX.DXGI;using SlimDX.Windows;namespace DX11_Cube{    struct SimpleVertex    {   public SimpleVertex(Vector3 pos, Vector4 color)   {  Pos = pos;  Color = color;   }   Vector3 Pos;   Vector4 Color;    };    public struct ConstantBuffer    {   public Matrix mWorld;   public Matrix mView;   public Matrix mProjection;    };    public class D3D11CubeSample    {   public IntPtr g_hWnd = IntPtr.Zero;   public D3D11.DriverType g_driverType = SlimDX.Direct3D11.DriverType.Null;   public D3D11.FeatureLevel g_featureLevel = SlimDX.Direct3D11.FeatureLevel.Level_11_0;   public D3D11.Device g_pd3dDevice = null;   public D3D11.DeviceContext g_pImmediateContext = null;   public DXGI.SwapChain g_pSwapChain = null;   public D3D11.RenderTargetView g_pRenderTargetView = null;   public D3D11.VertexShader g_pVertexShader = null;   public D3D11.PixelShader g_pPixelShader = null;   public D3D11.InputLayout g_pVertexLayout = null;   public D3D11.Buffer g_pVertexBuffer = null;   public D3D11.Buffer g_pIndexBuffer = null;   public D3D11.Buffer g_pConstantBuffer = null;   public Matrix g_World;   public Matrix g_View;   public Matrix g_Projection;   public D3D11CubeSample(IntPtr winHandle)   {  g_hWnd = winHandle;   }   public bool InitDevice(int width, int height)   {  D3D11.FeatureLevel[] featureLevels = new D3D11.FeatureLevel[]  { D3D11.FeatureLevel.Level_11_0, D3D11.FeatureLevel.Level_10_1, D3D11.FeatureLevel.Level_10_0,  };  DXGI.SwapChainDescription sd = new DXGI.SwapChainDescription();  sd.BufferCount = 1;  sd.ModeDescription = new SlimDX.DXGI.ModeDescription()  { Width = width, Height = height, Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, RefreshRate = new Rational(60, 1)  };  sd.Usage = SlimDX.DXGI.Usage.RenderTargetOutput;  sd.OutputHandle = g_hWnd;  sd.SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0);  sd.IsWindowed = true;  D3D11.Device.CreateWithSwapChain(null, D3D11.DriverType.Hardware, SlimDX.Direct3D11.DeviceCreationFlags.None, sd, out g_pd3dDevice, out g_pSwapChain);  g_pRenderTargetView = new SlimDX.Direct3D11.RenderTargetView(g_pd3dDevice, D3D11.Texture2D.FromSwapChain<D3D11.Texture2D>(g_pSwapChain, 0));  g_pd3dDevice.ImmediateContext.OutputMerger.SetTargets(g_pRenderTargetView);  // Setup the viewport  D3D11.Viewport vp = new SlimDX.Direct3D11.Viewport();  vp.Width = (float)width;  vp.Height = (float)height;  vp.MinZ = 0.0f;  vp.MaxZ = 1.0f;  vp.X = 0;  vp.Y = 0;  g_pd3dDevice.ImmediateContext.Rasterizer.SetViewports(vp);  // Create VS  string vsCompileError = "";  ShaderBytecode shaderByteCode = ShaderBytecode.CompileFromFile("Tutorial04.fx", "VS", "vs_4_0", ShaderFlags.EnableStrictness | ShaderFlags.Debug, EffectFlags.None, null, null, out vsCompileError);  g_pVertexShader = new SlimDX.Direct3D11.VertexShader(g_pd3dDevice, shaderByteCode);  // Create the input layout  D3D11.InputElement[] inputElements = new D3D11.InputElement[]  { new D3D11.InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32_Float, 0, 0, SlimDX.Direct3D11.InputClassification.PerVertexData, 0),  new D3D11.InputElement("COLOR", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 12, 0, SlimDX.Direct3D11.InputClassification.PerVertexData, 0)  };  g_pVertexLayout = new D3D11.InputLayout(g_pd3dDevice, shaderByteCode, inputElements);  // Set the input layout  g_pd3dDevice.ImmediateContext.InputAssembler.InputLayout = g_pVertexLayout;  // Create PS  string psCompileError = "";  ShaderBytecode shaderByteCodePS = ShaderBytecode.CompileFromFile("Tutorial04.fx", "PS", "ps_4_0", ShaderFlags.EnableStrictness | ShaderFlags.Debug, EffectFlags.None, null, null, out psCompileError);  g_pPixelShader = new SlimDX.Direct3D11.PixelShader(g_pd3dDevice, shaderByteCodePS);  // Create vertices array  SimpleVertex[] m_Vertices = new SimpleVertex[] {new SimpleVertex(new Vector3( -1.0f, 1.0f, -1.0f ), new Vector4( 0.0f, 0.0f, 1.0f, 1.0f )),new SimpleVertex(new Vector3( 1.0f, 1.0f, -1.0f ), new Vector4( 0.0f, 1.0f, 0.0f, 1.0f )),new SimpleVertex(new Vector3( 1.0f, 1.0f, 1.0f ), new Vector4( 0.0f, 1.0f, 1.0f, 1.0f )),new SimpleVertex(new Vector3( -1.0f, 1.0f, 1.0f ), new Vector4( 1.0f, 0.0f, 0.0f, 1.0f )),new SimpleVertex(new Vector3( -1.0f, -1.0f, -1.0f ), new Vector4( 1.0f, 0.0f, 1.0f, 1.0f )),new SimpleVertex(new Vector3( 1.0f, -1.0f, -1.0f ), new Vector4( 1.0f, 1.0f, 0.0f, 1.0f )),new SimpleVertex(new Vector3( 1.0f, -1.0f, 1.0f ), new Vector4( 1.0f, 1.0f, 1.0f, 1.0f )),new SimpleVertex(new Vector3( -1.0f, -1.0f, 1.0f ), new Vector4( 0.0f, 0.0f, 0.0f, 1.0f )) };  // Write vertex data to a DataStream  DataStream vbStream = new DataStream(28 * 8, true, true);  vbStream.WriteRange<SimpleVertex>(m_Vertices);  vbStream.Position = 0;  // Create vertex buffer  g_pVertexBuffer = new D3D11.Buffer(g_pd3dDevice, vbStream, new SlimDX.Direct3D11.BufferDescription()  { BindFlags = SlimDX.Direct3D11.BindFlags.VertexBuffer, OptionFlags = SlimDX.Direct3D11.ResourceOptionFlags.None, CpuAccessFlags = SlimDX.Direct3D11.CpuAccessFlags.None, SizeInBytes = 28 * 8, Usage = SlimDX.Direct3D11.ResourceUsage.Default  });g_pd3dDevice.ImmediateContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding() {Buffer = g_pVertexBuffer,Offset = 0,Stride = 28 });  // Create indices array  byte[] m_Indices = new byte[] {3,1,0,2,1,3,0,5,4,1,5,0,3,4,7,0,4,3,1,6,5,2,6,1,2,7,6,3,7,2,6,4,5,7,4,6, };  // Write index data to a DataStream  DataStream ibStream = new DataStream(sizeof(byte) * 36, true, true);  ibStream.WriteRange<byte>(m_Indices);  ibStream.Position = 0;  // Create index buffer  g_pIndexBuffer = new D3D11.Buffer(g_pd3dDevice, ibStream, new SlimDX.Direct3D11.BufferDescription()  { BindFlags = SlimDX.Direct3D11.BindFlags.IndexBuffer, OptionFlags = SlimDX.Direct3D11.ResourceOptionFlags.None, CpuAccessFlags = SlimDX.Direct3D11.CpuAccessFlags.None, SizeInBytes = sizeof(byte) * 36, Usage = SlimDX.Direct3D11.ResourceUsage.Default  });g_pd3dDevice.ImmediateContext.InputAssembler.SetIndexBuffer(g_pIndexBuffer, SlimDX.DXGI.Format.R16_UInt, 0);  // Set primitive topology  g_pd3dDevice.ImmediateContext.InputAssembler.PrimitiveTopology = D3D11.PrimitiveTopology.TriangleList;  // Initialize the world matrix  g_World = Matrix.Identity;  // Initialize the view matrix  Vector3 Eye = new Vector3(0.0f, 1.0f, -5.0f);  Vector3 At = new Vector3(0.0f, 1.0f, 0.0f);  Vector3 Up = new Vector3(0.0f, 1.0f, 0.0f);  g_View = Matrix.LookAtLH(Eye, At, Up);  // Initialize the projection matrix  g_Projection = Matrix.PerspectiveFovLH((float)(Math.PI / 2), (float)width / (float)height, 0.01f, 100.0f);  // Create Constant buffer  ConstantBuffer cb = new ConstantBuffer();  cb.mWorld = Matrix.Transpose(g_World);  cb.mProjection = Matrix.Transpose(g_Projection);  cb.mView = Matrix.Transpose(g_View);  DataStream cbStream = new DataStream(192, true, true);  cbStream.Write<ConstantBuffer>(cb);  cbStream.Position = 0;  g_pConstantBuffer = new D3D11.Buffer(g_pd3dDevice, cbStream, new D3D11.BufferDescription()  { BindFlags = SlimDX.Direct3D11.BindFlags.ConstantBuffer, CpuAccessFlags = SlimDX.Direct3D11.CpuAccessFlags.Write, OptionFlags = SlimDX.Direct3D11.ResourceOptionFlags.None, Usage = SlimDX.Direct3D11.ResourceUsage.Dynamic, SizeInBytes = 192, StructureByteStride = 0  });  return true;   }   public void Render()   {  // Clear  g_pd3dDevice.ImmediateContext.ClearRenderTargetView(g_pRenderTargetView, new Color4(0.0f, 0.125f, 0.3f, 1.0f));  // Draw  g_pd3dDevice.ImmediateContext.VertexShader.Set(g_pVertexShader);  g_pd3dDevice.ImmediateContext.VertexShader.SetConstantBuffer(g_pConstantBuffer, 0);  g_pd3dDevice.ImmediateContext.PixelShader.Set(g_pPixelShader);  g_pd3dDevice.ImmediateContext.DrawIndexed(36, 0, 0);  // Present  g_pSwapChain.Present(0, 0);   }    }    static class Program    {   /// <summary>   /// The main entry point for the application.   /// </summary>   [STAThread]   static void Main()   {  RenderForm form = new RenderForm();  D3D11CubeSample cubeSample = new D3D11CubeSample(form.Handle);  if (!cubeSample.InitDevice(form.ClientSize.Width, form.ClientSize.Height)) return;  MessagePump.Run(form, () =>  { cubeSample.Render();  });   }    }}Thank if anyone can tell me where is my mistake.[Edited by - karnalta on July 9, 2010 1:28:32 PM]
  My error was the IndexBuffer binding format, I used the same than the C++ sample, but C++ use DWORD (16 bit) and in C# I used int (32 bit). So I just had to change this line :g_pd3dDevice.ImmediateContext.InputAssembler.SetIndexBuffer(g_pIndexBuffer, SlimDX.DXGI.Format.R16_UInt, 0);intog_pd3dDevice.ImmediateContext.InputAssembler.SetIndexBuffer(g_pIndexBuffer, SlimDX.DXGI.Format.R32_UInt, 0);   