Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 29 Aug 2009
Offline Last Active Nov 03 2012 02:09 PM

Topics I've Started

Trouble resolving dependency properly

03 October 2012 - 09:12 AM


firstly little background. I've seen a lot posts "Write games on engines" and I don't need this type of stuff here. I'm just trying to separate engine and game early on instead of having a lot of hardcoded things later on. This is where I encountered a problem. I'm trying to wrap DirectX into a bit more friendly classes, but they need DirectX Device and I can't think of a decent way of passing device to these objects. Roughly my code looks like this:

class VertexShader {
	ID3D11VertexShader *shdr;
	void Create(void *bytecode, uint32 size); // requires ID3D11Device or DXWrapper;

class DXWrapper {
	ID3D11Device *dvc;
	ID3D11DeviceContext *dvcc;

class Engine {
	DXWrapper DXW;

// this is how I use VertexShader class:
class SkinnedRenderer {
	VertexShader vrtShader;
	SkinnedRenderer() {
		auto bytecode = ResourceManager.Get("skinned_vs.cso");
		vrtShader.Create(bytecode.data(), bytecode.size());

I managed to think of 2 possible solutions (workarounds?) but I'm not satisfied with either of them.
  • Pass D3D Device as argument in VertexShader::Create() call, but I feel it's a bit messy doing it all the time.
  • Make Engine or DXWrapper class's instance static/global, allowing global access, but I'm trying to avoid such dependency, as code re-useability decreases.

Anyone have any ideas how can I resolve this issue, or solution #1 is the right way?

Thank you in advance.

Multithreading in games

04 September 2012 - 02:52 AM

I've implemented Fixed-Time-Step based on L.Spiro's article (http://lspiroengine.com/?p=378); such approach forces to separate update and rendering completely. I thought this would be a good place to split them into 2 threads:
  • update thread; input, physics, AI, etc, etc; doesn't touch rendering at all;
  • rendering thread; not allowed to write shared data at all, only reads it; takes care only of rendering.

However there was other multithreading thread and some people said it's bad approach because of multiple dependencies, therefore tasks are better, ex. resource loading. Does this still apply even though threads are quite separate, and dependencies aren't very high (GUI seems to have heaviest dependency)?

Thank you in advance.

[C++] Blend state issue

29 August 2012 - 02:26 AM

I'm trying to setup blending state to perform some blending, but for some reason I can't understand how it works, it's always giving me troubles.
My pixel shader returns (0, 0, 0, x), where x is some amount of alpha, basically I just want to make some areas darker. Here's my current blend state setup:
  D3D11_BLEND_DESC blendDesc;
  blendDesc.AlphaToCoverageEnable = FALSE;
  blendDesc.IndependentBlendEnable = FALSE;
  blendDesc.RenderTarget[0].BlendEnable = TRUE;

  blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_COLOR;
  blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_DEST_COLOR;
  blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
  blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
  blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
  blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;

  blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
Why I setup it like this? Well I think SRC is new color, DEST is current render target color, so SrcBlend and DestBlend are set to use those colors, SrcBlendAlpha is set to use whatever alpha pixel shader returns, and DestBlendAlpha uses inverse of new alpha. Therefore I believe it'll end up like this:
rt_color = rt_color * (1 - new_color.a) + new_color * new_color.a;
But it doesn't seem to do that.

Could anyone explain me how blending states work and/or help setup it to get it to work like I need?

Thank you in advance.

Weird issue while rendering screen quad

13 August 2012 - 11:07 AM

I'm having some troubles drawing render target's contents on the screen. Problem is probably trivial but I have no luck finding it.
Right now I create small quad (0.2 x 0.2), however for some reason my whole screen becomes white, RTs contents aren't fully white either.
Anyone knows what could possibly cause such issue, or how to fix it?

My render loop can be generalized like this:

if(debug_draw) {
else {

Here's how I draw the quad (this is in the actual loop, nothing cut from other places):
if(debug_draw) {
VertexShader vertexShader;
/* vs.cso is compiled shader:
struct Input {
    float4 position : POSITION;
    float2 texCoord0 : TEXCOORD0;
struct Output {
    float4 position : SV_POSITION;
    float2 texCoord0 : TEXCOORD0;
Output main(Input input) {
    Output output;
    output.position = input.position;
    output.texCoord0 = input.texCoord0;
    return output;
PixelShader pixelShader;
/* ps.cso is:
SamplerState sampler0 : register(s0);
Texture2D texture0 : register(t0);
struct Input {
    float4 position : SV_POSITION;
    float2 texCoord0 : TEXCOORD0;
float4 main(Input input) : SV_TARGET0 {
    return texture0.Sample(sampler0, input.texCoord0);
D3D11_INPUT_ELEMENT_DESC inputLayoutDesc[] = {
InputLayout inputLayout;
inputLayout.Create(inputLayoutDesc, vertexShader);
/* notice that quad is not fullscreen,
however my whole screen becomes white
float quad[] = {
    -0.1, 0.1, 0, 0, 0, 0, 0, 0,
    0.1, 0.1, 0, 0, 1, 0, 0, 0,
    -0.1, -0.1, 0, 0, 0, 1, 0, 0,
    0.1, -0.1, 0, 0, 1, 1, 0, 0
VertexBuffer vertexBuffer;
// at first I thought some states cause issue, so I added this
auto windowViewport = Window::instance.GetClientArea();
D3D11_VIEWPORT viewport = {0, 0, get<0>(windowViewport), get<1>(windowViewport), 0, 1};
deviceContext->RSSetViewports(1, &viewport);
deviceContext->OMSetRenderTargets(1, &renderTarget, 0);
deviceContext->VSSetShader(vertexShader.shader, 0, 0);
deviceContext->PSSetShader(pixelShader.shader, 0, 0);
UINT strides = sizeof(float) * 8;
UINT offsets = 0;
deviceContext->IASetVertexBuffers(0, 1, vertexBuffer.GetPtr(), &strides, &offsets);
/* Visual Studio 2012 Graphical Debugger shows there is something in this SRV
However it fails to debug pixel shader
deviceContext->PSSetShaderResources(0, 1, &RTColor.SRV);
/* draw call gives warning about missing Sampler State,
however it also mentions default state will be used, that's fine
deviceContext->Draw(4, 0);
else {
    // draw normally
swapChain->Present(0, 0);

Let me know if any information needed. Thank you in advance.

[WinApi] Open file dialog

10 August 2012 - 01:03 PM

I am trying to create open file dialog. In the past I used to use GetOpenFileName function, however MSDN suggests not to use it and it's not available in Visual Studio 2012 at all, so I had to use IFileOpenDialog interface. Sadly I ran into some problems, it just fails randomly with HRESULT of RPC_E_WRONG_THREAD, even though whole dialog is handled on the single thread. Anyone knows solution to this? Here's my code:
COMDLG_FILTERSPEC c_rgSaveTypes[] = {
	{L"Any File", L"*.*"},
IFileOpenDialog *pfd;
CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd));
DWORD dwFlags;
pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);
pfd->SetFileTypes(ARRAYSIZE(c_rgSaveTypes), c_rgSaveTypes);
IShellItem *result;
PWSTR pszFilePath;
HRESULT hr = result->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // this call fails
	return; // breakpoint gets triggered here
pathToFile = string_cast<string>(wstring(pszFilePath));

Thank you in advance.