summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/render/bgfx/uniform.cpp
blob: a0ee1d174da5f544946eda09d7f27eec1a47ccc9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "uniform.h"

bgfx_uniform::bgfx_uniform(std::string name, bgfx::UniformType::Enum type)
    : m_name(name)
    , m_type(type)
{
    m_handle = bgfx::createUniform(m_name.c_str(), m_type);
    m_data_size = get_size_for_type(type);
    if (m_data_size > 0)
    {
        m_data = new uint8_t[m_data_size];
    }
}

bgfx_uniform::~bgfx_uniform()
{
    bgfx::destroyUniform(m_handle);
    delete [] m_data;
}

void bgfx_uniform::upload()
{
    bgfx::setUniform(m_handle, m_data);
}

bgfx_uniform* bgfx_uniform::set(float* value)
{
    return set(value, get_size_for_type(bgfx::UniformType::Vec4));
}

bgfx_uniform* bgfx_uniform::set_int(int value)
{
    return set(&value, get_size_for_type(bgfx::UniformType::Int1));
}

bgfx_uniform* bgfx_uniform::set_mat3(float* value)
{
    return set(value, get_size_for_type(bgfx::UniformType::Mat3));
}

bgfx_uniform* bgfx_uniform::set_mat4(float* value)
{
    return set(value, get_size_for_type(bgfx::UniformType::Mat4));
}

bgfx_uniform* bgfx_uniform::set(void* data, size_t size)
{
    int min_size = (size < m_data_size) ? size : m_data_size;
    memcpy(m_data, data, min_size);
    return this;
}

size_t bgfx_uniform::get_size_for_type(bgfx::UniformType::Enum type)
{
    switch (type)
    {
        case bgfx::UniformType::Vec4:
            return sizeof(float) * 4;

        case bgfx::UniformType::Int1:
            return sizeof(int);

        case bgfx::UniformType::Mat3:
            return sizeof(float) * 3 * 3;

        case bgfx::UniformType::Mat4:
            return sizeof(float) * 4 * 4;

        default:
            return 0;
    }
}