summaryrefslogtreecommitdiff
path: root/Runtime/GfxDevice/GLESCommon.h
blob: 9d1ffee777692de70d71f2a75a498c9847e3b023 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef GLES_COMMON_H
#define GLES_COMMON_H

// internal header
// common gles 1.x/2.x stuff
// also some common android/ios stuff

#include <ctype.h>

inline int GLES_EstimateVRAM_MB(int total_mem_mb)
{
#if UNITY_IPHONE
	// For iOS we estimate 1/4 of POT(total_mem)
	const int physical_mem_mb = 1 << (32 - __builtin_clz(total_mem_mb - 1));
	return physical_mem_mb >> 2;
#elif UNITY_ANDROID || UNITY_BB10 || UNITY_TIZEN
	// There is (afawk) no way to determine the size of the VRAM
    // So we try to create ('fake') a value that is somewhere along the lines of possibly correct.
    // We make two assumption here:
    // 1) The VRAM is cut-out from the physical memory range and 2) VRAM size is never bigger than
    // a 1/4  of the physical memory size, but still bigger than a 1/16 of the total memory size.
    // Even if not correct it should give an indication of how much memory is available for GPU resources.
    const int physical_mem_mb = 1 << (32 - __builtin_clz(total_mem_mb - 1));
    return std::max(  std::min(physical_mem_mb - total_mem_mb, (physical_mem_mb >> 2)),  (total_mem_mb >> 4) );
#else
    return 256;
#endif
}

namespace systeminfo { int GetPhysicalMemoryMB(); }
struct GraphicsCaps;

inline void GLES_InitCommonCaps(GraphicsCaps* caps)
{
    {
        caps->vendorID   = 0;
        caps->rendererID = 0;

        #define GRAB_STRING(target, name)                           \
        do {                                                        \
            const char* _tmp_str = (const char*)glGetString(name);  \
            GLESAssert();                                           \
            caps->target = _tmp_str ? _tmp_str : "<unknown>";       \
        } while(0)                                                  \

        GRAB_STRING(rendererString, GL_RENDERER);
        GRAB_STRING(vendorString, GL_VENDOR);
        GRAB_STRING(driverVersionString, GL_VERSION);

        #undef GRAB_STRING

        // Distill
        //    driverVersionString = "OpenGL ES 2.0 build 1.8@905891"
        // into
        //    driverLibraryString = "build 1.8@905891"
        //
        // See http://www.khronos.org/opengles/sdk/1.1/docs/man/glGetString.xml
        //
        const char OpenGL[] = "OpenGL";
        const char ES[] = "ES";
        const char* gl_version = caps->driverVersionString.c_str();
        for (int i = 0 ; i < 3; ++i, ++gl_version)
        {
            if(    (i == 0 && strncmp(gl_version, OpenGL, sizeof(OpenGL)-1))
                || (i == 1 && strncmp(gl_version, ES, sizeof(ES)-1))
                || (i == 2 && !isdigit(*gl_version))
              )
            {
                gl_version = NULL;
                break;
            }
            if ( !(gl_version = strstr(gl_version, " ")) )
                break;
        }

        if (gl_version)
            caps->driverLibraryString = gl_version;
        else
            caps->driverLibraryString = "n/a";

        caps->fixedVersionString  = caps->driverVersionString;

        const char* ext = (const char*)glGetString(GL_EXTENSIONS);
        GLESAssert();

        ::printf_console ("Renderer: %s\n", caps->rendererString.c_str());
        ::printf_console ("Vendor:   %s\n", caps->vendorString.c_str());
        ::printf_console ("Version:  %s\n", caps->driverVersionString.c_str());

        if(ext) DebugTextLineByLine(ext);
        else    ::printf_console("glGetString(GL_EXTENSIONS) - failure");
    }

    {
    #if UNITY_ANDROID || UNITY_IPHONE || UNITY_BB10 || UNITY_TIZEN
        caps->videoMemoryMB = GLES_EstimateVRAM_MB(systeminfo::GetPhysicalMemoryMB());
    #else
        caps->videoMemoryMB = 256; // awesome estimation
    #endif
    }
}


#endif // GLES_COMMON_H