1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FRAMEWORKS_ANIMATION_SERVER_SERVER_INCLUDE_SHADER_H 17 #define FRAMEWORKS_ANIMATION_SERVER_SERVER_INCLUDE_SHADER_H 18 19 #include <cstdint> 20 #include <unordered_map> 21 #include <string> 22 23 #include <matrix.h> 24 25 class Shader { 26 public: 27 Shader(const std::string& vertexSource, const std::string& fragmentSource); 28 ~Shader(); 29 30 bool Available(); 31 32 void Bind() const; 33 void Unbind() const; 34 35 void SetUniform1i(const std::string& name, int32_t v); 36 void SetUniform1f(const std::string& name, float v); 37 void SetUniform4f(const std::string& name, float v0, float v1, float v2, float v3); 38 void SetUniformMat4f(const std::string& name, const Matrix<float>& matrix); 39 40 int32_t GetAttribLocation(const std::string& name); 41 42 private: 43 int32_t GetUniformLocation(const std::string& name); 44 uint32_t CompileShader(uint32_t type, const std::string& source); 45 uint32_t CreateShader(const std::string& vertexShader, const std::string& fragmentShader); 46 47 uint32_t rendererID_ = 0; 48 std::unordered_map<std::string, int32_t> uniformLocationCache_; 49 }; 50 51 #endif // FRAMEWORKS_ANIMATION_SERVER_SERVER_INCLUDE_SHADER_H 52