OpenGL计算着色器多维工作组获取正确的索引

#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)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值