/
shaderBuffer.cpp
63 lines (45 loc) · 1.41 KB
/
shaderBuffer.cpp
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
#include "shader.h"
#include "shaderBuffer.h"
#include <memory.h>
namespace SCore {
shaderBuffer::shaderBuffer( const std::string &name, unsigned int programId )
{
target = GL_SHADER_STORAGE_BUFFER;
size = 0;
unsigned int idx = glGetProgramResourceIndex( programId, GL_BUFFER_VARIABLE, name.c_str() );
int props[] = { GL_BUFFER_DATA_SIZE };
int paramLen = 4;
int params;
glGetProgramResourceiv( programId, GL_BUFFER_VARIABLE, idx, 1, (const GLenum *) props, 4, ¶mLen, ¶ms );
glGenBuffersARB( 1, &bufferId );
glBindBufferARB( target, bufferId );
glBufferDataARB( target, params, 0, GL_DYNAMIC_COPY );
glBindBufferARB( target, 0 );
assert( glGetError() == GL_NO_ERROR );
}
shaderBuffer::shaderBuffer( const size_t sz )
{
target = GL_SHADER_STORAGE_BUFFER;
size = sz;
glGenBuffersARB( 1, &bufferId );
glBindBufferARB( target, bufferId );
glBufferDataARB( target, sz, 0, GL_DYNAMIC_COPY );
glBindBufferARB( target, 0 );
assert( glGetError() == GL_NO_ERROR );
}
void* shaderBuffer::getContent()
{
void* result = 0;
glBindBufferARB( target, bufferId );
void* source = glMapBufferARB( target, GL_READ_ONLY_ARB );
if (source)
{
result = malloc( size );
if (result) memcpy( result, source, size );
else result = 0;
glUnmapBufferARB( target );
}
glBindBufferARB( target, 0 );
return result;
}
}