AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
Program.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12//
   13// Created by alex2772 on 25.07.2018.
   14//
   15
   16#pragma once
   17
   18#include <AUI/Common/AString.h>
   19#include <cstdint>
   20#include <glm/glm.hpp>
   21#include <optional>
   22#include <variant>
   23#include <AUI/Common/AMap.h>
   24#include <AUI/Common/AVector.h>
   25#include <AUI/Views.h>
   26#include <AUI/Util/AArrayView.h>
   27
   28class AString;
   29
   30namespace gl {
   31class API_AUI_VIEWS Program: public aui::noncopyable {
   32public:
   33    class API_AUI_VIEWS Uniform {
   34    private:
   35        const char* mUniformString;
   36        unsigned mId = -1;
   37
   38        static unsigned next();
   39
   40    public:
   41        explicit Uniform(const char* uniformString) : mUniformString(uniformString), mId(next()) {}
   42
   43        const char* getUniformName() const { return mUniformString; }
   44
   45        unsigned getId() const { return mId; }
   46    };
   47
   48    Program();
   49    explicit Program(Program&& program) noexcept {
   50        operator=(std::move(program));
   51    }
   52    void
   53    load(const AString& vertex, const AString& fragment, const AVector<AString>& attribs = {},
   54         const AString& version = {});
   55    void loadVertexShader(const AString& vertex, bool raw);
   56    void loadFragmentShader(const AString& fragment, bool raw);
   57    void loadRaw(const AString& vertex, const AString& fragment);
   58    void compile();
   59    void bindAttribute(uint32_t index, const AString& name);
   60    void use() const;
   61    ~Program();
   62    Program(const Program&) = delete;
   63
   64    uint32_t handle() const { return mProgram; }
   65
   66    int32_t getLocation(const Uniform& uniform) const;
   67
   68    void set(const gl::Program::Uniform& uniform, int value) const;
   69    void set(const gl::Program::Uniform& uniform, float value) const;
   70    void set(const gl::Program::Uniform& uniform, double value) const;
   71    void set(const gl::Program::Uniform& uniform, glm::mat4 value) const;
   72    void set(const gl::Program::Uniform& uniform, glm::mat3 value) const;
   73    void set(const gl::Program::Uniform& uniform, glm::dmat4 value) const;
   74    void set(const gl::Program::Uniform& uniform, glm::vec2 value) const;
   75    void set(const gl::Program::Uniform& uniform, glm::vec3 value) const;
   76    void set(const gl::Program::Uniform& uniform, glm::vec4 value) const;
   77
   78    void setArray(const gl::Program::Uniform& uniform, AArrayView<float> value) const;
   79
   80    static gl::Program*& currentShader() {
   81        static gl::Program* c;
   82        return c;
   83    }
   84
   85    Program& operator=(Program&& program) noexcept {
   86        if (this == &program) {
   87            return *this;
   88        }
   89        mProgram = std::exchange(program.mProgram, 0);
   90        mUniforms = std::exchange(program.mUniforms, {});
   91        mVertex = std::exchange(program.mVertex, 0);
   92        mFragment = std::exchange(program.mFragment, 0);
   93
   94        return *this;
   95    }
   96
   97private:
   98    uint32_t mProgram;
   99    uint32_t mVertex = 0;
  100    uint32_t mFragment = 0;
  101
  102    struct UniformCache {
  103        int32_t id = UniformState::UNINITIALIZED;
  104        using Value = std::variant<
  105            std::nullopt_t, int, float, double, glm::vec2, glm::vec3, glm::vec4, glm::mat3, glm::mat4, glm::dmat4>;
  106        Value lastValue = std::nullopt;
  107    };
  108    mutable std::array<UniformCache, 64> mUniforms;
  109
  110    class UniformState {
  111    public:
  112        enum Value { UNINITIALIZED = -2, DOES_NOT_EXIST = -1 };
  113    };
  114
  115    template <typename T>
  116    inline bool sameAsCache(const gl::Program::Uniform& uniform, T value) const {
  117        if (auto p = std::get_if<T>(&mUniforms[uniform.getId()].lastValue)) {
  118            return *p == value;
  119        }
  120        return false;
  121    }
  122    uint32_t load(const AString& code, uint32_t type, bool raw) {
  123        return load(code.toStdString(), type, raw);
  124    }
  125    uint32_t load(std::string code, uint32_t type, bool raw);
  126};
  127}   // namespace gl
Represents a Unicode character string.
Definition AString.h:38
std::string toStdString() const noexcept
A std::vector with AUI extensions.
Definition AVector.h:39
class_of c
Selects views that are of the specified classes.
Definition class_of.h:84
Forbids copy of your class.
Definition values.h:45