Well I've been trying to read a .cso file to use as a shader for a DirectX program I'm currently making.

Problem is no matter how I implemented a way to read the file it never worked. And after fidgeting around I discover that it's only the .cso files I can't read.

I can read anything else (which means it works) even their .hlsl files. Which is strange because the .hlsl (high level shader language) files are supposed to turn into .cso (compiled shader object) files.

What I'm currently doing is:

vector<byte> Read(string File){
    vector<byte> Text;
    fstream file(File, ios::in | ios::ate | ios::binary);

        file.seekg(0 , ios::beg);
        file.read(reinterpret_cast<char*>(&Text[0]), Text.size());

    return Text;

If I then implement it.

Read("VertexShader.hlsl"); //Works
Read("VertexShader.cso"); //Doesn't Works?!?!

And I need the .cso version of the shader to draw my sexy triangles. Without it my life and application will never continue and I have no idea what could be wrong.

(I've also asked this at stack overflow but still no answers.)

  • 2
    \$\begingroup\$ What do you mean, "doesn't work"? Is the problem with reading the file off disk somehow, or with creating a shader based on the contents of the file? You have to use D3DCompile to compile the HLSL before it can be used, while the .cso is already compiled, so you can pass it directly to CreateVertexShader and friends. \$\endgroup\$ May 12 '14 at 1:24
  • 1
    \$\begingroup\$ Please don't cross post, only post on the site most appropriate for your question. Please remove one. \$\endgroup\$
    – House
    May 12 '14 at 2:35
  • 1
    \$\begingroup\$ Please stop using "doesn't work"!, please describe what happened and what exactly you want! \$\endgroup\$
    – zdd
    May 12 '14 at 3:19

Depending on how you've set up your project, you likely have your .cso files going to the project output $(OutDir), and your debug working directory set to the project directory $(ProjectDir). As a result, file paths are relative to the project directory. You should change your working directory to the output directory for debugging, and you should be able to load the .cso files.

Working Directory

Note that this will cause loading your .hlsl files to stop working though, unless you copy them to the output directory as well (but you shouldn't need them there if you're compiling at build-time).

  • \$\begingroup\$ This was it. Guess I'll have to read on relative paths too. \$\endgroup\$ Jun 9 '14 at 23:12

Can be multiple things, don't have access to VS right now to give you exact answer but it can be:

  • VS shader compiler is not attached to your project. Right click on project-> build customization and check the list
  • shader file is not acctually set as shader file. Right click on shader file in your project and make sure it has selected shader type and properties
  • by default VS will compile your hlsl files into cso files before it compiles your main project and put them in //debug or //release, make sure you search in the right directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.