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

Problems passing an XMFLOAT4 to a shader

10 posts in this topic

Hi Guys,

I am trying to pass a float to a shader to control the alpha channel.

The shader works fine if I hard code a value, but I am having trouble getting the float to the shader via my application.

I have setup an XMFLOAT4 to a constant buffer and have set all of the values XMFLOAT4 to 0.5 (although I should only have to set the first one).

But for some reason it isn't working.

This is the shader (snipped a bit)

cbuffer cbChangesAlpha : register(b3)
{
	float4 image_alpha;
}

float4 PShader( PS_Input frag ) : SV_TARGET
{
    float4 fRet = colorMap_.Sample( colorSampler_, frag.tex0 );
    fRet.a = image_alpha.w;		// works if I hard code a value
    return fRet;
}
And this is my in-app code

// setting up the XMFLOAT4 constant buffer

	ZeroMemory(&constDesc,sizeof(constDesc));
	constDesc.BindFlags=D3D11_BIND_CONSTANT_BUFFER;
	constDesc.ByteWidth=sizeof(XMFLOAT4);
	constDesc.Usage=D3D11_USAGE_DEFAULT;

	if(FAILED(d3dDevice->CreateBuffer(&constDesc,0,&floatCB)))
		return 11114;
and part of my render loop

	XMFLOAT4 image_alpha;
	image_alpha.w=0.5;
	image_alpha.x=0.5;
	image_alpha.y=0.5;
	image_alpha.z=0.5;

	d3dContext->UpdateSubresource(floatCB,0,0,&image_alpha,0,0);
	
	d3dContext->VSSetConstantBuffers(3,1,&floatCB);

All complies and executes ok but I just get a black screen. If I hard code the shader to be a value between 0 & 1 it works correctly. So, it seems that the image_alpha values are not getting to the shader correctly.

Any advice would be awesome smile.png
0

Share this post


Link to post
Share on other sites

I think the reason is you are not adding D3D11_CPU_ACCESS_WRITE flag to your constant buffer. Since you are writing data from CPU you need to use that flag.

2

Share this post


Link to post
Share on other sites
The buffer does not have CPU-write access.
constDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
The buffer cannot be written because it is in the default pool.
constDesc.Usage=D3D11_USAGE_DYNAMIC;

L. Spiro
2

Share this post


Link to post
Share on other sites
Thanks guys,

I now have this but I get the same result (hard coded in shader is fine though).
 
	ZeroMemory(&constDesc,sizeof(constDesc));
	constDesc.BindFlags=D3D11_BIND_CONSTANT_BUFFER;
	constDesc.ByteWidth=sizeof(XMFLOAT4);
	constDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	constDesc.Usage=D3D11_USAGE_DYNAMIC;
I just noticed in debug mode I am getting this

Can only invoke UpdateSubresource when the destination Resource was created with D3D11_USAGE_DEFAULT and is not a multisampled Resource. The destination Resource has (1) samples. [ RESOURCE_MANIPULATION ERROR #289: UPDATESUBRESOURCE_INVALIDDESTINATIONSTATE]

[edit]
Actually, I just checked with my original code and debug mode doesn't throw any errors (just doesn't work though). Edited by DarkRonin
0

Share this post


Link to post
Share on other sites

cbuffer cbChangesAlpha : register(b3)


What's register "b3"? Which shader model are you using? All I could find was a constant boolean register, in shader model 3. I think "cb#" is used for constant buffers.

Anyway, why are you starting at 3? I have only played around with shaders a bit as a hobby, but I generally just leave out the register stuff and stick the constant buffer in slot 0.

I'd try changing this register thing, setting the CPU access flag, and leaving it as default usage.
0

Share this post


Link to post
Share on other sites
I am using b3 as registers 0-2 are being used

Texture2D colorMap_ : register( t0 );
SamplerState colorSampler_ : register( s0 );

cbuffer cbChangesEveryFrame : register(b0)
{
	matrix worldMatrix;
};

cbuffer cbNeverChanges : register(b1)
{
	matrix viewMatrix;
};

cbuffer cbChangeOnResize : register(b2)
{
	matrix projMatrix;
}

