GteProgramFactory.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
9 #include <LowLevel/GteLogger.h>
11 #include <fstream>
12 using namespace gte;
13 
14 
16 {
17 }
18 
20  :
21  version(""),
22  vsEntry(""),
23  psEntry(""),
24  gsEntry(""),
25  csEntry(""),
26  defines(),
27  flags(0)
28 {
29 }
30 
31 std::shared_ptr<VisualProgram> ProgramFactory::CreateFromFiles(
32  std::string const& vsFile, std::string const& psFile,
33  std::string const& gsFile)
34 {
35  if (vsFile == "" || psFile == "")
36  {
37  LogError("A program must have a vertex shader and a pixel shader.");
38  return nullptr;
39  }
40 
41  std::string vsSource = GetStringFromFile(vsFile);
42  if (vsSource == "")
43  {
44  return nullptr;
45  }
46 
47  std::string psSource = GetStringFromFile(psFile);
48  if (psSource == "")
49  {
50  return nullptr;
51  }
52 
53  std::string gsSource = "";
54  if (gsFile != "")
55  {
56  gsSource = GetStringFromFile(gsFile);
57  if (gsSource == "")
58  {
59  return nullptr;
60  }
61  }
62 
63  return CreateFromNamedSources(vsFile, vsSource, psFile, psSource, gsFile,
64  gsSource);
65 }
66 
67 std::shared_ptr<VisualProgram> ProgramFactory::CreateFromSources(
68  std::string const& vsSource, std::string const& psSource,
69  std::string const& gsSource)
70 {
71  return CreateFromNamedSources("vs", vsSource, "ps", psSource, "gs",
72  gsSource);
73 }
74 
75 std::shared_ptr<ComputeProgram> ProgramFactory::CreateFromFile(
76  std::string const& csFile)
77 {
78  if (csFile == "")
79  {
80  LogError("A program must have a compute shader.");
81  return nullptr;
82  }
83 
84  std::string csSource = GetStringFromFile(csFile);
85  if (csSource == "")
86  {
87  return nullptr;
88  }
89 
90  return CreateFromNamedSource(csFile, csSource);
91 }
92 
93 std::shared_ptr<ComputeProgram> ProgramFactory::CreateFromSource(
94  std::string const& csSource)
95 {
96  return CreateFromNamedSource("cs", csSource);
97 }
98 
100 {
101  std::string source = "";
102  std::ifstream input(filename);
103  if (input)
104  {
105  while (!input.eof())
106  {
107  std::string line;
108  getline(input, line);
109  source += line + "\n";
110  }
111  input.close();
112  }
113  else
114  {
115  LogError("Cannot open file " + filename);
116  }
117  return source;
118 }
119 
121 {
122  mDefinesStack.push(defines);
123  defines.Clear();
124 }
125 
127 {
128  if (mDefinesStack.size() > 0)
129  {
130  defines = mDefinesStack.top();
131  mDefinesStack.pop();
132  }
133 }
134 
136 {
137  mFlagsStack.push(flags);
138  flags = 0;
139 }
140 
142 {
143  if (mFlagsStack.size() > 0)
144  {
145  flags = mFlagsStack.top();
146  mFlagsStack.pop();
147  }
148 }
149 
virtual std::shared_ptr< VisualProgram > CreateFromNamedSources(std::string const &vsName, std::string const &vsSource, std::string const &psName, std::string const &psSource, std::string const &gsName, std::string const &gsSource)=0
std::shared_ptr< ComputeProgram > CreateFromSource(std::string const &csSource)
ProgramDefines defines
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:798
virtual std::shared_ptr< ComputeProgram > CreateFromNamedSource(std::string const &csName, std::string const &csSource)=0
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
#define LogError(message)
Definition: GteLogger.h:92
GLbitfield flags
Definition: glcorearb.h:1591
std::stack< unsigned int > mFlagsStack
std::shared_ptr< VisualProgram > CreateFromFiles(std::string const &vsFile, std::string const &psFile, std::string const &gsFile)
std::stack< ProgramDefines > mDefinesStack
std::shared_ptr< VisualProgram > CreateFromSources(std::string const &vsSource, std::string const &psSource, std::string const &gsSource)
GLenum GLenum GLenum input
Definition: glext.h:9913
static std::string GetStringFromFile(std::string const &filename)
std::shared_ptr< ComputeProgram > CreateFromFile(std::string const &csFile)


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01