优化地形渲染的内存

Optimizing memory for terrain rendering

本文关键字:内存 优化      更新时间:2023-10-16

im想知道(我很确定有)是否有更好的方法来处理im填充到顶点/索引缓冲区中的数据。目前,我的地形顶点结构如下:

struct VertexType
    {
        D3DXVECTOR3 position;
        D3DXVECTOR2 texture;
        D3DXVECTOR3 normal;
    };

每次游戏开始时,我都会随机生成地形,并根据这个高度图计算法线和纹理坐标。最后,我以这种方式填充顶点数组:

// Calculate the number of vertices in the terrain mesh.
m_vertexCount = (m_terrainWidth - 1) * (m_terrainHeight - 1) * 6;
// Create the vertex array.
m_vertices.resize(m_vertexCount);
// Initialize the index to the vertex buffer.
index = 0;
// Load the vertex and index array with the terrain data.
for(j=0; j<(m_terrainHeight-1); j++)
{
    for(i=0; i<(m_terrainWidth-1); i++)
    {
        index1 = (m_terrainHeight * j) + i;          // Bottom left.
        index2 = (m_terrainHeight * j) + (i+1);      // Bottom right.
        index3 = (m_terrainHeight * (j+1)) + i;      // Upper left.
        index4 = (m_terrainHeight * (j+1)) + (i+1);  // Upper right.
        // Upper left.
        tv = m_heightMap[index3].tv;
        // Modify the texture coordinates to cover the top edge.
        if(tv == 1.0f) { tv = 0.0f; }
        m_vertices[index].position = D3DXVECTOR3(m_heightMap[index3].x, m_heightMap[index3].y, m_heightMap[index3].z);
        m_vertices[index].texture = D3DXVECTOR2(m_heightMap[index3].tu, tv);
        m_vertices[index].normal = D3DXVECTOR3(m_heightMap[index3].nx, m_heightMap[index3].ny, m_heightMap[index3].nz);
        index++;
        // Upper right.
        tu = m_heightMap[index4].tu;
        tv = m_heightMap[index4].tv;
        // Modify the texture coordinates to cover the top and right edge.
        if(tu == 0.0f) { tu = 1.0f; }
        if(tv == 1.0f) { tv = 0.0f; }
        m_vertices[index].position = D3DXVECTOR3(m_heightMap[index4].x, m_heightMap[index4].y, m_heightMap[index4].z);
        m_vertices[index].texture = D3DXVECTOR2(tu, tv);
        m_vertices[index].normal = D3DXVECTOR3(m_heightMap[index4].nx, m_heightMap[index4].ny, m_heightMap[index4].nz);
        index++;
        // Bottom left.
        m_vertices[index].position = D3DXVECTOR3(m_heightMap[index1].x, m_heightMap[index1].y, m_heightMap[index1].z);
        m_vertices[index].texture = D3DXVECTOR2(m_heightMap[index1].tu, m_heightMap[index1].tv);
        m_vertices[index].normal = D3DXVECTOR3(m_heightMap[index1].nx, m_heightMap[index1].ny, m_heightMap[index1].nz);
        index++;
        // Bottom left.
        m_vertices[index].position = D3DXVECTOR3(m_heightMap[index1].x, m_heightMap[index1].y, m_heightMap[index1].z);
        m_vertices[index].texture = D3DXVECTOR2(m_heightMap[index1].tu, m_heightMap[index1].tv);
        m_vertices[index].normal = D3DXVECTOR3(m_heightMap[index1].nx, m_heightMap[index1].ny, m_heightMap[index1].nz);
        index++;
        // Upper right.
        tu = m_heightMap[index4].tu;
        tv = m_heightMap[index4].tv;
        // Modify the texture coordinates to cover the top and right edge.
        if(tu == 0.0f) { tu = 1.0f; }
        if(tv == 1.0f) { tv = 0.0f; }
        m_vertices[index].position = D3DXVECTOR3(m_heightMap[index4].x, m_heightMap[index4].y, m_heightMap[index4].z);
        m_vertices[index].texture = D3DXVECTOR2(tu, tv);
        m_vertices[index].normal = D3DXVECTOR3(m_heightMap[index4].nx, m_heightMap[index4].ny, m_heightMap[index4].nz);
        index++;
        // Bottom right.
        tu = m_heightMap[index2].tu;
        // Modify the texture coordinates to cover the right edge.
        if(tu == 0.0f) { tu = 1.0f; }
        m_vertices[index].position = D3DXVECTOR3(m_heightMap[index2].x, m_heightMap[index2].y, m_heightMap[index2].z);
        m_vertices[index].texture = D3DXVECTOR2(tu, m_heightMap[index2].tv);
        m_vertices[index].normal = D3DXVECTOR3(m_heightMap[index2].nx, m_heightMap[index2].ny, m_heightMap[index2].nz);
        index++;
    }
}
// shrink heightmap because its no longer in use...
m_heightMap.clear();
m_heightMap.swap(m_heightMap);

