1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkXMLShader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkXMLShader.h"
#include "vtkObjectFactory.h"
#include "vtkShaderCodeLibrary.h"
#include "vtkToolkits.h" // for VTK_MATERIALS_DIRS.
#include "vtkXMLDataElement.h"
#include <vtksys/SystemTools.hxx>
#include <assert.h>
vtkStandardNewMacro(vtkXMLShader);
vtkCxxSetObjectMacro(vtkXMLShader, SourceLibraryElement, vtkXMLDataElement);
//-----------------------------------------------------------------------------
vtkXMLShader::vtkXMLShader()
: Code(NULL),
RootElement(NULL),
SourceLibraryElement(NULL),
Args(NULL)
{
}
//-----------------------------------------------------------------------------
vtkXMLShader::~vtkXMLShader()
{
if (this->RootElement)
{
this->RootElement->UnRegister(this);
this->RootElement = 0;
}
this->SetSourceLibraryElement(0);
this->SetCode(0);
this->CleanupArgs();
}
//-----------------------------------------------------------------------------
void vtkXMLShader::SetRootElement(vtkXMLDataElement* root)
{
vtkSetObjectBodyMacro(RootElement, vtkXMLDataElement, root);
this->SetCode(0);
this->SetSourceLibraryElement(0); // release the SourceLibrary element.
// Determine if this shader description uses a library.
if (this->RootElement)
{
switch (this->GetLocation())
{
case vtkXMLShader::LOCATION_LIBRARY:
{
const char* name = this->RootElement->GetAttribute("name");
this->Code = vtkShaderCodeLibrary::GetShaderCode(name);
// TODO: the library should be XML enclosed.
// For now, it's not.
if (!this->Code)
{
vtkErrorMacro("Failed to locate library " << name);
return;
}
}
break;
case vtkXMLShader::LOCATION_FILE:
{
const char* filename = this->RootElement->GetAttribute("location");
char* fullpath = this->LocateFile(filename);
if (!fullpath)
{
vtkErrorMacro("Failed to locate file " << filename);
return;
}
this->ReadCodeFromFile(fullpath);
delete [] fullpath;
}
break;
}
}
}
//-----------------------------------------------------------------------------
// Note that this method allocates a new string which must be deleted by
// the caller.
char* vtkXMLShader::LocateFile(const char* filename)
{
if(!filename)
{
return NULL;
}
// if filename is absolute path, return the same.
if (vtksys::SystemTools::FileExists(filename))
{
return vtksys::SystemTools::DuplicateString(filename);
}
// Fetch any runtime defined user paths for materials
std::vector<std::string> paths;
std::string userpaths;
vtksys::SystemTools::GetEnv("USER_MATERIALS_DIRS", userpaths);
if (userpaths.size()>0)
{
vtksys::SystemTools::Split(userpaths.c_str(), paths, ';');
}
#ifdef VTK_MATERIALS_DIRS
// search thru default paths to locate file.
vtksys::SystemTools::Split(VTK_MATERIALS_DIRS, paths, ';');
#endif
for (unsigned int i =0; i < paths.size(); i++)
{
std::string path = paths[i];
if (path.size() == 0)
{
continue;
}
vtksys::SystemTools::ConvertToUnixSlashes(path);
if (path[path.size()-1] != '/')
{
path += "/";
}
path += filename;
if (vtksys::SystemTools::FileExists(path.c_str()))
{
return vtksys::SystemTools::DuplicateString(path.c_str());
}
}
return NULL;
}
//-----------------------------------------------------------------------------
void vtkXMLShader::ReadCodeFromFile(const char* filepath)
{
// Delete the existing code first. If 'filepath' doesn't exist,
// default to standard rendering.
if (this->Code)
{
delete [] this->Code;
this->Code = 0;
}
ifstream ifp;
ifp.open(filepath, ios::binary);
if (!ifp)
{
vtkErrorMacro("Failed to open file " << filepath);
return;
}
// determine the length of the file.
long length;
ifp.seekg(0, ios::end);
length = ifp.tellg();
ifp.seekg(0, ios::beg);
// Allocate for the file and the null terminator.
this->Code = new char[length+1];
ifp.read(this->Code, length);
// See how many characters were actually read. On Windows, CRLF line endings
// are read as a single char, so the number of read bytes will be fewer than
// the number of bytes reported in the file size query above.
long charsRead = ifp.gcount();
ifp.close();
// Null terminate the string so GL doesn't get confused.
this->Code[charsRead] = '\0';
}
//-----------------------------------------------------------------------------
int vtkXMLShader::GetLanguage()
{
if (this->RootElement)
{
const char* language = this->RootElement->GetAttribute("language");
if (!language)
{
vtkErrorMacro("Shader description missing Language attribute.");
}
else if (strcmp(language, "Cg") == 0)
{
return vtkXMLShader::LANGUAGE_CG;
}
else if (strcmp(language, "GLSL") == 0)
{
return vtkXMLShader::LANGUAGE_GLSL;
}
}
return vtkXMLShader::LANGUAGE_NONE;
}
//-----------------------------------------------------------------------------
int vtkXMLShader::GetScope()
{
if (this->RootElement)
{
const char* scope = this->RootElement->GetAttribute("scope");
if (!scope)
{
vtkErrorMacro("Shader description missing \"scope\" attribute.");
}
else if (strcmp(scope, "Vertex") == 0)
{
return vtkXMLShader::SCOPE_VERTEX;
}
else if (strcmp(scope, "Fragment") == 0)
{
return vtkXMLShader::SCOPE_FRAGMENT;
}
}
return vtkXMLShader::SCOPE_NONE;
}
//-----------------------------------------------------------------------------
int vtkXMLShader::GetLocation()
{
if (this->RootElement)
{
const char* loc= this->RootElement->GetAttribute("location");
if (!loc)
{
vtkErrorMacro("Shader description missing 'location' attribute.");
}
else if (strcmp(loc, "Inline") == 0)
{
return vtkXMLShader::LOCATION_INLINE;
}
else if (strcmp(loc, "Library") == 0)
{
return vtkXMLShader::LOCATION_LIBRARY;
}
else
{
// assume its a filename.
return vtkXMLShader::LOCATION_FILE;
}
}
return vtkXMLShader::LOCATION_NONE;
}
// ----------------------------------------------------------------------------
// \post valid_result: result==1 || result==2
int vtkXMLShader::GetStyle()
{
int result=1;
if(this->RootElement)
{
const char *loc=this->RootElement->GetAttribute("style");
if(loc==0)
{
// fine. this attribute is optional.
}
else
{
if(strcmp(loc,"1")==0)
{
// fine. default value.
}
else
{
if(strcmp(loc,"2")==0)
{
result=2; // new style
}
else
{
vtkErrorMacro(<<"style number not supported. Expect 1 or 2. We force it to be 1.");
}
}
}
}
assert("post valid_result" && (result==1 || result==2) );
return result;
}
//-----------------------------------------------------------------------------
const char* vtkXMLShader::GetName()
{
return (this->RootElement)? this->RootElement->GetAttribute("name") : 0;
}
//-----------------------------------------------------------------------------
const char* vtkXMLShader::GetEntry()
{
return (this->RootElement)? this->RootElement->GetAttribute("entry") : 0;
}
//-----------------------------------------------------------------------------
const char** vtkXMLShader::GetArgs()
{
this->CleanupArgs();
if (!this->RootElement || !this->RootElement->GetAttribute("args"))
{
return 0;
}
std::vector<std::string> args;
vtksys::SystemTools::Split(this->RootElement->GetAttribute("args"), args, ' ');
int i;
int size = static_cast<int>(args.size());
if (size == 0)
{
return 0;
}
this->Args = new char*[size+1];
for (i=0; i < size; i++)
{
this->Args[i] = vtksys::SystemTools::DuplicateString(args[i].c_str());
}
this->Args[size] = 0;
return const_cast<const char**>(this->Args);
}
//-----------------------------------------------------------------------------
const char* vtkXMLShader::GetCode()
{
switch(this->GetLocation())
{
case vtkXMLShader::LOCATION_INLINE:
return this->RootElement->GetCharacterData();
break;
case vtkXMLShader::LOCATION_LIBRARY:
// until the ShaderCode library starts providing XMLs, we just return the code.
return this->Code;
break;
case vtkXMLShader::LOCATION_FILE:
return this->Code;
break;
}
return 0;
}
//-----------------------------------------------------------------------------
void vtkXMLShader::CleanupArgs()
{
if (this->Args)
{
char** a = this->Args;
while (*a)
{
delete [] (*a);
a++;
}
delete [] this->Args;
this->Args = 0;
}
}
//-----------------------------------------------------------------------------
void vtkXMLShader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Name: " << (this->GetName()? this->GetName() : "(none)")
<< endl;
os << indent << "Scope: ";
switch(this->GetScope())
{
case SCOPE_NONE:
os << "None";
break;
case SCOPE_MIXED:
os << "Mixed";
break;
case SCOPE_VERTEX:
os << "Vertex";
break;
case SCOPE_FRAGMENT:
os << "Fragment";
break;
}
os << endl;
os << indent << "Language: ";
switch (this->GetLanguage())
{
case LANGUAGE_NONE:
os << "None";
break;
case LANGUAGE_MIXED:
os << "Mixed";
break;
case LANGUAGE_CG:
os << "Cg";
break;
case LANGUAGE_GLSL:
os << "GLSL";
}
os << endl;
os << indent << "Location: ";
switch (this->GetLocation())
{
case LOCATION_NONE:
os << "None";
break;
case LOCATION_INLINE:
os << "Inline";
break;
case LOCATION_FILE:
os << "(loaded from a source file)";
break;
case LOCATION_LIBRARY:
os << "Library";
break;
}
os << endl;
os << indent << "Entry: "
<< (this->GetEntry()? this->GetEntry() : "(none)") << endl;
os << indent << "Args: ";
const char** args = this->GetArgs();
if (!args)
{
os << "(none)" << endl;
}
else
{
while (*args)
{
os << indent << *args << " ";
args++;
}
os << endl;
}
os << indent << "RootElement: ";
if (this->RootElement)
{
os << endl;
this->RootElement->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
}
|