• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
AdeptStrain

DX11
[SOLVED]Viewport Transform giving weird resuts. Need a fresh set of eyes.

11 posts in this topic

Forgive the newbish plea for help but this is my first time using SlimDX and everything appears correct but I'm getting weird results (my quads are acting like they are being rendered straight to screenspace instead of being transformed by all the appropriate matrices). According to PIX, everything is as it should be until the viewport kicks in.

For reference, I'm outputting to a panel that is within a larger form (it's a simple level editor).

PIX Output:
[url="http://i.imgur.com/v9uca.png"]http://i.imgur.com/v9uca.png[/url]

Code -

Device Setup/Viewport Creation
[code] public DXRenderer(IntPtr outputHandle, System.Drawing.Size formSize)
{
SwapChainDescription swapDesc = new SwapChainDescription();
swapDesc.BufferCount = 1;
swapDesc.Usage = Usage.RenderTargetOutput;
swapDesc.OutputHandle = outputHandle;
swapDesc.IsWindowed = true;
swapDesc.ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm);
swapDesc.SampleDescription = new SampleDescription(1, 0);
swapDesc.Flags = SwapChainFlags.AllowModeSwitch;
swapDesc.SwapEffect = SwapEffect.Discard;

if (SlimDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, swapDesc, out m_d3dDevice, out m_swapChain).IsFailure)
{
System.Windows.Forms.MessageBox.Show("Failed to create DX11 device.");
System.Environment.Exit(1);
}

var factory = m_swapChain.GetParent<Factory>();
factory.SetWindowAssociation(outputHandle, WindowAssociationFlags.IgnoreAll);

m_d3dContext = m_d3dDevice.ImmediateContext;
m_renderTarget = new RenderTargetView(m_d3dDevice, SlimDX.Direct3D11.Resource.FromSwapChain<Texture2D>(m_swapChain, 0));
m_d3dContext.OutputMerger.SetTargets(m_renderTarget);

SlimDX.Direct3D11.Viewport viewPort = new Viewport(0.0f, 0.0f, (float)formSize.Width, (float)formSize.Height, 0.0f, 1.0f);
m_d3dContext.Rasterizer.SetViewports(viewPort);

Camera frameCamera = new Camera();
float aspect = (float)formSize.Width / (float)formSize.Height;
float fov = (float)System.Math.PI * 0.5f;
frameCamera.ProjectionMatrix = SlimDX.Matrix.PerspectiveFovLH(fov, aspect, 1.0f, 1000.0f);
}[/code]

Shader Setup:
[code] SlimDX.Direct3D11.Device device = DXRenderer.Instance.D3DDevice;
DataStream verticeStream;
// Setup Index/Vertex Buffers.
int vertexDeclSize = 24 * 4;
verticeStream = new DataStream(vertexDeclSize, false, true);
verticeStream.Write(new Vector4(0.0f, 0.0f, 0.0f, 0.0f));
verticeStream.Write(new Vector2(0.0f, 0.0f));
verticeStream.Write(new Vector4(0.0f, 1.0f, 0.0f, 0.0f));
verticeStream.Write(new Vector2(0.0f, 0.0f));
verticeStream.Write(new Vector4(1.0f, 1.0f, 0.0f, 0.0f));
verticeStream.Write(new Vector2(1.0f, 1.0f));
verticeStream.Write(new Vector4(1.0f, 0.0f, 0.0f, 0.0f));
verticeStream.Write(new Vector2(1.0f, 1.0f));
verticeStream.Position = 0;
m_vertexBuffer = new SlimDX.Direct3D11.Buffer(device, verticeStream, vertexDeclSize, SlimDX.Direct3D11.ResourceUsage.Dynamic, SlimDX.Direct3D11.BindFlags.VertexBuffer,
SlimDX.Direct3D11.CpuAccessFlags.Write, SlimDX.Direct3D11.ResourceOptionFlags.None, 0);
verticeStream.Close();
SlimDX.DataStream indexStream = new DataStream(sizeof(UInt16) * 6, false, true);
indexStream.Write((UInt16)0);
indexStream.Write((UInt16)1);
indexStream.Write((UInt16)2);
indexStream.Write((UInt16)0);
indexStream.Write((UInt16)2);
indexStream.Write((UInt16)3);
indexStream.Position = 0;
m_indexBuffer = new SlimDX.Direct3D11.Buffer(device, indexStream, sizeof(UInt16) * 6, SlimDX.Direct3D11.ResourceUsage.Default, SlimDX.Direct3D11.BindFlags.IndexBuffer,
SlimDX.Direct3D11.CpuAccessFlags.None, SlimDX.Direct3D11.ResourceOptionFlags.None, 0);
indexStream.Close();
// Compile shaders
SlimDX.D3DCompiler.ShaderBytecode vertexByteCode = SlimDX.D3DCompiler.ShaderBytecode.CompileFromFile("./Shaders/Sprite.fx", "VS", "vs_5_0", SlimDX.D3DCompiler.ShaderFlags.None, SlimDX.D3DCompiler.EffectFlags.None);
SlimDX.D3DCompiler.ShaderSignature inputSig = SlimDX.D3DCompiler.ShaderSignature.GetInputSignature(vertexByteCode);
m_vertexShader = new SlimDX.Direct3D11.VertexShader(device, vertexByteCode);
m_pixelShader = new SlimDX.Direct3D11.PixelShader(device, SlimDX.D3DCompiler.ShaderBytecode.CompileFromFile("./Shaders/Sprite.fx", "PS", "ps_5_0", SlimDX.D3DCompiler.ShaderFlags.None, SlimDX.D3DCompiler.EffectFlags.None));
m_inputLayout = new SlimDX.Direct3D11.InputLayout(device, inputSig, InputLayoutManager.TextureVertexLayout);

