Ejemplo n.º 1
0
GL::ComputeShader::ComputeShader(const string& shader_name)
    : Shader()
{
    _name = shader_name;


    if (config.verbosity_level() >= 2) {
        cout << boost::format("Compiling compute-shader \"%1%\"...") % _name << endl;
    }
    
    ShaderObject compute_shader(shader_name, "", GL_COMPUTE_SHADER);

    if (compute_shader.invalid()) {
        cout << boost::format("Compiling compute-shader \"%1%\" FAILED") % _name << endl;
        exit(1);
        return;
    }
    
    _program = glCreateProgram();
    compute_shader.attach_to(_program);

    link();    
}
Ejemplo n.º 2
0
void ShaderEffect_Impl::create_shaders(GraphicContext &gc, const ShaderEffectDescription_Impl *description)
{
	std::string vertex_shader_code = add_defines(gc, description->vertex_shader_code, description);
	std::string fragment_shader_code = add_defines(gc, description->fragment_shader_code, description);
	std::string compute_shader_code = add_defines(gc, description->compute_shader_code, description);

	if (!vertex_shader_code.empty()) 
	{
		ShaderObject vertex_shader(gc, shadertype_vertex, vertex_shader_code);
		if(!vertex_shader.compile())
			throw Exception(string_format("Unable to compile vertex shader: %1", vertex_shader.get_info_log()));
		program.attach(vertex_shader);
	}

	if (!fragment_shader_code.empty()) 
	{
		ShaderObject fragment_shader(gc, shadertype_fragment, fragment_shader_code);
		if(!fragment_shader.compile())
			throw Exception(string_format("Unable to compile fragment shader: %1", fragment_shader.get_info_log()));
		program.attach(fragment_shader);
	}

	if (!compute_shader_code.empty()) 
	{
		ShaderObject compute_shader(gc, shadertype_compute, compute_shader_code);
		if(!compute_shader.compile())
			throw Exception(string_format("Unable to compile compute shader: %1", compute_shader.get_info_log()));
		program.attach(compute_shader);
	}

	int index = 0;
	for(const auto & elem : description->attributes)
	{
		program.bind_attribute_location(index++, elem.first);
	}

	index = 0;
	for(const auto & elem : description->frag_data)
	{
		program.bind_frag_data_location(index++, elem.first);
	}

	if (!program.link())
		throw Exception(string_format("Link failed: %1", program.get_info_log()));

	index = 0;
	for(auto it = description->uniform_buffers.begin(); it != description->uniform_buffers.end(); ++it, index++)
	{
		program.set_uniform_buffer_index(it->first, index);
		uniform_bindings[index] = it->second;
	}

	index = 0;
	for(auto it = description->textures.begin(); it != description->textures.end(); ++it, index++)
	{
		program.set_uniform1i(it->first, index);
		texture_bindings[index] = it->second;
	}

	index = 0;
	for(auto it = description->images.begin(); it != description->images.end(); ++it, index++)
	{
		program.set_uniform1i(it->first, index);
		image_bindings[index] = it->second;
	}

	index = 0;
	for(auto it = description->storage_buffers.begin(); it != description->storage_buffers.end(); ++it, index++)
	{
		program.set_uniform1i(it->first, index);
		storage_bindings[index] = it->second;
	}

}
Ejemplo n.º 3
0
int main(int argc, const char *argv[])
{
    if(!glfwInit()) {
        std::cerr << "Failed to initialize GLFW3." << std::endl;
        return -1;
    }

    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    GLFWwindow *window = glfwCreateWindow(640, 480, "Compute Shader Test", NULL, NULL);
    if(!window) {
        std::cerr << "Failed to create a GLFW window." << std::endl;
        return -1;
    }

    glfwMakeContextCurrent(window);

    glewExperimental = 1;
    glewInit();

    std::cout << "Vendor:" << glGetString(GL_VENDOR) << std::endl;
    std::cout << "Renderer:" << glGetString(GL_RENDERER) << std::endl;
    std::cout << "Version:" << glGetString(GL_VERSION) << std::endl;
    std::cout << "Shader Language Version:" << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;

    if(GLEW_ARB_compute_shader) {
        std::cerr << "ARB_compute_shader extension supported." << std::endl;
    } else {
        std::cerr << "ARB_compute_shader extension not supported." << std::endl;
        return -1;
    }
    if(GLEW_ARB_shader_image_load_store) {
        std::cerr << "ARB_shader_image_load_store extension supported." << std::endl;
    } else {
        std::cerr << "ARB_shader_image_load_store extension not supported." << std::endl;
        return -1;
    }

    glEnable(GL_TEXTURE_2D);

    GLuint vertex_array;
    glGenVertexArrays(1, &vertex_array);
    glBindVertexArray(vertex_array);

    std::cout << "X = " << ComputeShader::get_max_localsize_x() << std::endl;
    std::cout << "Y = " << ComputeShader::get_max_localsize_y() << std::endl;
    std::cout << "Z = " << ComputeShader::get_max_localsize_z() << std::endl;

    {
        ComputeShader compute_shader("shader.comp");
        Shader shader("shader.vert", "shader.frag");
        //Texture input("opengl_logo.bmp");
        Texture output(512, 512);

        while(!glfwWindowShouldClose(window)) {
            //glClearColor(1, 1, 1, 1);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            compute_shader.use();
            glUniform1i(0, 0);
            //input.bind_image(0, GL_READ_ONLY);
            output.bind_image(0, GL_WRITE_ONLY);
            glDispatchCompute(32, 32, 1);
            glMemoryBarrier(GL_TEXTURE_FETCH_BARRIER_BIT);
            shader.use();
            glUniform1i(0, 0);
            output.render();
            glfwSwapBuffers(window);
            glfwPollEvents();
        }
    }

    glfwTerminate();

    return 0;
}