• 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
Migi0027

DX11
D3D11 Constant Buffer not being sent correctly!

2 posts in this topic

Hi guys,

 

this error has currently been a pain in the ass for me, but I haven't given up yet! So basically I have a constant buffer called UberData which contains loads of floats (code below). In my shader I have around 5 buffers and all have the correct size. So there is a float called _texture, when i debug the UberBuffer, _texture is ALWAYS 1, but in the shader it is 821371982 (basically unset). This float is used as a bool for now. So i tried to change the if statement (code in hlsl) from if (_texture == 1) to if (1) , so it will execute it, and the code inside the statement works perfectly, but the buffer somehow isn't being set correctly!

 

One thing that is irritating me is this message:

 

D3D11: WARNING: ID3D11DeviceContext::DrawIndexed: The size of the Constant Buffer at slot 2 of the Pixel Shader unit is too small (32 bytes provided, 208 bytes, at least, expected). This is OK, as out-of-bounds reads are defined to return 0. It is also possible the developer knows the missing data will not be used anyway. This is only a problem if the developer actually intended to bind a sufficiently large Constant Buffer for what the shader expects.  [ EXECUTION WARNING #351: DEVICE_DRAW_CONSTANT_BUFFER_TOO_SMALL ]

 

I perfectly understand this, but I don't see where (in my code) I'm doing it wrong, though it will probably be a simple mistake biggrin.png !

 

Code:

 

Creation of Uber Shader

BOOL UberShaderObject::Create( ID3D11Device *&dev )
{
	string finals = string(
		"C:\\Users\\Utilizador\\Documents\\Visual Studio 2012\\Projects\\Cube3D\\Product\\Bin32\\") + string("UberShader.hlsl").c_str();

	ID3D10Blob *vserrors;
	ID3D10Blob *pserrors;
	HRESULT HR;

	D3DX11CompileFromFile(finals.c_str(), 0, 0, "VShader", "vs_5_0", D3DCOMPILE_DEBUG, 0, 0, &VS, &vserrors, &HR);
	D3DX11CompileFromFile(finals.c_str(), 0, 0, "PShader", "ps_5_0", D3DCOMPILE_DEBUG, 0, 0, &PS, &pserrors, &HR);
	
	// create the shader objects
	if (!VS)
	{
		DXTRACE_ERR(TEXT(DXGetErrorDescription(HR)),HR);

		MessageBoxA(NULL, "The uber vertex shader creation has failed, program will now exit!", "ERROR", 0);
		exit(0);
	}
	if (!PS)
	{
		DXTRACE_ERR(TEXT(DXGetErrorDescription(HR)),HR);

		MessageBoxA(NULL, "The uber pixel shader creation has failed, program will now exit!", "ERROR", 0);
		exit(0);
	}
	
	dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
	dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);

	D3D11_INPUT_ELEMENT_DESC ied[] =
	{
		{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
		{"TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0},
	};
	
	if (dev->CreateInputLayout(ied, 4, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout) != S_OK)
		CE_WARNING("Input Layout Creation", "Input Layout creation in Uber Shader has failed!");

	pCBuffer = new ID3D11Buffer*[4]; // Object || Frame || Uber || Matrix || Fog

	// create the constant buffer
	D3D11_BUFFER_DESC bd;
	ZeroMemory(&bd, sizeof(bd));
	bd.Usage = D3D11_USAGE_DEFAULT;

	//---------------------------------------//
	
	bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;

	bd.ByteWidth = 144; // Object
	if(dev->CreateBuffer(&bd, NULL, &pCBuffer[0]) != S_OK)
		CE_WARNING("Buffer Creation", "Object Buffer creation in Uber Shader has failed!");

    bd.ByteWidth = 32; // Frame
	if(dev->CreateBuffer(&bd, NULL, &pCBuffer[1]) != S_OK)
		CE_WARNING("Buffer Creation", "Frame Buffer creation in Uber Shader has failed!");

	bd.ByteWidth = 32; // Uber
	if(dev->CreateBuffer(&bd, NULL, &pCBuffer[2]) != S_OK)
		CE_WARNING("Buffer Creation", "Uber Buffer creation in Uber Shader has failed!");
	
	bd.ByteWidth = 224; // Matrix
	if(dev->CreateBuffer(&bd, NULL, &pCBuffer[3]) != S_OK)
		CE_WARNING("Buffer Creation", "Matrix Buffer creation in Uber Shader has failed!");
	
	bd.ByteWidth = 32; // Fog
	if(dev->CreateBuffer(&bd, NULL, &pCBuffer[4]) != S_OK)
		CE_WARNING("Buffer Creation", "Fog Buffer creation in Uber Shader has failed!");
}

 

 

Sending Buffers:

void UberShaderObject::SendObjectBuffer(CBUFFER_PEROBJECT &bff, ID3D11DeviceContext *&devcon)
{
	devcon->UpdateSubresource(pCBuffer[0], 0, 0, &bff, NULL, NULL);
}

void UberShaderObject::SendFrameBuffer(CBUFFER_PERFRAME &bff, ID3D11DeviceContext *&devcon)
{
	devcon->UpdateSubresource(pCBuffer[1], 0, 0, &bff, NULL, NULL);
}

void UberShaderObject::SendUberBuffer(CBUFFER_UBERDATA &bff, ID3D11DeviceContext *&devcon)
{
	devcon->UpdateSubresource(pCBuffer[2], 0, 0, &bff, NULL, NULL);
}

void UberShaderObject::SendMatrixBuffer(CBUFFER_MATRIX &bff, ID3D11DeviceContext *&devcon)
{
	devcon->UpdateSubresource(pCBuffer[3], 0, 0, &bff, NULL, NULL);
}

void UberShaderObject::SendFogBuffer(CBUFFER_FOG &bff, ID3D11DeviceContext *&devcon)
{
	devcon->UpdateSubresource(pCBuffer[4], 0, 0, &bff, NULL, NULL);
}

 

 

Buffers in C++:

struct CBUFFER_PERFRAME
{
	D3DXVECTOR4 LightVector;
	D3DXCOLOR LightColor;
};

struct CBUFFER_PEROBJECT
{
	D3DXMATRIX Final;
	D3DXMATRIX Rotation;
	D3DXCOLOR AmbientColor;
};

struct CBUFFER_UBERDATA
{
	float _diffuse;
	float _texture;
	float _directionall;
	float _bumpmap;

	float _fog;
	D3DXVECTOR3 space5;
};

struct CBUFFER_MATRIX
{
	D3DXMATRIX world;
	D3DXMATRIX view;
	D3DXMATRIX projection;
	D3DXVECTOR4 EyePosition;
	D3DXVECTOR3 Forward;
	float space;
};

struct CBUFFER_FOG
{
	float FogStart;
	float FogEnd;
	D3DXVECTOR2 __space;
	D3DXVECTOR3 FogColor;
	float __space2;
};

 

 

Shader (Some code like bump mapping probably isn't working yet biggrin.png , the important part is the buffers!)

cbuffer ConstantObjectBuffer
{
	float4x4 final;
	float4x4 rotation;
	float4 ambientcol;
};

cbuffer ConstantFrameBuffer
{
	float4 lightvec;
	float4 lightcol;
};

cbuffer UberData
{
	float _diffuse;
	float _texture;
	float _directionall;
	float _bumpmap;

	float _fog;
	float3 space5;
};

cbuffer ConstantMatrixBuffer
{
	float4x4 world;
	float4x4 view;
	float4x4 projection;
	float4 eyepos;
	float3 cforward;
	float _space;
};

cbuffer ConstantFogBuffer
{
	float FogStart;
	float FogEnd;
	float2 __space;
	float3 FogColor;
	float __space2;
};

Texture2D txt;
Texture2D txt2;

SamplerState ss;

struct VOut
{
    float4 position : SV_POSITION;
    float4 worldPos : POSITION;
    float2 texcoord : TEXCOORD;
	float4 normal : NORMAL;
	float4 tangent : TANGENT;
	float Depth : FOG;
    float4 color : COLOR;
};

VOut VShader(float4 position : POSITION, float4 normal : NORMAL, float2 texcoord : TEXCOORD, float4 tangent : TANGENT)
{
    VOut output;
    output.position = mul(final, position);
    output.worldPos = mul(position, final);

	output.color = float4(0,0,0,1);

    // set the ambient light
	if (_diffuse == 1)
	{
		output.color += ambientcol;
	}

	if (_texture == 1)
	{
		output.texcoord = texcoord;
	}

	// Stuff for dir and bump
	if (_directionall == 1)
	{
		float4 norm = normalize(mul(rotation, normal));
		float diffusebrightness = saturate(dot(norm, lightvec));
		output.color += lightcol * diffusebrightness;
	}

	if (_bumpmap)
	{
		output.normal = normal;
		output.tangent = tangent;
	}

	// Final stuff
	output.Depth = output.position.z;
	output.color.a = 1;

    return output;
}

float4 PShader(VOut input) : SV_TARGET
{
	float4 color = input.color;

	float4 spec = float4(0,0,0, 1);
	float4 tex = float4(0,0,0, 1);
	float4 bump = float4(0,0,0, 1);

	if (_texture == 1)
	{
		color *= txt.Sample(ss, input.texcoord);
	}

	if (_directionall == 1)
	{
		float3 h = normalize(normalize(eyepos - input.position) - lightvec);
		float specLighting = pow(saturate(dot(h, input.normal)), 200);

		color += saturate(float4(1, 1, 1, 1) * float4(1, 1, 1, 1) * specLighting * 0.5);
	}

	if (_bumpmap== 1)
	{
		float4 light = normalize(input.position-lightvec);
		float4 eye = normalize(eyepos-light);
		float4 vhalf = normalize(eye-lightvec);

		float4 Normal = txt2.Sample(ss, input.texcoord);
		color *= dot(2.0*(Normal-0.5), 2.0*(color-0.5));

		input.tangent = mul(float4(input.tangent.xyz,0.0),final);
		input.normal = mul(float4(input.normal.xyz,0.0),final);
		float3 binormal = cross(input.normal.xyz,input.tangent.xyz);

		float3 L = float3(dot(input.tangent, light), dot(binormal, light.xyz), dot(input.normal, light));
		float3 H = float3(dot(input.tangent, vhalf), dot(binormal, vhalf.xyz), dot(input.normal, vhalf));

		float diffuse = dot(input.normal, L);
		float specular = dot(input.normal, H);
		specular = pow(specular, 2);

		bump = 2.0*(diffuse + specular);
	}

	color = saturate(color + spec);;
	
	if (_fog == 1)
	{
		float l = saturate((input.Depth - FogStart) / (FogEnd - FogStart));
		color = float4(lerp(color,FogColor, l), 1);
	}

	color.a = 1;

    return color;
}

 

 

Sorry for such a long post, but i kinda see you people as a last hope for this wink.png !

 

Thank You!

0

Share this post


Link to post
Share on other sites

You don't specify registers for your buffers so they might be different order than you expect.

 

Error message says it expects 208 bytes, which is 64 + 64 + 16 = float4x4 + float4x4 + float4 = ConstantObjectBuffer. This buffer has index 2 for some reason, you probably thought it's going to be 0.

1

Share this post


Link to post
Share on other sites

didn't go through all the code... so this may not be applicable

  • this is HLSL code as far as i can tell... use fxc.exe  to test your shader code explicitly without having to feed it any info.at the top of the output it gives you info of what sizes the shader expects for all your constant buffers. fxc.exe is part of the directx sdk for windows 7 and below, and (afaik) part of the windows sdk as of windows 8.
  • make sure the sizes of your constant buffers in C++ match the sizes of the shaders'. easily done by making your struct mirror the cbuffer, and using sizeof() when mapping the memory.
  • make sure all your buffers are multiples of 16 (in bytes that is). i always found this restriction to be stupid, but probably has some performance reason.
  • make sure all buffers are actually created and mapped, send in dummy values if you have to.
  • make sure slot numbers are passed in correctly when calling the XXSetConstantBuffer() method, where XX is the appropriate shader stage.
  • make sure you set the buffers for the correct shader stage. (see your fxc output to find out what is expected where)

well this is a very general troubleshooting list, but running through these i usually solve all my constant buffer issues.

good luck,

   Tasche =)

2

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