GteHLSLProgramFactory.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.1 (2016/09/12)
7 
8 #include <GTEnginePCH.h>
9 #include <LowLevel/GteLogger.h>
10 #if defined(GTE_USE_DX12)
11 #include <Graphics/DX12/GteDX12Include.h>
12 #else
14 #endif
19 using namespace gte;
20 
26 unsigned int HLSLProgramFactory::defaultFlags = (
27  D3DCOMPILE_ENABLE_STRICTNESS |
28  D3DCOMPILE_IEEE_STRICTNESS |
29  D3DCOMPILE_OPTIMIZATION_LEVEL3);
30 
32 {
33 }
34 
36 {
43 }
44 
46 {
47  return PF_HLSL;
48 }
49 
50 std::shared_ptr<VisualProgram> HLSLProgramFactory::CreateFromBytecode(
51  std::vector<unsigned char> const& vsBytecode,
52  std::vector<unsigned char> const& psBytecode,
53  std::vector<unsigned char> const& gsBytecode)
54 {
55  if (vsBytecode.size() == 0 || psBytecode.size() == 0)
56  {
57  LogError("A program must have a vertex shader and a pixel shader.");
58  return nullptr;
59  }
60 
61  std::shared_ptr<VertexShader> vshader;
62  std::shared_ptr<PixelShader> pshader;
63  std::shared_ptr<GeometryShader> gshader;
64 
66  vsEntry, std::string("vs_") + version, vsBytecode.size(),
67  vsBytecode.data());
68  if (hlslVShader.IsValid())
69  {
70  vshader =
71  std::static_pointer_cast<VertexShader>(std::make_shared<Shader>(hlslVShader));
72  }
73  else
74  {
75  return nullptr;
76  }
77 
79  psEntry, std::string("ps_") + version, psBytecode.size(),
80  psBytecode.data());
81  if (hlslPShader.IsValid())
82  {
83  pshader =
84  std::static_pointer_cast<PixelShader>(std::make_shared<Shader>(hlslPShader));
85  }
86  else
87  {
88  return nullptr;
89  }
90 
91  HLSLShader hlslGShader;
92  if (gsBytecode.size() > 0)
93  {
94  hlslGShader = HLSLShaderFactory::CreateFromBytecode("gs",
95  gsEntry, std::string("gs_") + version, gsBytecode.size(),
96  gsBytecode.data());
97  if (hlslGShader.IsValid())
98  {
99  gshader =
100  std::static_pointer_cast<GeometryShader>(std::make_shared<Shader>(hlslGShader));
101  }
102  else
103  {
104  return nullptr;
105  }
106  }
107 
108  std::shared_ptr<HLSLVisualProgram> program = std::make_shared<HLSLVisualProgram>();
109  program->SetVShader(vshader);
110  program->SetPShader(pshader);
111  program->SetGShader(gshader);
112  return program;
113 }
114 
115 std::shared_ptr<ComputeProgram> HLSLProgramFactory::CreateFromNamedSource(
116  std::string const& csName, std::string const& csSource)
117 {
118  if (csSource == "")
119  {
120  LogError("A program must have a compute shader.");
121  return nullptr;
122  }
123 
124  HLSLShader hlslCShader = HLSLShaderFactory::CreateFromString(csName,
125  csSource, csEntry, std::string("cs_") + version, defines, flags);
126  if (hlslCShader.IsValid())
127  {
128  std::shared_ptr<ComputeShader> cshader =
129  std::static_pointer_cast<ComputeShader>(std::make_shared<Shader>(hlslCShader));
130 
131  std::shared_ptr<HLSLComputeProgram> program = std::make_shared<HLSLComputeProgram>();
132  program->SetCShader(cshader);
133  return program;
134  }
135  else
136  {
137  return nullptr;
138  }
139 }
140 
141 std::shared_ptr<VisualProgram> HLSLProgramFactory::CreateFromNamedSources(
142  std::string const& vsName, std::string const& vsSource,
143  std::string const& psName, std::string const& psSource,
144  std::string const& gsName, std::string const& gsSource)
145 {
146  if (vsSource == "" || psSource == "")
147  {
148  LogError("A program must have a vertex shader and a pixel shader.");
149  return nullptr;
150  }
151 
152  std::shared_ptr<VertexShader> vshader;
153  std::shared_ptr<PixelShader> pshader;
154  std::shared_ptr<GeometryShader> gshader;
155 
156  HLSLShader hlslVShader = HLSLShaderFactory::CreateFromString(vsName,
157  vsSource, vsEntry, std::string("vs_") + version, defines, flags);
158  if (hlslVShader.IsValid())
159  {
160  vshader =
161  std::static_pointer_cast<VertexShader>(std::make_shared<Shader>(hlslVShader));
162  }
163  else
164  {
165  return nullptr;
166  }
167 
168  HLSLShader hlslPShader = HLSLShaderFactory::CreateFromString(psName,
169  psSource, psEntry, std::string("ps_") + version, defines, flags);
170  if (hlslPShader.IsValid())
171  {
172  pshader =
173  std::static_pointer_cast<PixelShader>(std::make_shared<Shader>(hlslPShader));
174  }
175  else
176  {
177  return nullptr;
178  }
179 
180  HLSLShader hlslGShader;
181  if (gsSource != "")
182  {
183  hlslGShader = HLSLShaderFactory::CreateFromString(gsName,
184  gsSource, gsEntry, std::string("gs_") + version, defines, flags);
185  if (hlslGShader.IsValid())
186  {
187  gshader =
188  std::static_pointer_cast<GeometryShader>(std::make_shared<Shader>(hlslGShader));
189  }
190  else
191  {
192  return nullptr;
193  }
194  }
195 
196  std::shared_ptr<HLSLVisualProgram> program = std::make_shared<HLSLVisualProgram>();
197  program->SetVShader(vshader);
198  program->SetPShader(pshader);
199  program->SetGShader(gshader);
200  return program;
201 }
202 
203 std::shared_ptr<ComputeProgram> HLSLProgramFactory::CreateFromBytecode(
204  std::vector<unsigned char> const& csBytecode)
205 {
206  if (csBytecode.size() == 0)
207  {
208  LogError("A program must have a compute shader.");
209  return nullptr;
210  }
211 
213  csEntry, std::string("cs_") + version, csBytecode.size(),
214  csBytecode.data());
215  if (hlslCShader.IsValid())
216  {
217  std::shared_ptr<ComputeShader> cshader =
218  std::static_pointer_cast<ComputeShader>(std::make_shared<Shader>(hlslCShader));
219 
220  std::shared_ptr<HLSLComputeProgram> program = std::make_shared<HLSLComputeProgram>();
221  program->SetCShader(cshader);
222  return program;
223  }
224  else
225  {
226  return nullptr;
227  }
228 }
static std::string defaultVersion
bool IsValid() const
static HLSLShader CreateFromBytecode(std::string const &name, std::string const &entry, std::string const &target, size_t numBytes, unsigned char const *bytecode)
static std::string defaultCSEntry
GLbitfield GLuint program
Definition: glcorearb.h:1926
static std::string defaultVSEntry
static std::string defaultGSEntry
ProgramDefines defines
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
#define LogError(message)
Definition: GteLogger.h:92
static std::string defaultPSEntry
GLbitfield flags
Definition: glcorearb.h:1591
std::shared_ptr< VisualProgram > CreateFromBytecode(std::vector< unsigned char > const &vsBytecode, std::vector< unsigned char > const &psBytecode, std::vector< unsigned char > const &gsBytecode)
virtual std::shared_ptr< ComputeProgram > CreateFromNamedSource(std::string const &csName, std::string const &csSource)
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)
static HLSLShader CreateFromString(std::string const &name, std::string const &source, std::string const &entry, std::string const &target, ProgramDefines const &defines, unsigned int compileFlags)
static unsigned int defaultFlags


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