0
101 Jan 16, 2009 at 15:00

In DirectX, when compiling a shader with D3DXCompileShader() a buffer containing the shader bytecodes is received. Apart from the bytecodes, extra content like debug and symbol table information is embedded. That extra information is added in form of comments that probably can be eliminated because you are already processing it at compile-time and it is not needed at run-time when loading the shader.

If you can do without that information the following code will help you to save a few bytes, even halving the size of the byte-code in the best cases.

Although not documented in the DirectX SDK, this CodeGem is not an undocumented hack. The Direct3D shader code format is documented in the MSDN, so it probaly won’t change in future revision of DirectX v9.0 (if there is going to be any more…)

D3DPtr<ID3DXBuffer> StripComments(const D3DPtr<ID3DXBuffer>& code)
{
// Calculates the new size (without comments)
int* codeData = static_cast<int*>(code->GetBufferPointer());
unsigned int sizeInWords = code->GetBufferSize() / 4;
unsigned int strippedSizeInWords = sizeInWords;

for (unsigned int i = 0; i < sizeInWords; i++)
{
if ((codeData[i] & 0xffff) == D3DSIO_COMMENT)
{
int commentSize = codeData[i] >> 16;
}
}

// Creates a new buffer with the original code but omitting the comments
D3DPtr<ID3DXBuffer> strippedCode;
V(D3DXCreateBuffer(strippedSizeInWords * 4, strippedCode.GetPtrForInit()));

int* strippedCodeData = static_cast<int*>(strippedCode->GetBufferPointer());
size_t offset = 0;

for (unsigned int i = 0; i < sizeInWords; i++)
{
if ((codeData[i] & 0xffff) == D3DSIO_COMMENT)
{
int commentSize = codeData[i] >> 16;
}
else
{
strippedCodeData[offset++] = codeData[i];
}
}

return strippedCode;
}


Happy coding,

#### 2 Replies

0
101 Jan 31, 2009 at 19:36

Sorry, but I don’t understand why you would need to do something like that.

If you compile your code without debug informations then the comments will be ommited anyway.

0
102 Feb 02, 2009 at 13:24

@Axel

Sorry, but I don’t understand why you would need to do something like that. If you compile your code without debug informations then the comments will be ommited anyway.

I think it’s nice to be able to embed all sorts of information in your shader code. ent’s code puts you in control of when it gets removed.

For instance I know a tool which lets you visually ‘construct’ shaders with drag-and-drop diagrams. It’s useful to have additional comments in the code about the dependencies (which can be shown to the artist), which should not be removed by the shader compiler.