Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HlslProg.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include <string>
26 #include <sstream>
27 #include <iostream>
28 #include <fstream>
29 #include <exception>
30 #include <map>
31 
32 #include "cinder/dx/dx.h"
33 #include "cinder/Vector.h"
34 #include "cinder/Color.h"
35 #include "cinder/Matrix.h"
36 #include "cinder/DataSource.h"
37 #include <d3d11.h>
38 
39 namespace cinder { namespace dx {
40 
41 class HlslProg;
42 typedef std::shared_ptr<HlslProg> HlslProgRef;
43 
45 class HlslProg {
46 public:
47 
48  HlslProg() {}
50  HlslProg(
51  DataSourceRef vertexShader,
52  DataSourceRef fragmentShader,
53  DataSourceRef geometryShader = DataSourceRef()
54  );
56  HlslProg(
57  const BYTE *vertexShader, UINT vertexShaderSize,
58  const BYTE *fragmentShader, UINT fragmentShaderSize,
59  const BYTE *geometryShader = nullptr, UINT geometryShaderSize = 0
60  );
62  HlslProg(
63  const std::string& vertexEntryPoint, DataSourceRef vertexShader,
64  const std::string& fragmentEntryPoint, DataSourceRef fragmentShader,
65  const std::string& geometryEntryPoint = "", DataSourceRef geometryShader = DataSourceRef()
66  );
68  HlslProg(
69  const std::string& vertexEntryPoint, const char *vertexShader,
70  const std::string& fragmentEntryPoint, const char *fragmentShader,
71  const std::string& geometryEntryPoint = "", const char *geometryShader = nullptr
72  );
73 
75  static HlslProgRef create (
76  DataSourceRef vertexShader,
77  DataSourceRef fragmentShader,
78  DataSourceRef geometryShader = DataSourceRef()
79  );
81  static HlslProgRef create(
82  const BYTE *vertexShader, UINT vertexShaderSize,
83  const BYTE *fragmentShader, UINT fragmentShaderSize,
84  const BYTE *geometryShader = nullptr, UINT geometryShaderSize = 0
85  );
87  static HlslProgRef create(
88  const std::string& vertexEntryPoint, DataSourceRef vertexShaderSrc,
89  const std::string& fragmentEntryPoint, DataSourceRef fragmentShaderSrc,
90  const std::string& geometryEntryPoint = "", DataSourceRef geometryShaderSrc = DataSourceRef()
91  );
93  static HlslProgRef create(
94  const std::string& vertexEntryPoint, const char *vertexShaderSrc,
95  const std::string& fragmentEntryPoint, const char *fragmentShaderSrc,
96  const std::string& geometryEntryPoint = "", const char *geometryShaderSrc = nullptr
97  );
98 
99  void bind() const;
100  static void unbind();
101 
102  ID3D11VertexShader* GetVertexShader() { return mObj->mVS; }
103  ID3D11PixelShader* GetPixelShader() { return mObj->mPS; }
104  ID3D11GeometryShader* GetGeometryShader() { return mObj->mGS; }
105  ID3D11ComputeShader* GetComputeShader() { return mObj->mCS; }
106 
107  void CreateCBufferVertex(UINT slot, UINT size);
108  void* MapCBufferVertex(UINT slot);
109  void UnmapCBufferVertex(UINT slot);
110 
111  void CreateCBufferFragment(UINT slot, UINT size);
112  void* MapCBufferFragment(UINT slot);
113  void UnmapCBufferFragment(UINT slot);
114 
115  void CreateCBufferGeometry(UINT slot, UINT size);
116  void* MapCBufferGeometry(UINT slot);
117  void UnmapCBufferGeometry(UINT slot);
118 
119  void CreateCBufferCompute(UINT slot, UINT size);
120  void* MapCBufferCompute(UINT slot);
121  void UnmapCBufferCompute(UINT slot);
122 
123 protected:
124  struct Cbo //constant buffer object
125  {
126  Cbo(UINT slot, UINT size);
127  ~Cbo();
128 
129  UINT mSlot;
130  ID3D11Buffer *mCBuffer;
131  };
132 
133  struct Obj {
134  Obj() : mVS( nullptr ), mPS( nullptr ), mGS( nullptr ), mCS( nullptr ) {}
135  ~Obj();
136 
137  //GLuint mHandle;
138  //std::map<std::string,int> mUniformLocs;
139  ID3D11VertexShader* mVS;
140  ID3D11PixelShader* mPS;
141  ID3D11GeometryShader* mGS;
142  ID3D11ComputeShader* mCS;
143  std::vector<std::shared_ptr<Cbo>> mCBuffersVertex;
144  std::vector<std::shared_ptr<Cbo>> mCBuffersFragment;
145  std::vector<std::shared_ptr<Cbo>> mCBuffersGeometry;
146  std::vector<std::shared_ptr<Cbo>> mCBuffersCompute;
147  };
148 
149  std::shared_ptr<Obj> mObj;
150 
151 private:
152  void initFromSource(
153  const std::string& vertexShaderName, const char *vertexShaderSrc,
154  const std::string& fragmentShaderName, const char *fragmentShaderSrc,
155  const std::string& geometryShaderName, const char *geometryShaderSrc
156  );
157 
158 public:
160  typedef std::shared_ptr<Obj> HlslProg::*unspecified_bool_type;
162  operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &HlslProg::mObj; }
163  void reset() { mObj.reset(); }
165 };
166 
167 class HlslProgCompileExc : public std::exception {
168 public:
169  HlslProgCompileExc( const std::string &log, GLint aShaderType ) throw();
170  virtual const char* what() const throw()
171  {
172  return mMessage;
173  }
174 
175 private:
176  char mMessage[16001];
177  GLint mShaderType;
178 };
179 
180 class HlslNullProgramExc : public std::exception {
181 public:
182  virtual const char* what() const throw()
183  {
184  return "Hlsl: Attempt to use null shader";
185  }
186 
187 };
188 
189 class HlslDuplicateCBufferExc : public std::exception {
190 public:
191  virtual const char* what() const throw()
192  {
193  return "Hlsl: Attempt to create a duplicate constant buffer";
194  }
195 };
196 
197 } } // namespace cinder::dx
void * MapCBufferFragment(UINT slot)
Definition: HlslProg.cpp:240
void CreateCBufferVertex(UINT slot, UINT size)
Definition: HlslProg.cpp:201
ID3D11PixelShader * GetPixelShader()
Definition: HlslProg.h:103
GLsizei const GLchar ** string
Definition: GLee.h:2427
ID3D11ComputeShader * GetComputeShader()
Definition: HlslProg.h:105
ID3D11VertexShader * mVS
Definition: HlslProg.h:139
std::shared_ptr< Obj > mObj
Definition: HlslProg.h:149
HlslProg()
Definition: HlslProg.h:48
std::vector< std::shared_ptr< Cbo > > mCBuffersCompute
Definition: HlslProg.h:146
void bind() const
Definition: HlslProg.cpp:167
std::shared_ptr< Obj > HlslProg::* unspecified_bool_type
Emulates shared_ptr-like behavior.
Definition: HlslProg.h:161
Definition: HlslProg.h:133
Definition: HlslProg.h:124
void UnmapCBufferGeometry(UINT slot)
Definition: HlslProg.cpp:284
void UnmapCBufferFragment(UINT slot)
Definition: HlslProg.cpp:253
Definition: HlslProg.h:167
Definition: HlslProg.h:189
std::shared_ptr< HlslProg > HlslProgRef
Definition: HlslProg.h:41
Represents a DirectX HLSL shader. Implicitly shared object.
Definition: HlslProg.h:45
Cbo(UINT slot, UINT size)
Definition: HlslProg.cpp:38
std::vector< std::shared_ptr< Cbo > > mCBuffersGeometry
Definition: HlslProg.h:145
HlslProgCompileExc(const std::string &log, GLint aShaderType)
Definition: HlslProg.cpp:296
Definition: HlslProg.h:180
std::vector< std::shared_ptr< Cbo > > mCBuffersVertex
Definition: HlslProg.h:143
void UnmapCBufferVertex(UINT slot)
Definition: HlslProg.cpp:222
ID3D11GeometryShader * GetGeometryShader()
Definition: HlslProg.h:104
~Obj()
Definition: HlslProg.cpp:56
virtual const char * what() const
Definition: HlslProg.h:170
void CreateCBufferFragment(UINT slot, UINT size)
Definition: HlslProg.cpp:232
void * MapCBufferCompute(UINT slot)
~Cbo()
Definition: HlslProg.cpp:51
ID3D11Buffer * mCBuffer
Definition: HlslProg.h:130
virtual const char * what() const
Definition: HlslProg.h:191
static void unbind()
Definition: HlslProg.cpp:192
void * MapCBufferVertex(UINT slot)
Definition: HlslProg.cpp:209
UINT mSlot
Definition: HlslProg.h:129
virtual const char * what() const
Definition: HlslProg.h:182
ID3D11VertexShader * GetVertexShader()
Definition: HlslProg.h:102
void CreateCBufferGeometry(UINT slot, UINT size)
Definition: HlslProg.cpp:263
Obj()
Definition: HlslProg.h:134
void UnmapCBufferCompute(UINT slot)
int int int * dx
Definition: GLee.h:17162
void reset()
Emulates shared_ptr-like behavior.
Definition: HlslProg.h:163
int GLint
Definition: gldx.h:51
static HlslProgRef create(DataSourceRef vertexShader, DataSourceRef fragmentShader, DataSourceRef geometryShader=DataSourceRef())
Constructs a shader from compiled object bytecode.
ID3D11GeometryShader * mGS
Definition: HlslProg.h:141
std::shared_ptr< class DataSource > DataSourceRef
Definition: DataSource.h:35
void * MapCBufferGeometry(UINT slot)
Definition: HlslProg.cpp:271
ID3D11PixelShader * mPS
Definition: HlslProg.h:140
std::vector< std::shared_ptr< Cbo > > mCBuffersFragment
Definition: HlslProg.h:144
GLsizeiptr size
Definition: GLee.h:2089
ID3D11ComputeShader * mCS
Definition: HlslProg.h:142
void CreateCBufferCompute(UINT slot, UINT size)