// Setup Constant Buffer and Texture Sampler State.
int matrixSize = (sizeof(float) * 4 * 4); // 4 rows, each with 4 values - x,y,z,w
DataStream constantStream;
constantStream = new DataStream(matrixSize * 3, true, true);
constantStream.Write(SlimDX.Matrix.Identity);
constantStream.Write(SlimDX.Matrix.Identity);
constantStream.Write(SlimDX.Matrix.Identity);
constantStream.Position = 0;
m_constantBuffer = new SlimDX.Direct3D11.Buffer(device, constantStream, matrixSize * 3, SlimDX.Direct3D11.ResourceUsage.Dynamic, SlimDX.Direct3D11.BindFlags.ConstantBuffer,
SlimDX.Direct3D11.CpuAccessFlags.Write, SlimDX.Direct3D11.ResourceOptionFlags.None, 0);
constantStream.Close();
SlimDX.Direct3D11.SamplerDescription samplerDesc = new SlimDX.Direct3D11.SamplerDescription();
samplerDesc.AddressU = SlimDX.Direct3D11.TextureAddressMode.Wrap;
samplerDesc.AddressV = SlimDX.Direct3D11.TextureAddressMode.Wrap;
samplerDesc.AddressW = SlimDX.Direct3D11.TextureAddressMode.Wrap;
samplerDesc.ComparisonFunction = SlimDX.Direct3D11.Comparison.Never;
samplerDesc.Filter = SlimDX.Direct3D11.Filter.MinMagMipLinear;
samplerDesc.MinimumLod = 0.0f;
samplerDesc.MaximumLod = float.MaxValue;
m_samplerState = SlimDX.Direct3D11.SamplerState.FromDescription(device, samplerDesc);[/code]

