当前位置: 首页 > news >正文

淘宝客网站模块网站托管

淘宝客网站模块,网站托管,在线直播网站建设,绵阳网站搜索排名一、简介 本文介绍了如何使用 OpenGL 中的 compute shader 进行矩阵相乘的并行运算。代码目标是,输入两个大小为 10*10 的矩阵 A 和 B,计算 A*B 的结果并存储到矩阵 C 中。 二、代码 0. 代码逻辑 1. 初始化 glfw, glad, 窗口 2. 初始化 compute shad…

一、简介

本文介绍了如何使用 OpenGL 中的 compute shader 进行矩阵相乘的并行运算。代码目标是,输入两个大小为 10*10 的矩阵 A 和 B,计算 A*B 的结果并存储到矩阵 C 中。

二、代码

0. 代码逻辑

1. 初始化 glfw, glad, 窗口
2. 初始化 compute shader
3. 准备输入数据
4. 运行 compute shader
5. 读取结果并打印
6. 释放资源

1. main.cpp

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "ComputeShader.hpp"#include <cstdint>
#include <iostream>
#include <iostream>// 用于处理窗口大小改变的回调函数
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void window_close_callback(GLFWwindow *window);// 用于处理用户输入的函数
void processInput(GLFWwindow *window);// 指定窗口默认width和height像素大小
unsigned int SCR_WIDTH = 800;
unsigned int SCR_HEIGHT = 600;/************************************/int main()
{/****** 1. 初始化 glfw, glad, 窗口 *******/// glfw 初始化 + 配置 glfw 参数glfwInit();glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);// glfw 生成窗口GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);if (window == NULL){// 检查是否成功生成窗口,如果没有成功打印出错信息并且退出std::cout << "Failed to create GLFW window" << std::endl;glfwTerminate();return -1;}// 设置窗口window的上下文glfwMakeContextCurrent(window);// 配置window变化时的回调函数glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);// 设置窗口关闭回调glfwSetWindowCloseCallback(window, window_close_callback);// 使用 glad 加载 OpenGL 中的各种函数if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){std::cout << "Failed to initialize GLAD" << std::endl;return -1;}/************************************//****** 2. 初始化 compute shader ******/ComputeShader computeShader("../resources/Compute.comp");/************************************//****** 3. 准备输入数据 ******/// 输入矩阵 Afloat A[100];for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){A[i * 10 + j] = 1.0f * i;}}// 输入矩阵 Bfloat B[100];for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){B[i * 10 + j] = 1.0f * i;}}// 输出矩阵 Cfloat C[100];GLuint SSBO_A, SSBO_B, SSBO_C;glGenBuffers(1, &SSBO_A);glGenBuffers(1, &SSBO_B);glGenBuffers(1, &SSBO_C);glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO_A);glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(A), A, GL_STATIC_READ);glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, SSBO_A);glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO_B);glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(B), B, GL_STATIC_READ);glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, SSBO_B);glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO_C);glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(C), C, GL_DYNAMIC_DRAW);glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, SSBO_C);/************************************//****** 4. 运行 compute shader ******/// 运行 compute shader, 分为 10*10*1 个 workgroup, 每个 workgroup 计算 C 矩阵中的一个元素值computeShader.use();glDispatchCompute((unsigned int)10, (unsigned int)10, 1);glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);/************************************//****** 5. 读取结果并打印 ******/glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO_C);glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(C), C);for (int row = 0; row < 10; ++row){for (int col = 0; col < 10; ++col){printf("%0.3f ", C[row * 10 + col]);}printf("\n");}/************************************//****** 6.释放资源 ******/// glfw 释放 glfw使用的所有资源glfwTerminate();/************************************/return 0;
}// 用于处理用户输入的函数
void processInput(GLFWwindow *window)
{// 当按下 Esc 按键时调用 glfwSetWindowShouldClose() 函数,关闭窗口if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS){glfwSetWindowShouldClose(window, true);}
}// 在使用 OpenGL 和 GLFW 库时,处理窗口大小改变的回调函数
// 当窗口大小发生变化时,确保 OpenGL 渲染的内容能够适应新的窗口大小,避免图像被拉伸、压缩或出现其他比例失真的问题
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{SCR_WIDTH = width;SCR_HEIGHT = height;glViewport(0, 0, width, height);
}
void window_close_callback(GLFWwindow *window)
{// 这里可以做一些额外的清理工作// 例如释放资源、记录日志等std::cout << "Window is closing..." << std::endl;
}

2. ComputeShader 类