这部分代码取自rastertek.com。我想知道这是否不是保存了大量冗余数据(每个四边形有6个顶点)。但问题是,我以后需要这个结构来形成四叉树的缓冲区。以下是在每个节点填充缓冲区的代码(每个节点都有自己的缓冲区-有更好的方法吗?):

node->triangleCount = numTriangles;
// Calculate the number of vertices.
vertexCount = numTriangles * 3;
// Create the vertex array.
vertices = new VertexType[vertexCount];
// Create the index array.
indices = new unsigned long[vertexCount];
// Create the vertex array.
node->vertexArray = new VectorType[vertexCount];
// Initialize the index for this new vertex and index array.
index = 0;
// Go through all the triangles in the vertex list.
for(i=0; i<m_triangleCount; i++)
{
    // If the triangle is inside this node then add it to the vertex array.
    result = IsTriangleContained(i, positionX, positionZ, width);
    if(result == true)
    {
        // Calculate the index into the terrain vertex list.
        vertexIndex = i * 3;
        // Get the three vertices of this triangle from the vertex list.
        vertices[index].position = (*m_vertexList)[vertexIndex].position;
        vertices[index].texture = (*m_vertexList)[vertexIndex].texture;
        vertices[index].normal = (*m_vertexList)[vertexIndex].normal;
        indices[index] = index;
        // Also store the vertex position information in the node vertex array.
        node->vertexArray[index].x = (*m_vertexList)[vertexIndex].position.x;
        node->vertexArray[index].y = (*m_vertexList)[vertexIndex].position.y;
        node->vertexArray[index].z = (*m_vertexList)[vertexIndex].position.z;
        index++;
        vertexIndex++;
        vertices[index].position = (*m_vertexList)[vertexIndex].position;
        vertices[index].texture = (*m_vertexList)[vertexIndex].texture;
        vertices[index].normal = (*m_vertexList)[vertexIndex].normal;
        indices[index] = index;
        node->vertexArray[index].x = (*m_vertexList)[vertexIndex].position.x;
        node->vertexArray[index].y = (*m_vertexList)[vertexIndex].position.y;
        node->vertexArray[index].z =(*m_vertexList)[vertexIndex].position.z;
        index++;
        vertexIndex++;
        vertices[index].position = (*m_vertexList)[vertexIndex].position;
        vertices[index].texture = (*m_vertexList)[vertexIndex].texture;
        vertices[index].normal = (*m_vertexList)[vertexIndex].normal;
        indices[index] = index;
        node->vertexArray[index].x = (*m_vertexList)[vertexIndex].position.x;
        node->vertexArray[index].y = (*m_vertexList)[vertexIndex].position.y;
        node->vertexArray[index].z = (*m_vertexList)[vertexIndex].position.z;
        index++;
    }
}
// Set up the description of the vertex buffer.
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexType) * vertexCount;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the vertex data.
vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;
// Now finally create the vertex buffer.
device->CreateBuffer(&vertexBufferDesc, &vertexData, &node->vertexBuffer);
// Set up the description of the index buffer.
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(unsigned long) * vertexCount;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
indexBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the index data.
indexData.pSysMem = indices;
indexData.SysMemPitch = 0;
indexData.SysMemSlicePitch = 0;
// Create the index buffer.
device->CreateBuffer(&indexBufferDesc, &indexData, &node->indexBuffer);
// Release the vertex and index arrays now that the data is stored in the buffers in the node.
delete [] vertices;
vertices = 0;
delete [] indices;
indices = 0;

isTriangleContainerd()方法检查当前三角形是否在当前节点中。如果是,那么三角形将被保存在数组中,wich将填充该节点的缓冲区。正如你所看到的,由于上面提到的结构,这里的顶点选择非常简单。

问题是,如果我尝试生成和渲染大于2048的地形,m_顶点向量将分配大量内存。有更好的方法来处理这一切吗?也许可以在GPU上进行一些计算?

感谢任何愿意帮忙的人!

如果地形大小为2048,则需要2048(宽)*22048(长)*8(#float)*4(每个float字节),约为128 MB。这是一块不错的内存,但它本身也不无道理。

我不明白四叉树代码应该做什么。看起来你正在创建另两个几何体副本?为什么要这样做?