Actual Render call:
[code] public void RenderSprite(Texture texture, Vector3 position, Vector2 size, Vector2 TopLeft, Vector2 BottomRight)
{
SlimDX.Direct3D11.DeviceContext context = DXRenderer.Instance.D3DContext;
context.InputAssembler.InputLayout = m_inputLayout;
context.InputAssembler.PrimitiveTopology = SlimDX.Direct3D11.PrimitiveTopology.TriangleList;
context.VertexShader.Set(m_vertexShader);
context.VertexShader.SetConstantBuffer(m_constantBuffer, 0);
context.PixelShader.Set(m_pixelShader);
context.PixelShader.SetSampler(m_samplerState, 0);
context.PixelShader.SetConstantBuffer(m_constantBuffer, 0);
DataBox vertices = context.MapSubresource(m_vertexBuffer, SlimDX.Direct3D11.MapMode.WriteDiscard, SlimDX.Direct3D11.MapFlags.None);
DataStream verticeStream = vertices.Data;
// 1 - 2
// | / |
// 0 - 3
verticeStream.Write(new Vector4(0.0f, 0.0f - size.Y, 0.0f, 0.0f));
verticeStream.Write(new Vector2(TopLeft.X, BottomRight.Y));
verticeStream.Write(new Vector4(0.0f, 0.0f, 0.0f, 0.0f));
verticeStream.Write(new Vector2(TopLeft.X, TopLeft.Y));
verticeStream.Write(new Vector4(0.0f + size.X, 0.0f, 0.0f, 0.0f));
verticeStream.Write(new Vector2(BottomRight.X, TopLeft.Y));
verticeStream.Write(new Vector4(0.0f + size.X, 0.0f - size.Y, 0.0f, 0.0f));
verticeStream.Write(new Vector2(BottomRight.X, BottomRight.Y));
context.UnmapSubresource(m_vertexBuffer, 0);
DataBox constant = context.MapSubresource(m_constantBuffer, SlimDX.Direct3D11.MapMode.WriteDiscard, SlimDX.Direct3D11.MapFlags.None);
DataStream constantStream = constant.Data;

Camera camera = Camera.Instance;
Matrix world = SlimDX.Matrix.Translation(position);

constantStream.Write(SlimDX.Matrix.Transpose(world));
constantStream.Write(SlimDX.Matrix.Transpose(camera.ViewMatrix));
constantStream.Write(SlimDX.Matrix.Transpose(camera.ProjectionMatrix));

context.UnmapSubresource(m_constantBuffer, 0);
context.InputAssembler.SetIndexBuffer(m_indexBuffer, SlimDX.DXGI.Format.R16_UInt, 0);
context.InputAssembler.SetVertexBuffers(0, new SlimDX.Direct3D11.VertexBufferBinding(m_vertexBuffer, 24, 0));
context.PixelShader.SetShaderResource(new SlimDX.Direct3D11.ShaderResourceView(DXRenderer.Instance.D3DDevice, texture.Resource), 0);
context.DrawIndexed(6, 0, 0);
}[/code]

Sorry for all the code (and some of it is poorly laid out, i.e. setting the pixel/vertex shader every time I render an object, but I was trying ANYTHING to get this thing working late last night. Hopefully someone can see what I'm obviously missing.
0

Share this post


Link to post
Share on other sites
[quote name='Mike.Popoloski' timestamp='1332207555' post='4923487']
Are you sure your vertex shader is working properly? Your viewport seems to be set up fine.
[/quote]

That's what I thought as well. The vertex shader is basically straight from the DirectX samples.

[code]//--------------------------------------------------------------------------------------
// File: Tutorial04.fx
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Constant Buffer Variables
//--------------------------------------------------------------------------------------
cbuffer ConstantBuffer : register( b0 )
{
matrix World;
matrix View;
matrix Projection;
}
Texture2D txDiffuse : register( t0 );
SamplerState samLinear : register( s0 );
//--------------------------------------------------------------------------------------
struct VS_INPUT
{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
};
struct VS_OUTPUT
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};
//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
VS_OUTPUT VS(VS_INPUT input )
{
VS_OUTPUT output = (VS_OUTPUT)0;
output.Pos = mul( input.Pos, World );
output.Pos = mul( output.Pos, View );
output.Pos = mul( output.Pos, Projection );
output.Tex = input.Tex;

return output;
}

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS( VS_OUTPUT input ) : SV_Target
{
return txDiffuse.Sample( samLinear, input.Tex );
}
[/code]
0

Share this post


Link to post
Share on other sites
Are your matrix muls in the wrong order? I can never seem to remember which way it expects them to be by default, but you're transposing your matrices in there, so maybe you need to reverse them.
0

Share this post


Link to post
Share on other sites
Hmmm, still nothing. Looking at PIX the constant buffer appears to be getting set correctly. And the vertices appear to be transformed properly. Altering the Viewport dimension causes the rect to shrink/grow so obviously it's being applied... If I change the texture it's trying to load I get a different colored rect (I'm not setting blend states so it looks like a black/grey/red/whatever the texture is square - although it looks like the texture is always at the lowest MIP level...). So, obviously the pixel shader is getting executed as well.

I keep thinking it has to be something screwy with the order of how I'm setting the vertex / pixel shader and all their resources. I only create the constant buffer once at runtime (with all identity matrices) and then update it via the map/unmap methods (I've also tried just calling UpdateSubresource to directly update it without the Map commands).

