Diligent Engine API Reference
GLProgramResources.h
1 /* Copyright 2015-2018 Egor Yusov
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12  *
13  * In no event and under no legal theory, whether in tort (including negligence),
14  * contract, or otherwise, unless required by applicable law (such as deliberate
15  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16  * liable for any damages, including any direct, indirect, special, incidental,
17  * or consequential damages of any character arising as a result of this License or
18  * out of the use or inability to use the software (including but not limited to damages
19  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20  * all other commercial damages or losses), even if such Contributor has been advised
21  * of the possibility of such damages.
22  */
23 
24 #pragma once
25 #include "GLObjectWrapper.h"
26 #include "HashUtils.h"
27 #include "ShaderBase.h"
28 #include "SamplerGLImpl.h"
29 
30 #ifdef _DEBUG
31 # define VERIFY_RESOURCE_BINDINGS
32 #endif
33 
34 namespace Diligent
35 {
36  class GLProgramResources
37  {
38  public:
39  GLProgramResources();
40  GLProgramResources( GLProgramResources&& Program );
41 
42  void LoadUniforms(class RenderDeviceGLImpl *pDeviceGLImpl,
43  GLuint GLProgram,
44  const SHADER_VARIABLE_TYPE DefaultVariableType,
45  const ShaderVariableDesc *VariableDesc,
46  Uint32 NumVars,
47  const StaticSamplerDesc *StaticSamplers,
48  Uint32 NumStaticSamplers);
49 
50  void Clone(const GLProgramResources& SrcLayout,
51  SHADER_VARIABLE_TYPE *VarTypes,
52  Uint32 NumVarTypes,
53  IObject &Owner);
54 
55  struct GLProgramVariableBase
56  {
57  GLProgramVariableBase( const Char* _Name, size_t _ArraySize, SHADER_VARIABLE_TYPE _VarType) :
58  Name( _Name ),
59  VarType(_VarType),
60  pResources(_ArraySize)
61  {
62  VERIFY_EXPR(_ArraySize >= 1);
63  }
64 
65  String Name;
66  std::vector< RefCntAutoPtr<IDeviceObject> > pResources;
67  SHADER_VARIABLE_TYPE VarType;
68  };
69 
70  struct UniformBufferInfo : GLProgramVariableBase
71  {
72  UniformBufferInfo(const Char* _Name, size_t _ArraySize, SHADER_VARIABLE_TYPE _VarType, GLint _Index) :
73  GLProgramVariableBase(_Name, _ArraySize, _VarType),
74  Index(_Index)
75  {}
76 
77  GLuint Index;
78  };
79  std::vector<UniformBufferInfo>& GetUniformBlocks(){ return m_UniformBlocks; }
80 
81  struct SamplerInfo : GLProgramVariableBase
82  {
83  SamplerInfo(const Char* _Name, size_t _ArraySize, SHADER_VARIABLE_TYPE _VarType, GLint _Location, GLenum _Type, class SamplerGLImpl *_pStaticSampler) :
84  GLProgramVariableBase(_Name, _ArraySize, _VarType),
85  Location(_Location),
86  Type(_Type),
87  pStaticSampler(_pStaticSampler)
88  {}
89  GLint Location;
90  GLenum Type;
91  RefCntAutoPtr<class SamplerGLImpl> pStaticSampler;
92  };
93  std::vector<SamplerInfo>& GetSamplers(){ return m_Samplers; }
94 
95  struct ImageInfo : GLProgramVariableBase
96  {
97  ImageInfo(const Char* _Name, size_t _ArraySize, SHADER_VARIABLE_TYPE _VarType, GLint _BindingPoint, GLenum _Type) :
98  GLProgramVariableBase(_Name, _ArraySize, _VarType),
99  BindingPoint(_BindingPoint),
100  Type(_Type)
101  {}
102 
103  GLint BindingPoint;
104  GLenum Type;
105  };
106  std::vector<ImageInfo>& GetImages(){ return m_Images; }
107 
108  struct StorageBlockInfo : GLProgramVariableBase
109  {
110  StorageBlockInfo(const Char* _Name, size_t _ArraySize, SHADER_VARIABLE_TYPE _VarType, GLint _Binding) :
111  GLProgramVariableBase(_Name, _ArraySize, _VarType),
112  Binding(_Binding)
113  {}
114 
115  GLint Binding;
116  };
117  std::vector<StorageBlockInfo>& GetStorageBlocks(){ return m_StorageBlocks; }
118 
119 
120  struct CGLShaderVariable : ShaderVariableBase
121  {
122  CGLShaderVariable( IObject &Owner, GLProgramResources::GLProgramVariableBase &ProgVar ) :
123  ShaderVariableBase( Owner ),
124  ProgramVar(ProgVar)
125  {}
126 
127  virtual void Set( IDeviceObject *pObject )override final
128  {
129  ProgramVar.pResources[0] = pObject;
130  }
131 
132  virtual void SetArray(IDeviceObject* const* ppObjects, Uint32 FirstElement, Uint32 NumElements)override final
133  {
134  for(Uint32 i=0; i < NumElements; ++i)
135  ProgramVar.pResources[FirstElement + i] = ppObjects[i];
136  }
137 
138  private:
139  GLProgramVariableBase &ProgramVar;
140  };
141 
142  void BindResources(IResourceMapping *pResourceMapping, Uint32 Flags);
143 
144 #ifdef VERIFY_RESOURCE_BINDINGS
145  void dbgVerifyResourceBindings();
146 #endif
147 
148  IShaderVariable* GetShaderVariable( const Char* Name );
149 
150  const std::unordered_map<HashMapStringKey, CGLShaderVariable>& GetVariables(){return m_VariableHash;}
151 
152  private:
153  const GLProgramResources& operator = (const GLProgramResources& Program);
154 
155  void InitVariables(IObject &Owner);
156 
157  std::vector<UniformBufferInfo> m_UniformBlocks;
158  std::vector< SamplerInfo > m_Samplers;
159  std::vector< ImageInfo > m_Images;
160  std::vector< StorageBlockInfo > m_StorageBlocks;
161 
163  std::unordered_map<HashMapStringKey, CGLShaderVariable> m_VariableHash;
164  // When adding new member DO NOT FORGET TO UPDATE GLProgramResources( GLProgramResources&& ProgramResources )!!!
165  };
166 }
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
SHADER_VARIABLE_TYPE
Describes shader variable type that is used by ShaderVariableDesc.
Definition: Shader.h:100