Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Can';t从GPU复制到CPU(OpenGL)_C++_Opengl - Fatal编程技术网

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
函数的方式,目标是第一个参数。:)