I'm stumped. :/

Here's the Vertex Declaration (even though it looks fine to me):
[code] static private SlimDX.Direct3D11.InputElement[] ms_texturedVertex;
static InputLayoutManager()
{
ms_texturedVertex = new[] { new SlimDX.Direct3D11.InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 0, 0),
new SlimDX.Direct3D11.InputElement("TEXCOORD", 0, SlimDX.DXGI.Format.R32G32_Float, 16, 0) };
}
static public SlimDX.Direct3D11.InputElement[] TextureVertexLayout
{
get { return ms_texturedVertex; }
}[/code]

And the latest render call (with the changes I made in how I was populating the ConstantBuffer).
[code] public void RenderSprite(Texture texture, Vector3 position, Vector2 size, Vector2 TopLeft, Vector2 BottomRight)
{
SlimDX.Direct3D11.DeviceContext context = DXRenderer.Instance.D3DContext;
context.VertexShader.Set(m_vertexShader);
context.PixelShader.Set(m_pixelShader);
context.PixelShader.SetSampler(m_samplerState, 0);
DataBox vertices = context.MapSubresource(m_vertexBuffer, SlimDX.Direct3D11.MapMode.WriteDiscard, SlimDX.Direct3D11.MapFlags.None);
DataStream verticeStream = vertices.Data;
// 1 - 2
// | / |
// 0 - 3
verticeStream.Write(new Vector4(0.0f, 0.0f - size.Y, 0.0f, 0.0f));
verticeStream.Write(new Vector2(TopLeft.X, BottomRight.Y));
verticeStream.Write(new Vector4(0.0f, 0.0f, 0.0f, 0.0f));
verticeStream.Write(new Vector2(TopLeft.X, TopLeft.Y));
verticeStream.Write(new Vector4(0.0f + size.X, 0.0f, 0.0f, 0.0f));
verticeStream.Write(new Vector2(BottomRight.X, TopLeft.Y));
verticeStream.Write(new Vector4(0.0f + size.X, 0.0f - size.Y, 0.0f, 0.0f));
verticeStream.Write(new Vector2(BottomRight.X, BottomRight.Y));
context.UnmapSubresource(m_vertexBuffer, 0);
DataStream constantStream = new DataStream(System.Runtime.InteropServices.Marshal.SizeOf(typeof(ConstantBuffer)), true, true);

Camera camera = Camera.Instance;
Matrix world = SlimDX.Matrix.Translation(position);
ConstantBuffer cBuffer;
cBuffer.WorldMatrix = SlimDX.Matrix.Transpose(world);
cBuffer.ViewMatrix = SlimDX.Matrix.Transpose(Camera.Instance.ViewMatrix);
cBuffer.ProjectionMatrix = SlimDX.Matrix.Transpose(Camera.Instance.ProjectionMatrix);
constantStream.Write(cBuffer);
constantStream.Position = 0;
context.UpdateSubresource(new DataBox(0, 0, constantStream), m_constantBuffer, 0);

context.VertexShader.SetConstantBuffer(m_constantBuffer, 0);
context.PixelShader.SetConstantBuffer(m_constantBuffer, 0);
context.InputAssembler.InputLayout = m_inputLayout;
context.InputAssembler.PrimitiveTopology = SlimDX.Direct3D11.PrimitiveTopology.TriangleList;
context.InputAssembler.SetIndexBuffer(m_indexBuffer, SlimDX.DXGI.Format.R16_UInt, 0);
context.InputAssembler.SetVertexBuffers(0, new SlimDX.Direct3D11.VertexBufferBinding(m_vertexBuffer, 24, 0));
context.PixelShader.SetShaderResource(new SlimDX.Direct3D11.ShaderResourceView(DXRenderer.Instance.D3DDevice, texture.Resource), 0);
context.DrawIndexed(6, 0, 0);
}[/code]
0

Share this post


Link to post
Share on other sites
Also, it appears like the Viewport is being centered on the Form's size rather than the panel's size (the panel is only about 3/4th's the size of the entire editor(form) ). Is there anywhere in SlimDX where it assumes to use the application/forms size rather than the output panel you tell it about? Also, perhaps I need to update the X,Y of my viewport to account for that offset? Does the form need to inherit from SlimDX.Windows.RenderForm (it was previously, didn't seem to change things)?

