Jump to content
  • Advertisement
Sign in to follow this  
vpalmer

DX11 can't load precompiled shaders

This topic is 1970 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

SOLVED! 

//my shader files were wrong compiled (without /E parametr for Entry point), and need to remember to allocate memory for variable to read file content into it with ifstream

I'm trying to learn DX11 programming - by books and tutorials on net. Few days ago I stucked at loading precompiled shaders files and creating shaders with them.With tutorial I wrote program to draw simple triangle and it works fine while i load and compile shaders from 2 .hlsl files with

D3DX11CompileFromFile function. But after i compiled .hlsl files with fxc.exe utility and trying to read them with ifstream, my program crash during runtime (still compiled without warnings).

 

shaderbuf struct for file reading and global vars:

struct shaderbuf
{
	char* pbuf;
	int size;
};

extern shaderbuf vsh;
extern shaderbuf psh;

 

compiled shader files reading function and shader creation:

shaderbuf ReadShader(char* filename)
{
	shaderbuf sb;
	int size;
	std::ifstream ifs;
	
	ifs.open(filename, ifs.in | ifs.binary);
	ifs.seekg(0, ifs.end);
	size = int(ifs.tellg());

	sb.size = size;

	ifs.seekg(0, ifs.beg);
	ifs.read(sb.pbuf, size);
	ifs.close();
	return sb;
}

void InitPipeline()
{
	shaderbuf vsh =	ReadShader("vshader.cso");
	shaderbuf psh = ReadShader("pshader.cso");

	dev->CreateVertexShader(&vsh.pbuf, vsh.size,
							NULL, &pVS);
	dev->CreatePixelShader(&psh.pbuf, psh.size,
							NULL, &pPS);

	
	//set shader objects
	devcon->VSSetShader(pVS, 0, 0);
	devcon->PSSetShader(pPS, 0, 0);

	BufferCreatingAndMapping();
}

 

InputLayout creation (i guess there can be some errors with pointers to Bytecode too):

dev->CreateInputLayout(ied, sizeof(ied)/sizeof(ied[0]),
		       vsh.pbuf, vsh.size,
		       &pLayout);

 

 

Help me pls to find my errors. Need to resolve shader files loading with ifstream.

Edited by vpalmer

Share this post


Link to post
Share on other sites
Advertisement

You need to allocate some space for data that pbuf will point at.

shaderbuf ReadShader(char* filename)
{
    shaderbuf sb;
    int size;
    std::ifstream ifs;
    
    ifs.open(filename, ifs.in | ifs.binary);
    ifs.seekg(0, ifs.end);
    size = int(ifs.tellg());

    sb.size = size;
 
    // =============================
   sb.pbuf = new char[size];
   // ============================= 

    ifs.seekg(0, ifs.beg);
    ifs.read(sb.pbuf, size);
    ifs.close();
    return sb;
}
Edited by belfegor

Share this post


Link to post
Share on other sites

You need to allocate some space for data that pbuf will point at.

shaderbuf ReadShader(char* filename)
{
    shaderbuf sb;
    int size;
    std::ifstream ifs;
    
    ifs.open(filename, ifs.in | ifs.binary);
    ifs.seekg(0, ifs.end);
    size = int(ifs.tellg());

    sb.size = size;
 
    // =============================
   sb.pbuf = new char[size];
   // ============================= 

    ifs.seekg(0, ifs.beg);
    ifs.read(sb.pbuf, size);
    ifs.close();
    return sb;
}

 

i tried this already (and tried now again), but unfortunatly this does not help - program still crashes on runtime.

Edited by vpalmer

Share this post


Link to post
Share on other sites

Put some breakpoints and run your project with debugger to find out where it crashes.

Also enable d3d debug runtimes to see additional warning/error messages that you might have.

Check all d3d functions that return HRESULT for errors.

Edited by belfegor

Share this post


Link to post
Share on other sites

via debugger i found some errors (compiled shader files were in wrong directory). but now i have next problem:

 

dev->CreateVertexShader(vsh.pbuf, vsh.size,NULL, &pVS);

 

returns E_INVALIDARG - what wrong with parameters?

Edited by vpalmer

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!