1 #include <osg/Shader>
2 #include <osgDB/ObjectWrapper>
3 #include <osgDB/Serializer>
4 
checkData(const osg::ShaderBinary & sb)5 static bool checkData( const osg::ShaderBinary& sb )
6 {
7     return sb.getSize()>0;
8 }
9 
readData(osgDB::InputStream & is,osg::ShaderBinary & sb)10 static bool readData( osgDB::InputStream& is, osg::ShaderBinary& sb )
11 {
12     unsigned int size; is >> size;
13     char* data = new char[size]();
14     if ( is.isBinary() )
15     {
16         is.readCharArray( data, size );
17     }
18     else
19     {
20         is >> is.BEGIN_BRACKET;
21         for ( unsigned int i=0; i<size; ++i )
22         {
23             is >> std::hex >> data[i] >> std::dec;
24         }
25         is >> is.END_BRACKET;
26     }
27 
28     if (size>0)
29     {
30         sb.assign( size, (unsigned char*)data );
31     }
32 
33     delete [] data;
34     return true;
35 }
36 
writeData(osgDB::OutputStream & os,const osg::ShaderBinary & sb)37 static bool writeData( osgDB::OutputStream& os, const osg::ShaderBinary& sb )
38 {
39     if ( os.isBinary() )
40     {
41         os << (unsigned int)sb.getSize();
42         os.writeCharArray( (char*)sb.getData(), sb.getSize() );
43     }
44     else
45     {
46         const unsigned char* data = sb.getData();
47         os << (unsigned int)sb.getSize();
48         os << os.BEGIN_BRACKET << std::endl;
49         for ( unsigned int i=0; i<sb.getSize(); ++i )
50         {
51             os << std::hex << data[i] << std::dec << std::endl;
52         }
53         os << os.END_BRACKET << std::endl;
54     }
55     return true;
56 }
57 
58 REGISTER_OBJECT_WRAPPER( ShaderBinary,
59                          new osg::ShaderBinary,
60                          osg::ShaderBinary,
61                          "osg::Object osg::ShaderBinary" )
62 {
63     ADD_USER_SERIALIZER( Data );  // _data
64 }
65