cbuffer cbChangesAlpha : register(b3)
{
	float4 image_alpha;
}
Shortly, I'll be combining them into one register (once I get things working as planned).
0

Share this post


Link to post
Share on other sites
Thanks kunos,

At the moment I am using the former method.

It is strange as I am able to send Matrix information to the shader happily enough (for positioning etc), but sending the XMFLOAT4 just seems to refuse to work sad.png

This is what I have right now.
 
ZeroMemory(&constDesc,sizeof(constDesc));
constDesc.BindFlags=D3D11_BIND_CONSTANT_BUFFER;
constDesc.ByteWidth=sizeof(XMFLOAT4);
constDesc.CPUAccessFlags = NULL;
constDesc.Usage=D3D11_USAGE_DEFAULT;

if(FAILED(d3dDevice->CreateBuffer(&constDesc,0,&floatCB)))
	return 11114;
	XMFLOAT4 image_alpha;
	image_alpha.x=0.5;
	image_alpha.y=0.5;
	image_alpha.z=0.5;
	image_alpha.w=0.5;

d3dContext->UpdateSubresource(worldCB,0,0,&worldMat,0,0);
d3dContext->UpdateSubresource(viewCB,0,0,&viewMatrix,0,0);
d3dContext->UpdateSubresource(projCB,0,0,&projMatrix,0,0);
d3dContext->UpdateSubresource(floatCB,0,0,&image_alpha,0,0);
	
d3dContext->VSSetConstantBuffers(0,1,&worldCB); // THESE WORK
d3dContext->VSSetConstantBuffers(1,1,&viewCB); // THESE WORK
d3dContext->VSSetConstantBuffers(2,1,&projCB); // THESE WORK
d3dContext->VSSetConstantBuffers(3,1,&floatCB); // THIS DOESN'T
Texture2D colorMap_ : register( t0 );
SamplerState colorSampler_ : register( s0 );

cbuffer cbChangesEveryFrame : register(b0)
{
	matrix worldMatrix;
};

cbuffer cbNeverChanges : register(b1)
{
	matrix viewMatrix;
};

cbuffer cbChangeOnResize : register(b2)
{
	matrix projMatrix;
}

cbuffer cbChangesAlpha : register(b3)
{
	float4 image_alpha;
}

struct VS_Input
{
	float4 pos  : POSITION;
	float2 tex0 : TEXCOORD0;
};

struct PS_Input
{
	float4 pos  : SV_POSITION;
	float2 tex0 : TEXCOORD0;
};

PS_Input VShader( VS_Input vertex )
{
	PS_Input vsOut = ( PS_Input )0;

	vsOut.pos=mul(vertex.pos,worldMatrix);
	vsOut.pos=mul(vsOut.pos,viewMatrix);
	vsOut.pos=mul(vsOut.pos,projMatrix);

	// vsOut.pos = vertex.pos;
	vsOut.tex0 = vertex.tex0;

	return vsOut;
}

float4 PShader( PS_Input frag ) : SV_TARGET
{
    float4 fRet = colorMap_.Sample( colorSampler_, frag.tex0 );
    fRet.a = image_alpha.x;		// works if I hard code a value
    return fRet;
}
Still very confused as to why this doesn't work.

And debug mode doesn't show any errors in this case either. Edited by DarkRonin
0

Share this post


Link to post
Share on other sites

 

cbuffer cbChangesAlpha : register(b3)


What's register "b3"? Which shader model are you using? All I could find was a constant boolean register, in shader model 3. I think "cb#" is used for constant buffers.

Anyway, why are you starting at 3? I have only played around with shaders a bit as a hobby, but I generally just leave out the register stuff and stick the constant buffer in slot 0.

I'd try changing this register thing, setting the CPU access flag, and leaving it as default usage.

 

"cb" is only used for constant buffers when shader compatibility mode is enabled for D3D10+ shaders. Otherwise "b" registers are used for constant buffers.

0

Share this post


Link to post
Share on other sites

Oh maybe you have missed my edit.
You need to bind the CB to the PS with PSSetConstantBuffers because that is where you are using image_alpha.


Nice job! That was exactly it. smile.png

I don't think I would have ever picked that one up.

Thanks again kunos!
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