From 4262d102174b9016e649b4af6f7c3dfbd935250a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 18 Nov 2009 11:25:28 +0000 Subject: [PATCH] Added osg::ShaderBinary::readShaderBinaryFile(..) static method --- include/osg/Shader | 16 ++++++++++------ src/osg/Shader.cpp | 21 ++++++++++++++++++++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/include/osg/Shader b/include/osg/Shader index 449e7ef01..2fd9f8cad 100644 --- a/include/osg/Shader +++ b/include/osg/Shader @@ -35,7 +35,7 @@ class Program; class OSG_EXPORT ShaderBinary : public osg::Object { public: - + ShaderBinary(); /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ @@ -45,10 +45,10 @@ class OSG_EXPORT ShaderBinary : public osg::Object /** Allocated a data buffer of specified size/*/ void allocate(unsigned int size); - + /** Assign shader binary data, copying the specified data into locally stored data buffer, the original data can then be deleted.*/ void assign(unsigned int size, const unsigned char* data); - + /** Get the size of the shader binary data.*/ unsigned int getSize() const { return _data.size(); } @@ -57,9 +57,13 @@ class OSG_EXPORT ShaderBinary : public osg::Object /** Get a const ptr to the shader binary data.*/ const unsigned char* getData() const { return _data.empty() ? 0 : &(_data.front()); } - + + /** Read shader binary from file. + * Return the resulting Shader or 0 if no valid shader binary could be read.*/ + static ShaderBinary* readShaderBinaryFile(const std::string& fileName); + protected: - + typedef std::vector Data; Data _data; }; @@ -132,7 +136,7 @@ class OSG_EXPORT Shader : public osg::Object /** Read shader source from file and then constructor shader of specified type. - * Return the resulting Shader or 0 if no valid shader source code be read.*/ + * Return the resulting Shader or 0 if no valid shader source could be read.*/ static Shader* readShaderFile( Type type, const std::string& fileName ); /** Load the Shader's source code text from a file. */ diff --git a/src/osg/Shader.cpp b/src/osg/Shader.cpp index a35b58e2a..b4b4b1f64 100644 --- a/src/osg/Shader.cpp +++ b/src/osg/Shader.cpp @@ -63,7 +63,26 @@ void ShaderBinary::assign(unsigned int size, const unsigned char* data) } } - /////////////////////////////////////////////////////////////////////////// +ShaderBinary* ShaderBinary::readShaderBinaryFile(const std::string& fileName) +{ + std::ifstream fin; + fin.open(fileName.c_str(), std::ios::binary); + if (!fin) return 0; + + fin.seekg(0, std::ios::end); + int length = fin.tellg(); + if (length==0) return 0; + + osg::ref_ptr shaderBinary = new osg::ShaderBinary; + shaderBinary->allocate(length); + fin.seekg(0, std::ios::beg); + fin.read(reinterpret_cast(shaderBinary->getData()), length); + fin.close(); + + return shaderBinary.release(); +} + +/////////////////////////////////////////////////////////////////////////// // static cache of glShaders flagged for deletion, which will actually // be deleted in the correct GL context.