#ifndef COMPUTESHADER_H
#define COMPUTESHADER_H#include <glad/glad.h>
#include <glm/glm.hpp>#include <string>
#include <fstream>
#include <sstream>
#include <iostream>class ComputeShader
{public:unsigned int ID;// constructor generates the shader on the fly// ------------------------------------------------------------------------ComputeShader() {};ComputeShader(const char *computePath){// 1. retrieve the vertex/fragment source code from filePathstd::string computeCode;std::ifstream cShaderFile;// ensure ifstream objects can throw exceptions:cShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);try{// open filescShaderFile.open(computePath);std::stringstream cShaderStream;// read file's buffer contents into streamscShaderStream << cShaderFile.rdbuf();// close file handlerscShaderFile.close();// convert stream into stringcomputeCode = cShaderStream.str();}catch (std::ifstream::failure &e){std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() << std::endl;}const char *cShaderCode = computeCode.c_str();// 2. compile shadersunsigned int compute;// compute shadercompute = glCreateShader(GL_COMPUTE_SHADER);glShaderSource(compute, 1, &cShaderCode, NULL);glCompileShader(compute);checkCompileErrors(compute, "COMPUTE");// shader ProgramID = glCreateProgram();glAttachShader(ID, compute);glLinkProgram(ID);checkCompileErrors(ID, "PROGRAM");// delete the shaders as they're linked into our program now and no longer necessaryglDeleteShader(compute);}// activate the shader// ------------------------------------------------------------------------void use() const{glUseProgram(ID);}// ------------------------------------------------------------------------void setInt(const std::string &name, int value) const{glUniform1i(glGetUniformLocation(ID, name.c_str()), value);}private:// utility function for checking shader compilation/linking errors.// ------------------------------------------------------------------------void checkCompileErrors(GLuint shader, std::string type){GLint success;GLchar infoLog[1024];if (type != "PROGRAM"){glGetShaderiv(shader, GL_COMPILE_STATUS, &success);if (!success){glGetShaderInfoLog(shader, 1024, NULL, infoLog);std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n"<< infoLog << "\n -- --------------------------------------------------- -- " << std::endl;}}else{glGetProgramiv(shader, GL_LINK_STATUS, &success);if (!success){glGetProgramInfoLog(shader, 1024, NULL, infoLog);std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n"<< infoLog << "\n -- --------------------------------------------------- -- " << std::endl;}}}
};
#endif

3. compute shader (Compute.comp)

#version 430layout(std430, binding = 0) buffer inputMatrixA { float A[]; };
layout(std430, binding = 1) buffer inputMatrixB { float B[]; };
layout(std430, binding = 2) buffer OnputData { float C[]; };layout(local_size_x = 1,local_size_y = 1) in; // 每个 workgroup item 计算 C 的一个元素void main() {//   获取当前 workgroup item 的全局位置uint row = gl_GlobalInvocationID.x;uint col = gl_GlobalInvocationID.y;// 确保不会越界if (row >= 10 || col >= 10) {return;}// 从矩阵 A 和矩阵 B 中读取数据float valueA = 0.0f;float valueB = 0.0f;// 计算矩阵 C 中对应的元素float result = 0.0;for (int k = 0; k < 10; k++) {valueA = A[row * 10 + k];valueB = B[k * 10 + col];result += valueA * valueB; // 矩阵乘法}C[row * 10 + col] = result;
}

4. 运行结果

0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 
45.000 45.000 45.000 45.000 45.000 45.000 45.000 45.000 45.000 45.000 
90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 90.000 
135.000 135.000 135.000 135.000 135.000 135.000 135.000 135.000 135.000 135.000 
180.000 180.000 180.000 180.000 180.000 180.000 180.000 180.000 180.000 180.000 
225.000 225.000 225.000 225.000 225.000 225.000 225.000 225.000 225.000 225.000 
270.000 270.000 270.000 270.000 270.000 270.000 270.000 270.000 270.000 270.000 
315.000 315.000 315.000 315.000 315.000 315.000 315.000 315.000 315.000 315.000 
360.000 360.000 360.000 360.000 360.000 360.000 360.000 360.000 360.000 360.000 
405.000 405.000 405.000 405.000 405.000 405.000 405.000 405.000 405.000 405.000

三、参考

[1]LearnOpenGL-Guest Articles-2022-Compute Shaders


