C++ Can';t从GPU复制到CPU(OpenGL)
这是我的纹理类:C++ Can';t从GPU复制到CPU(OpenGL),c++,opengl,C++,Opengl,这是我的纹理类: 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 an
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读取到客户端内存
我也强烈建议大家观看
当然,
glReadPixels
从帧缓冲区读取,而不是从纹理,glgetEximage
从纹理读取。glTexSubImage*
函数将复制到纹理,而不是从纹理。使用glReadPixels
将纹理从GL读取到客户端内存
我也强烈建议大家观看
当然,
glReadPixels
从帧缓冲区读取,而不是从纹理,glGetTexImage
从纹理读取。那么什么是glGetTexImage
?我尝试了另一个glBindTexture(GL\u texture\u 2D,src.ID);glReadPixels(0,0,src.Width,src.Height,src.Format,src.DataType,dst)代码>我得到了1286
错误。。。我想我不知道如何使用它:S.@chill:值得注意的是,在OpenGL ES中没有glGetTexImage,人们确实将纹理渲染到帧缓冲区,并使用glReadPixels检索数据。但是在普通的OpenGL中,glGetTexImage当然是选择的方法。那么什么是glGetTexImage
?我试过的另一个方法是glBindTexture(GL\u TEXTURE\u 2D,src.ID);glReadPixels(0,0,src.Width,src.Height,src.Format,src.DataType,dst)代码>我得到了1286
错误。。。我想我不知道如何使用它:S.@chill:值得注意的是,在OpenGL ES中没有glGetTexImage,人们确实将纹理渲染到帧缓冲区,并使用glReadPixels检索数据。但在普通的OpenGL中,glGetTexImage当然是首选的方法。“我发现当我从GPU复制到CPU,然后再从CPU复制到GPU时,我得到了那个错误。如果我第一次从GPU复制到CPU,我没有得到任何错误。”。当您调用glTexImage*
时,GL将在GPU上分配存储。如果尝试读取尚未分配存储的纹理,则会出现错误。顺便说一句,按照编写两个memcpy
函数的方式,目标是第一个参数。:)“当我从GPU复制到CPU,然后从CPU复制到GPU时,我发现了那个错误。若我第一次从GPU复制到CPU,我并没有任何错误。”。当您调用glTexImage*
时,GL将在GPU上分配存储。如果尝试读取尚未分配存储的纹理,则会出现错误。顺便说一句,按照编写两个memcpy
函数的方式,目标是第一个参数。:)