2

这是我的纹理类:

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);
}
4

1 回答 1

4

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

我还强烈建议观看优化的纹理传输

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

于 2012-12-02T15:55:38.153 回答