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.

EDIT #1: When I read the .hlsl file the vector get's filled whit what was written in the file. For the .cso ones the function doesn't even pass the "if(file.is_open)" test and stays empty.

  • \$\begingroup\$ Does it make a difference if you do not pass ios::ate to the fstream constructor, instead open the file, then seekg to the end to figure out the size? \$\endgroup\$
    – Praetorian
    May 12, 2014 at 0:47
  • \$\begingroup\$ @Praetorian None whatsoever. But I feel it's shorter and more compact that way. \$\endgroup\$ May 12, 2014 at 0:48
  • \$\begingroup\$ Can you be more specific? What exactly happens when you read the .cso? The application crashes? No data is read? The data comes in corrupted? \$\endgroup\$
    – glampert
    May 12, 2014 at 0:48
  • \$\begingroup\$ Make sure the file exists! \$\endgroup\$
    – zdd
    May 12, 2014 at 1:20
  • \$\begingroup\$ @zdd Actually I'm not sure if it does exists or not. I believe it does since .hlsl turn into .cso files when compiling. Now if there was a setting I was supposed to change in Visual Studio then I'm sure I didn't do it. \$\endgroup\$ May 12, 2014 at 1:24

1 Answer 1


Finally found the problem.

Seems that the .hlsl turns into .cso files automatically for Windows 8 Applications but not for Windows 7 Applications (and others I guess). So my previous attempt to read the .cso file directly wouldn't work since the file never existed to begin with. Though I thought it would since I started to learn DirectX for Windows 8 first.

For windows 7 you have to compile the shader yourself and use that information.

Here's what I did:

#include <d3dcompiler.h>
#pragma comment(lib, "d3dcompiler.lib")

ID3D10Blob *blob, *blob2; //these hold the compiled shader data

    L"VertexShader.hlsl", NULL, NULL, "main", "vs_5_0", 0, 0, &blob, 0);

    L"PixelShader.hlsl", NULL, NULL, "main", "ps_5_0", 0, 0, &blob2, 0);

//Now you can just use the data with blob->GetBufferPointer(); 
//Or it's size with blob->GetBufferSize();

    blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, &mVertexShader);

md3devicecontext->VSSetShader(mVertexShader, nullptr, 0);

Hope other people find this useful and helpful.I couldn't find info on this anywhere but found the function syntax on msdn and decided to try it since I had no other lead and it worked. (Hence I came up with the fact that Windows 7 doesn't compile it for you)

  • \$\begingroup\$ There is no magic in Windows 8 compared to Windows 7, you probably forgot to compile your shaders. Using D3DCompile* stuff is not recommended nowadays and compilation to .cso is supposed to be done offline with fxc.exe. That's exactly what Visual Studio (since version 2012) do automatically when you press "build" with .hlsl files added to project. \$\endgroup\$ May 14, 2014 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .