#include "vgl.h"
#include "vmath.h"
#include "LoadShaders.h"
#include "vbm.h"
#include <iostream>
using namespace std;
GLuint vbo[2];
GLuint ProgCompure;
void init()
{
glGenBuffers(2, vbo);
for (int i = 0; i < 2; i++)
{
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo[i]);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLint) * 64, NULL, GL_DYNAMIC_COPY);
}
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo[0]);
GLint *tempP = (GLint *)glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY);
for (int i = 0; i < 64; ++i)
{
*(tempP++) = i;
}
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
ShaderInfo csi[] = {
{ GL_COMPUTE_SHADER, "Computer.comp" },
{GL_NONE, NULL}
};
ProgCompure = LoadShaders(csi);
}
void display()
{
glUseProgram(ProgCompure);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, vbo[0]);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbo[1]);
glDispatchCompute(2, 2, 2);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vbo[1]);
GLint *tempP = (GLint *)glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY);
for (int i = 0; i < 8; ++i){
for (int j = 0; j < 8; ++j)
{
cout << *(tempP++) << " ";
}
cout << endl;
}
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
}
void idle()
{
static unsigned int Time = GetTickCount();
unsigned int timeNow = GetTickCount();
if (timeNow - Time > 15)
{
Time = timeNow;
glutPostRedisplay();
}
else
{
Sleep(30);
}
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_STENCIL);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitContextVersion(4, 3);
glutInitWindowSize(400, 400);
glutCreateWindow("Test OpenGL");
if (glewInit() != 0)
{
cout << "Can not initilizate GLEW" << endl;
return 1;
}
init();
glutIdleFunc(idle);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
#version 430 core
layout(local_size_x = 1,local_size_y = 4,local_size_z = 2) in;
layout(binding = 0) buffer inBuf
{
int datas[];
}In;
layout(binding = 1) buffer outBuf
{
int datas[];
}Out;
void main()
{
//获取本地工作组在全局工作组中的索引
uint workGroupIndex = gl_WorkGroupID.z * gl_NumWorkGroups.x * gl_NumWorkGroups.y + gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x;
//一个本地工作组的运行次数
uint localWorkNum = gl_WorkGroupSize.x * gl_WorkGroupSize.y * gl_WorkGroupSize.z;
//当次运行的索引
uint index = workGroupIndex * localWorkNum + gl_LocalInvocationIndex;
Out.datas[index] = In.datas[index] * In.datas[index];
}
只做了64个数的平方,现在只要全局工作组和局部工作组六个分量的积是64就能得出正确的结果了,比如(2,2,2),(2,2,2)或者(4,4,1),(1,4,1)。