• 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

Unable to get depth working in 2D engine

2 posts in this topic

TL;DR: skip the first 3 paragraphs.


Hi! I've been toying around with building a 2D engine from scratch using Direct3D 11 for about a week, but I feel I'm in pretty deep water now and I could use some help. I'm very new to D3D, and I never used D3D9 for example, so most of what I've been able to accomplish is from looking at RasterTek's tutorials, and reading the MSDN documentation. Since 2D has been the plan all the way, I've ignored (and completely forgotten about) the Z dimension, but I feel it could come in handy again now for the rendered order of sprites, as my current approach seems very convoluted and inefficient.


First off, I have a stack of Scene objects to control the game state, inspired by the ScreenManager sample from XNA a while back. The scenes are updated from the top down, and when a Scene is closed (such as a menu for example) it is popped off the stack so the focus drops down to the next Scene, which could be the actual game in this example. Pointers to these Scene objects are then sent to my render subsystem and stacked in reverse order, as these will need to be rendered from the bottom up. This part works, and the scenes are rendered in the correct order.


However, I would also like to be able to give different layers to the renderables in a Scene, such as setting text on top of sprites for menus. This is where it currently gets iffy. Currently, I have a std::map with the Scene pointer as its key, and then a std::set of Renderable objects as the value. This set is sorted by a functor that compares a Z index value set for each individual Renderable object. Lower indices are rendered below the higher ones. The good thing about this system is that it works; it does what it's supposed to. The bad thing is that it'll probably become incredibly slow once I get to a certain number of Renderables in the set, because of the logarithmic insert/removes. Since the set doesn't sort itself, it's very impractical to change a Renderable's Z value as well, because I then have to remove it and add it to the set again.


What struck me after quite a while of fighting with this problem, though, is... why am I reimplementing something that is already done natively by the graphics card in 3D applications? Instead of a set, why not just use a std::vector with the Renderables, set their Z position as well to represent their respective layering order, and let the graphics card handle the depth ordering of my vertices. So that's what I'm trying to do now, but I can't get it to work. At first I just tried to set the Z component of the position, but it just ignored that and rendered the Renderables in the order they were added to the vector instead. I hadn't set up a depth buffer, so I did that as well, but then I just get a blank screen. I'm too new with D3D to understand what's going on, and I'm not sure what information you guys might need to try and help me, so I'll just post what I think could be relevant.


The 2D orthographic view-projection matrix that's passed to my vertex shader:

XMMATRIX viewMatrix = XMMatrixIdentity();
XMMATRIX projectionMatrix = XMMatrixOrthographicOffCenterLH( 0.0f, resX, resY, 0.0f, minZ, maxZ );
XMMATRIX viewProjectionMatrix = XMMatrixMultiplyTranspose( viewMatrix, projectionMatrix );

XMStoreFloat4x4( &mViewProjectionMatrix, viewProjectionMatrix );


MinZ and MaxZ are currently set to -100.0f and 100.0f respectively, and Renderables are by default placed at Z 0.0f. ResX and ResY are the screen resolution, 1024x576 with the current defaults.


My viewport:

vp.Width	= resX;
vp.Height	= resY;
vp.MinDepth	= 0.0f;
vp.MaxDepth	= 1.0f;
vp.TopLeftX	= 0.0f;
vp.TopLeftY	= 0.0f;


My active rasterizer state:

rasterizerDescs[ 0 ].AntialiasedLineEnable	= true;
rasterizerDescs[ 0 ].CullMode			= D3D11_CULL_NONE;
rasterizerDescs[ 0 ].DepthBias			= 0;
rasterizerDescs[ 0 ].DepthBiasClamp		= 0.0f;
rasterizerDescs[ 0 ].DepthClipEnable		= true;
rasterizerDescs[ 0 ].FillMode			= D3D11_FILL_SOLID;
rasterizerDescs[ 0 ].FrontCounterClockwise	= false;
rasterizerDescs[ 0 ].MultisampleEnable		= true;
rasterizerDescs[ 0 ].ScissorEnable		= false;
rasterizerDescs[ 0 ].SlopeScaledDepthBias	= 0.0f;