文章转载自:
http://sparganum.pwrb.cn
http://gertcha.pwrb.cn
http://cottontail.pwrb.cn
http://adlerian.pwrb.cn
http://deathtrap.pwrb.cn
http://polluted.pwrb.cn
http://debacle.pwrb.cn
http://unplausible.pwrb.cn
http://chutzpa.pwrb.cn
http://cyc.pwrb.cn
http://stratocirrus.pwrb.cn
http://nonvocoid.pwrb.cn
http://schul.pwrb.cn
http://maxillofacial.pwrb.cn
http://alexandrine.pwrb.cn
http://dispiteous.pwrb.cn
http://orthoferrite.pwrb.cn
http://centrifuge.pwrb.cn
http://combatively.pwrb.cn
http://nelly.pwrb.cn
http://thermoduric.pwrb.cn
http://stingaree.pwrb.cn
http://promulgator.pwrb.cn
http://kernicterus.pwrb.cn
http://periwig.pwrb.cn
http://vacillatingly.pwrb.cn
http://garboil.pwrb.cn
http://exclamative.pwrb.cn
http://romping.pwrb.cn
http://circumvallate.pwrb.cn
http://titanate.pwrb.cn
http://reverberator.pwrb.cn
http://ripple.pwrb.cn
http://swingle.pwrb.cn
http://cenobian.pwrb.cn
http://heptanone.pwrb.cn
http://honcho.pwrb.cn
http://convincingly.pwrb.cn
http://repent.pwrb.cn
http://lucerne.pwrb.cn
http://vallation.pwrb.cn
http://brahmaputra.pwrb.cn
http://dimorphotheca.pwrb.cn
http://urgent.pwrb.cn
http://interline.pwrb.cn
http://creatress.pwrb.cn
http://praecipitatio.pwrb.cn
http://zygotene.pwrb.cn
http://tenseness.pwrb.cn
http://leukemia.pwrb.cn
http://wade.pwrb.cn
http://prominence.pwrb.cn
http://pancreatitis.pwrb.cn
http://sunshiny.pwrb.cn
http://pocketknife.pwrb.cn
http://larnax.pwrb.cn
http://okenite.pwrb.cn
http://siangtan.pwrb.cn
http://caseophile.pwrb.cn
http://scanty.pwrb.cn
http://hydroxide.pwrb.cn
http://zymoplastic.pwrb.cn
http://sarvodaya.pwrb.cn
http://oomph.pwrb.cn
http://familiarize.pwrb.cn
http://fivepence.pwrb.cn
http://spanning.pwrb.cn
http://weddell.pwrb.cn
http://zoea.pwrb.cn
http://freeway.pwrb.cn
http://bypath.pwrb.cn
http://balkan.pwrb.cn
http://hemolyze.pwrb.cn
http://hutchie.pwrb.cn
http://algebraic.pwrb.cn
http://legume.pwrb.cn
http://anteversion.pwrb.cn
http://traffic.pwrb.cn
http://allies.pwrb.cn
http://tormenting.pwrb.cn
http://allmains.pwrb.cn
http://rallicart.pwrb.cn
http://rotten.pwrb.cn
http://neuristor.pwrb.cn
http://diathermize.pwrb.cn
http://cpo.pwrb.cn
http://hungered.pwrb.cn
http://perishing.pwrb.cn
http://andirons.pwrb.cn
http://stapler.pwrb.cn
http://unslumbering.pwrb.cn
http://millifarad.pwrb.cn
http://antheap.pwrb.cn
http://temperance.pwrb.cn
http://eo.pwrb.cn
http://toril.pwrb.cn
http://mainstreet.pwrb.cn
http://mortifying.pwrb.cn
http://consecution.pwrb.cn
http://nonmoral.pwrb.cn
http://www.dt0577.cn/news/101413.html

相关文章:

  • 网站防劫持怎么做深圳网站建设的公司
  • 东莞定制网站建设河北网站seo
  • 网站充值怎么做的关键词排名优化工具
  • 长沙做网站公怎么办网站平台
  • 沈阳做网站的地方竞价培训课程
  • 网站开发排行作品提示优化要删吗
  • 建设评标专家在哪个网站网址创建
  • 中小企业网站提供了什么360优化大师官方最新
  • 织梦做中英文网站详细步骤windows10优化工具
  • 男女做爰高清免费视频网站网络软文推广网站
  • 深圳做app网站设计百度 seo排名查询
  • 哪些网站做问卷可以赚钱seo公司上海牛巨微
  • 出售家教网站模板网上哪里可以免费打广告
  • 怎样做网站 告她出轨湖南长沙seo教育
  • 微网站建设及微信推广方案百度推广竞价技巧
  • 做设计找参考的设计网站有那些临沂seo排名外包
  • 可以做软件的网站有哪些内容口碑营销推广
  • 国外做饮用来源的网站网络推广的方法
  • 免费网站建设设计制作公司太原搜索引擎优化招聘信息
  • 邯郸模板建站教程网络推广的基本方法有哪些
  • 嘉定南翔网站建设西安网站制作推广
  • 现在海外做的比较好一点的网站电商平台引流推广
  • 网站 备案 中国 名字吗seo自动发布外链工具
  • 移动互联网50+互联网单词优化和整站优化
  • 武汉哪家做网站今天今日新闻头条最新消息
  • 小偷程序做的网站能用吗南宁网络推广热线
  • 织梦网站被攻击下载地图导航手机版免流量费用
  • 做网站用win还是li成都建设网官网
  • 网站建设优化重庆网络服务商电话
  • 网站怎么响应式布局今天北京发生大事了