Just throwing out suggestions here. :/
0

Share this post


Link to post
Share on other sites
Are you actually setting up the Camera's ViewMatrix (aka LookAt)? I can see you're setting up the ProjectionMatrix, but don't see any mention of the ViewMatrix.
0

Share this post


Link to post
Share on other sites
There are no assumptions made about the window. It should work fine with any native Win32 window handle.

Have you checked to see if there is any debug output that might shed some light? You can also try running using the reference rasterizer to see if it's a driver problem.
0

Share this post


Link to post
Share on other sites
I tried a reference model with no luck and found no info in the DXDebug log.

I've tossed the solution (it's only a few files and very simple) up here:

[url="http://www.mediafire.com/?9vfgnms1segya66"]http://www.mediafire.com/?9vfgnms1segya66[/url]

If anyone could check it out and report back their findings, I'd greatly appreciate it. Otherwise I'll continue digging!
0

Share this post


Link to post
Share on other sites
The w coords you're using for position are all 0 - they should be 1. Check your verticeStream.Write(new Vector4(...)) calls.
0

Share this post


Link to post
Share on other sites
BAH!

Solved it. Apparently my World Matrix wasn't properly getting set. Changed it to the following and it started working:

[code]
Matrix world = new Matrix();
world = SlimDX.Matrix.Identity;
SlimDX.Matrix.Translation(ref position, out world);
[/code]

No idea why that vs just calling SlimDX.Matrix.Translation(<some vector3>) didn't work.
0

Share this post


Link to post
Share on other sites
Hmm that doesn't make sense, you're creating a new empty matrix, then replacing that with an identity matrix, then replacing that with a new translation matrix built from the position vector.

For the sake of my sanity, could you verify that changing that code (and only that code) back to something akin to: Matrix world = SlimDX.Matrix.Translation(position); makes the problem come back? I use SlimDX.Matrix.Translation() all the time, and if there's the potential for some odd behavior with it I'd really like to know.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By lonewolff
      Hi Guys,
      I am just wondering if it is possible to acquire the address of the backbuffer if an API (based on DX11) only exposes the 'device' and 'context' pointers?
      Any advice would be greatly appreciated
    • By MarcusAseth
      bool InitDirect3D::Init() { if (!D3DApp::Init()) { return false; } //Additional Initialization //Disable Alt+Enter Fullscreen Toggle shortkey IDXGIFactory* factory; CreateDXGIFactory(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&factory)); factory->MakeWindowAssociation(mhWindow, DXGI_MWA_NO_WINDOW_CHANGES); factory->Release(); return true; }  
      As stated on the title and displayed on the code above, regardless of it Alt+Enter still takes effect...
      I recall something from the book during the swapChain creation, where in order to create it one has to use the same factory used to create the ID3D11Device, therefore I tested and indeed using that same factory indeed it work.
      How is that one particular factory related to my window and how come the MakeWindowAssociation won't take effect with a newly created factory?
      Also what's even the point of being able to create this Factories if they won't work,?(except from that one associated with the ID3D11Device) 
    • By ProfL
      Can anyone recommend a wrapper for Direct3D 11 that is similarly simple to use as SFML? I don't need all the image formats etc. BUT I want a simple way to open a window, allocate a texture, buffer, shader.
    • By lucky6969b
      Q1:
      Since there is no more fixed pipeline rendering in DX11, for every part of rendering in DX11, do I need to create a brand-new vertex shader and pixel shader... or at least I have to find one relevant online. If you work on skinned meshes and other effects originally worked in DX9 fixed pipeline, do I have to rework everything by now?
       
      Q2:
      For assimp, if it originally was designed for DX9, like it is coupled to a DX9 device for creating meshes and materials etc. Do I have to add in the DX11 device in the assimp, or can I just leave the assimp to remain in DX9 and after the meshes are loaded, I just convert the vertex buffers and index buffers into DX11 buffers?
      Thanks
      Jack
    • By MarcusAseth
      This header is mentioned in the book I'm reading but there is no documentation on msdn... Is it like an... outdated and abandoned header?
      If so, what's the current default/recomended library for handling errors with directX?
  • Popular Now