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…)
[code=cpp]
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;
strippedSizeInWords -= 1 + commentSize;
i += commentSize;
}
}
// 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;
i += commentSize;
}
else
{
strippedCodeData[offset++] = codeData[i];
}
}
return strippedCode;
}
[/code]
Happy coding,
http://entland.homelinux.com/blog/
Stripping comments from Shader bytecodes
Started by ent, Jan 16 2009 03:00 PM
2 replies to this topic
#1
Posted 16 January 2009 - 03:00 PM
#2
Posted 31 January 2009 - 07:36 PM
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.
If you compile your code without debug informations then the comments will be ommited anyway.
#3
Posted 02 February 2009 - 01:24 PM
Axel said:
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.
If you compile your code without debug informations then the comments will be ommited anyway.
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.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











