gpt4 book ai didi

c++ - 无法从 GPU 复制到 CPU (OpenGL)

转载 作者:搜寻专家 更新时间:2023-10-31 01:12:29 25 4
gpt4 key购买 nike

这是我的纹理类:

struct GPUAllocation {
uint ID,VectorType,DataType,Width,Height,Format,IntFormat;
//ID is the output from glGen* and Format is the format of the texture and IntFormat
//is the Internal Format of this texture. (Width and height and too obvious)
//DataType is like GL_FLOAT...
bool isFinalized;
GPUAllocation(uint vectorType,uint dataType,uint width,uint height);
void SetSlot(int n);
void Finalize();
~GPUAllocation();
};

从该纹理复制到 RAM 的代码:

void memcpy(GPUAllocation src,void* dst) {
glBindTexture(GL_TEXTURE_2D,src.ID); //ID is the output from glGen*
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,src.Width,src.Height,src.Format,src.DataType,dst);
}

运行这个函数的代码是:

GPUAllocation gpu_alloc(PCPU_Vector1,PCPU_Float,4096,4096); //I generate Format and IntFormat here.
//The generated format is correct because when I copied from GPU to CPU I didn't got any error
float *cpu_alloc=new float[4096*4096];
memcpy(gpu_alloc,cpu_alloc); //The error occurred here!

发生的错误是 1281


编辑:我发现当我从 GPU 复制到 CPU,然后从 CPU 复制到 GPU 时,我遇到了这个错误。如果我首先从 GPU 复制到 CPU,我没有得到任何错误。从CPU复制到GPU的函数是:

void memcpy(void* src,GPUAllocation dst) {
glBindTexture(GL_TEXTURE_2D,dst.ID);
glGetTexImage(GL_TEXTURE_2D,0,dst.Format,dst.DataType,src);
}

最佳答案

glTexSubImage* 函数复制 纹理,而不是从。使用 glReadPixels 将纹理从 GL 读取到客户端内存。

我也强烈推荐观看 Optimized Texture Transfers

附言。错误,当然,glReadPixels 从帧缓冲区读取,而不是从纹理读取,glGetTexImage 从纹理读取。

关于c++ - 无法从 GPU 复制到 CPU (OpenGL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13670839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com