DevMaster.net Forums
[[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]]

Go Back   DevMaster.net Forums > Site Discussions > Code & Snapshot Discussion
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
Old 01-16-2009, 08:00 AM   #1
ent
New Member
 
ent's Avatar
 
Join Date: May 2006
Location: Spain
Posts: 2
Default

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:
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; }

Happy coding,
http://entland.homelinux.com/blog/

Last edited by ent : 01-17-2009 at 05:02 AM.
ent is offline   Reply With Quote
Old 01-31-2009, 12:36 PM   #2
Axel
Valued Member
 
Join Date: Sep 2005
Location: Germany
Posts: 119
Default Re: Stripping comments from Shader bytecodes

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.
Axel is offline   Reply With Quote
Old 02-02-2009, 06:24 AM   #3
Nick
Senior Member
 
Join Date: Aug 2004
Location: Ghent, Belgium
Posts: 1,056
Default Re: Stripping comments from Shader bytecodes

Quote:
Originally Posted by 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.
Nick is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump


All times are GMT -7. The time now is 01:10 AM.


Powered by vBulletin
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.