forked from zmich/4K_displaywall_bench
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.h
114 lines (92 loc) · 2.79 KB
/
util.h
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
106
107
108
109
110
111
112
113
114
#ifndef __Util__proto__
#define __Util__proto__
#include <fstream>
#include <sstream>
#include <vector>
#include <cstring>
#define GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX 0x9049
#define GPU_MEMORY_INFO_EVICTED_MEMORY_NVX 0x904B
#ifndef GL_TEXTURE_FREE_MEMORY_ATI
#define GL_TEXTURE_FREE_MEMORY_ATI 0x87FC
#endif
namespace proto {
class Util {
public:
static bool compileShader (GLuint &shader, GLenum type,
const char *src)
{ shader = glCreateShader (type);
glShaderSource (shader, 1, &src, NULL);
glCompileShader (shader);
#if defined(DEBUG)
GLint logLength;
glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
std::vector<GLchar> plog(logLength);
glGetShaderInfoLog (shader, logLength, &logLength, &plog[0]);
std::string strLog(&plog[0], &plog[0] + logLength);
std::cout << strLog << std::endl;
}
#endif
GLint status;
glGetShaderiv (shader, GL_COMPILE_STATUS, &status);
if (status == 0) {
glDeleteShader (shader);
return false;
}
return true;
}
static bool linkProgram (GLuint &program)
{ glLinkProgram (program);
#if defined(DEBUG)
GLint logLength;
glGetProgramiv (program, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
std::vector<GLchar> plog(logLength);
glGetProgramInfoLog (program, logLength, &logLength, &plog[0]);
std::string strLog(&plog[0], &plog[0] + logLength);
std::cout << strLog << std::endl;
}
#endif
GLint status;
glGetProgramiv (program, GL_LINK_STATUS, &status);
if (status == 0) {
return false;
}
return true;
}
static std::string loadShaderSourceFromFile (const char *filepath)
{ std::ifstream in (filepath);
std::stringstream ss;
ss << in.rdbuf ();
return ss.str ();
}
static void query_gpu_mem_usage_nvidia()
{
GLint minfo;
glGetIntegerv(GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &minfo);
GLint evicted;
glGetIntegerv(GPU_MEMORY_INFO_EVICTED_MEMORY_NVX, &evicted);
printf("GL nvidia total available memory %d MB (evicted memory %d) \n", minfo/1024, evicted/1024);
}
static void query_gpu_mem_usage_amd()
{
GLint minfo[4];
glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI, minfo);
printf("GL amd total free memory in pool %d MB\n", minfo[0]/1024);
}
static void query_memory()
{
std::string vendor_string ( reinterpret_cast<const char *>(glGetString(GL_VENDOR)));
std::cout << vendor_string << std::endl;
if (vendor_string . find ("nvidia") != std::string::npos ||
vendor_string . find ("NVIDIA") != std::string::npos)
query_gpu_mem_usage_nvidia ();
else if (vendor_string . find ("ati") != std::string::npos ||
vendor_string . find ("amd") != std::string::npos)
query_gpu_mem_usage_amd ();
else
printf("unsupported graphics card vendor\n");
}
};
}
#endif // defined(__Scene__proto__)