MxEngine
Shader.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include "Utilities/Math/Math.h"
32 #include "core/Macro/Macro.h"
33 #include "Utilities/STL/MxString.h"
34 #include "Utilities/STL/MxHashMap.h"
35 
36 namespace MxEngine
37 {
38  class Shader
39  {
40  #if defined(MXENGINE_DEBUG)
41  MxString vertexShaderPath, geometryShaderPath, fragmentShaderPath;
42  #endif
43  using UniformType = int;
44  using UniformCache = MxHashMap<MxString, UniformType>;
45  using ShaderId = unsigned int;
46  using BindableId = unsigned int;
47 
48  BindableId id = 0;
49  mutable UniformCache uniformCache;
50 
51  ShaderId CompileShader(unsigned int type, const MxString& source, const MxString& name) const;
52  BindableId CreateProgram(ShaderId vertexShader, ShaderId fragmentShader) const;
53  BindableId CreateProgram(ShaderId vertexShader, ShaderId geometryShader, ShaderId fragmentShader) const;
54  UniformType GetUniformLocation(const MxString& uniformName) const;
55  public:
56  Shader();
57  Shader(const MxString& vertexShaderPath, const MxString& fragmentShaderPath);
58  Shader(const MxString& vertexShaderPath, const MxString& geometryShaderPath, const MxString& fragmentShaderPath);
59  Shader(const Shader&) = delete;
60  Shader(Shader&& shader) noexcept;
61  Shader& operator=(const Shader&) = delete;
62  Shader& operator=(Shader&& shader) noexcept;
63  ~Shader();
64 
65  void Bind() const;
66  void Unbind() const;
67  void InvalidateUniformCache();
68  BindableId GetNativeHandle() const;
69  void Load(const MxString& vertex, const MxString& fragment);
70  void Load(const MxString& vertex, const MxString& geometry, const MxString& fragment);
71  void LoadFromString(const MxString& vertex, const MxString& fragment);
72  void LoadFromString(const MxString& vertex, const MxString& geometry, const MxString& fragment);
73  void SetUniformFloat(const MxString& name, float f) const;
74  void SetUniformVec2(const MxString& name, const Vector2& vec) const;
75  void SetUniformVec3(const MxString& name, const Vector3& vec) const;
76  void SetUniformVec4(const MxString& name, const Vector4& vec) const;
77  void SetUniformMat4(const MxString& name, const Matrix4x4& matrix) const;
78  void SetUniformMat3(const MxString& name, const Matrix3x3& matrix) const;
79  void SetUniformInt(const MxString& name, int i) const;
80  };
81 }
Definition: Shader.h:38
Definition: Application.cpp:49