My attempted, but non-working, depth stencil state/view:

D3D11_TEXTURE2D_DESC depthTextureDesc;
depthTextureDesc.BindFlags				= D3D11_BIND_DEPTH_STENCIL;
depthTextureDesc.Height					= backBufferDesc.Height;
depthTextureDesc.Width					= backBufferDesc.Width;
depthTextureDesc.MipLevels				= 1;
depthTextureDesc.ArraySize				= 1;
depthTextureDesc.Format					= DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
depthTextureDesc.SampleDesc.Count			= 1;
depthTextureDesc.SampleDesc.Quality			= 0;
depthTextureDesc.Usage					= D3D11_USAGE_DEFAULT;
depthTextureDesc.CPUAccessFlags				= 0;
depthTextureDesc.MiscFlags				= 0;

stencilDesc.BackFace.StencilFailOp		= D3D11_STENCIL_OP_KEEP;
stencilDesc.BackFace.StencilDepthFailOp		= D3D11_STENCIL_OP_DECR;
stencilDesc.BackFace.StencilPassOp		= D3D11_STENCIL_OP_KEEP;
stencilDesc.BackFace.StencilFunc		= D3D11_COMPARISON_ALWAYS;
stencilDesc.DepthEnable				= true;
stencilDesc.DepthWriteMask			= D3D11_DEPTH_WRITE_MASK_ALL;
stencilDesc.DepthFunc				= D3D11_COMPARISON_LESS;
stencilDesc.FrontFace.StencilFailOp		= D3D11_STENCIL_OP_KEEP;
stencilDesc.FrontFace.StencilDepthFailOp	= D3D11_STENCIL_OP_INCR;
stencilDesc.FrontFace.StencilPassOp		= D3D11_STENCIL_OP_KEEP;
stencilDesc.FrontFace.StencilFunc		= D3D11_COMPARISON_ALWAYS;
stencilDesc.StencilEnable			= true;
stencilDesc.StencilReadMask			= 0xFF;
stencilDesc.StencilWriteMask			= 0xFF;

svd.Flags		= 0;
svd.Format		= DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
svd.Texture2D.MipSlice	= 0;
svd.ViewDimension	= D3D11_DSV_DIMENSION_TEXTURE2D;


They're all created in the correct order by the device, and throw no errors. I've also set the depth stencil view together with the render target view in the OMSetRenderTargets function, and I do the ClearDepthStencilView together with the ClearRenderTargetView, but to no avail. Just a blank screen.


If you'd like to peek at more of the code, it's all on https://github.com/Wedvich/Untopia (since it's an ongoing project, there are some obsolete classes and code in there). The game loop is in the Game class, while the render calls are in the RenderSystem class.


So, does anyone have any ideas as to what I'm doing wrong? Why can't I get the depth to work properly for this?


Sorry for the rambling wall of text, but I'm new to this and I'm not sure how to formulate it into a concise question. Please ask if I need to clear up anything.


I've also attached a Graphics Debugger log (basically PIX for VS2012) with a frame from the failed depth test, where nothing renders. I'm not sure what to make out of it myself, but here it is: [attachment=13256:graphicslog.zip]


Share this post

Link to post
Share on other sites

I'm working with DX9 and my answer may not help you so much but check the return value of ClearDepthStencilView.

In dx9 this function can fail if you don't have stencil information in depth buffer.


Share this post

Link to post
Share on other sites

I don't know about specifics on DX...

But I just like to say that sorting before you draw is not as wacky as you might think, and is sometimes unavoidable.


And it's probably not that much of a performance problem if you keep the list always sorted so the cost is only at insert/remove.


Even when using the Z-buffer, it might be a good idea to keep them sorted and draw from front to back (except transparent ofc).  If you don't, you might not use all the gains you could from it. (depending a bit on hardware...)

If you want semitransparent layers, you have to draw whats behind first, z-buffer will not help you there.


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