Jump to content
  • Advertisement
Sign in to follow this  
lucasite

DX11 [SlimDx] Nothing Being Rendered [Solved]

This topic is 2893 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

Hi,
I need some an extra pair of eyes folks, Ive been going crazy trying to figure this out for a few hours now.
I took the sample MiniTri code for Dx11.
All I wanted to do was change the code to include a camera.
So I added 2 matrices, one for view, one for projection.
I also changed the effect file to mul(...) the world/view/projection.

But the result is just a black screen. I cant figure it out. The effect file looks correct. Ive compare with out effect files.
And the matrices look correct. So I have no idea why its not working.
Ive also tried positioning the camera in different locations (incase its a culling issue), still nothing.

This is the code:
using System;
using System.Drawing;
using System.Windows.Forms;
using SlimDX;
using SlimDX.Direct3D11;
using SlimDX.DXGI;
using SlimDX.Windows;
using SlimDX.D3DCompiler;
using Device = SlimDX.Direct3D11.Device;

namespace Dx11Project1
{
static class Program
{
[STAThread]
static void Main()
{
var form = new RenderForm("SlimDX - MiniTri Direct3D 11 Sample");
var desc = new SwapChainDescription()
{
BufferCount = 1,
ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = form.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};

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

Factory factory = swapChain.GetParent<Factory>();
factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);

Texture2D backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
var renderView = new RenderTargetView(device, backBuffer);
var bytecode = ShaderBytecode.CompileFromFile("MiniTri.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None);
var effect = new Effect(device, bytecode);
var technique = effect.GetTechniqueByIndex(0);
var pass = technique.GetPassByIndex(0);
var layout = new InputLayout(device, pass.Description.Signature, new[] {
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)
});

// setup triangle vertices
var stream = new DataStream(3 * 32, true, true);
stream.WriteRange(new[] {
new Vector4(-5.0f, 0.0f, 0.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
new Vector4(0.0f, 5.0f, 0.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
new Vector4(5.0f, 0.0f, 0.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f)
});
stream.Position = 0;

var vertices = new SlimDX.Direct3D11.Buffer(device, stream, new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = 3 * 32,
Usage = ResourceUsage.Default
});
stream.Dispose();

device.ImmediateContext.OutputMerger.SetTargets(renderView);
device.ImmediateContext.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));

// Setup camera views
var view = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, 5.0f), Vector3.Zero, Vector3.UnitY);
var projection = Matrix.PerspectiveLH(form.ClientSize.Width, form.ClientSize.Height, 0.0f, 100.0f);

MessagePump.Run(form, () =>
{
device.ImmediateContext.ClearRenderTargetView(renderView, Color.Black);

// set effect variables
var viewVariable = effect.GetVariableByName("View").AsMatrix();
var worldVariable = effect.GetVariableByName("World").AsMatrix();
var projVariable = effect.GetVariableByName("Projection").AsMatrix();

viewVariable.SetMatrix(view);
projVariable.SetMatrix(projection);
worldVariable.SetMatrix(Matrix.Identity);

device.ImmediateContext.InputAssembler.InputLayout = layout;
device.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
device.ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, 32, 0));

for (int i = 0; i < technique.Description.PassCount; ++i)
{
pass.Apply(device.ImmediateContext);
device.ImmediateContext.Draw(3, 0);
}

swapChain.Present(0, PresentFlags.None);
});

bytecode.Dispose();
vertices.Dispose();
layout.Dispose();
effect.Dispose();
renderView.Dispose();
backBuffer.Dispose();
device.Dispose();
swapChain.Dispose();
}
}
}



And this is the effect file:
float4x4 World;
float4x4 View;
float4x4 Projection;

struct VS_IN
{
float4 pos : POSITION;
float4 col : COLOR;
};

struct PS_IN
{
float4 pos : SV_POSITION;
float4 col : COLOR;
};

PS_IN VS( VS_IN input )
{
PS_IN output = (PS_IN)0;

float4 worldPosition = mul(input.pos, World);
float4 viewPosition = mul(worldPosition, View);
output.pos = mul(viewPosition, Projection);

output.col = input.col;

return output;
}

float4 PS( PS_IN input ) : SV_Target
{
return input.col;
}

technique10 Render
{
pass P0
{
SetGeometryShader( 0 );
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetPixelShader( CompileShader( ps_4_0, PS() ) );
}
}




Is there a way of debugging Slimdx? Finding out whats going on with directx? Im running Win7 x64, so Pix doesnt work if I try.

Share this post


Link to post
Share on other sites
Advertisement
Your znear for the perspective transformation seems off. Try something > 0, e.g. 0.1.

Can't help you with PIX, unfortunately, I'm still on XP.

Share this post


Link to post
Share on other sites
Unfortunately that didnt help. However it did point me in the right direction.
It seems I shouldve been using:
var projection = Matrix.PerspectiveFovLH(45.0f, form.ClientSize.Width / (float)form.ClientSize.Height, 1.0f, 1000.0f);

I need to investigate the difference between PerspectiveLH and PerspectiveFovLH.

Share this post


Link to post
Share on other sites
When you call PerspectiveFOV, be sure to pass the angle in radians, not degrees.


Edit: Nevermind, your vertices are winding in the correct order.

The function PerspectiveLH takes its width and height in camera space coordinates, but you are passing screen space coordinates.

Share this post


Link to post
Share on other sites
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!