我一直在考虑用 openCL 内核形式重写这段代码的方法。转换不会特别困难(摆脱 glm 类型和位掩码),但我坚持的部分是如何将_triangles
、_uvs
、_indices
和_normals
传递给内核。openCL 中的向量是否有任何内置功能?
如果没有任何向量支持,我看到的唯一选择是float3
为我需要返回的 3 个变量( 、 和 )中的每一个_triangles
传递_uvs
4个类型数组_normals
和 2 个float3
for数组_indices
。然后在 CPU 中将数组转换回向量并缩小它们以适应。我不太确定将这么多内存缓冲区传递给内核是一种有效的方法,因为那将是从内核传递和返回的 14 个数组。并行化时我无法使用的其他解决方案。有没有办法简化这个解决方案,或者更好但更好的解决方案?
我遇到问题的功能是_addRectangle
并且_createMesh
是将在内核中组合的功能。
void Chunk::_addRectangle(glm::vec3 center, glm::vec3 height, glm::vec3 width, unsigned tex_num, cl_uint LOD)
{
glm::vec3 corner1 = center - (height / 2.0) - (width / 2.0);
glm::vec3 corner2 = center - (height / 2.0) + (width / 2.0);
glm::vec3 corner3 = center + (height / 2.0) + (width / 2.0);
glm::vec3 corner4 = center + (height / 2.0) - (width / 2.0);
glm::vec3 normal = glm::cross(height, width);
glm::vec2 uv1;
glm::vec2 uv2;
glm::vec2 uv3;
glm::vec2 uv4;
if (fabs(normal[1]) == 1.0)
{
uv1 = glm::vec2(1.0 / _tex_atlas_width, 1);
uv2 = glm::vec2(1.0 / _tex_atlas_width, 0);
uv3 = glm::vec2(0, 0);
uv4 = glm::vec2(0, 1);
}
else
{
uv1 = glm::vec2(1.0 / _tex_atlas_width, 1);
uv2 = glm::vec2(1.0 / _tex_atlas_width, 0);
uv3 = glm::vec2(0, 0);
uv4 = glm::vec2(0, 1);
}
float add = (1.0 / double(_tex_atlas_width)) * tex_num;
uv1.x += add;
uv2.x += add;
uv3.x += add;
uv4.x += add;
// triangle 1
_triangles.push_back(corner3);
_triangles.push_back(corner2);
_triangles.push_back(corner1);
_normals.push_back(normal);
_normals.push_back(normal);
_normals.push_back(normal);
_uvs.push_back(uv1);
_uvs.push_back(uv2);
_uvs.push_back(uv3);
_indices.push_back(glm::ivec3(nrOfIndices + 0, nrOfIndices + 1, nrOfIndices + 2));
// triangle 2
_triangles.push_back(corner4);
_normals.push_back(normal);
_uvs.push_back(uv4);
_indices.push_back(glm::ivec3(nrOfIndices + 2, nrOfIndices + 3, nrOfIndices + 0));
nrOfIndices += 4;
}
void Chunk::_createMesh(glm::ivec3 pos, int landmap_flags[96 * 96 * 96], cl_int LOD)
{
std::byte* faces = new std::byte[chunkSize / LOD * chunkSize / LOD * chunkSize / LOD];
int index = 0;
// a index conversion from a single index array to a 3d array
// landmap_flags[x + y * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))] is
for (int x = LOD; x < chunkSize + LOD; x += LOD) {
for (int y = LOD; y < chunkSize + LOD; y += LOD) {
for (int z = LOD; z < chunkSize + LOD; z += LOD) {
x + y * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD));
faces[index] = (std::byte)0;
if (landmap_flags[x + y * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))] == BLOCK::AIR)
{
index++;
continue;
}
if (landmap_flags[x + y * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))] != BLOCK::AIR)
{
if (landmap_flags[(x - LOD) + y * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))] == BLOCK::AIR)
faces[index] |= (std::byte)Direction::South;
if (landmap_flags[(x + LOD) + y * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))] == BLOCK::AIR)
faces[index] |= (std::byte)Direction::North;
if (landmap_flags[x + (y - LOD) * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))] == BLOCK::AIR)
faces[index] |= (std::byte)Direction::Down;
if (landmap_flags[x + (y + LOD) * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))] == BLOCK::AIR)
faces[index] |= (std::byte)Direction::Up;
if (landmap_flags[x + y * (chunkSize + (2 * LOD)) + (z - LOD) * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))] == BLOCK::AIR)
faces[index] |= (std::byte)Direction::West;
if (landmap_flags[x + y * (chunkSize + (2 * LOD)) + (z + LOD) * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))] == BLOCK::AIR)
faces[index] |= (std::byte)Direction::East;
}
if (faces[index] == (std::byte)0)
continue;
if ((faces[index] & (std::byte)Direction::North) != (std::byte)0)
{
_addRectangle(
glm::vec3( x + ((chunkSize - 1) * pos.x) - (chunkSize / 2) - (float(LOD - 1) / 2) + (float(LOD) / 2),
y + ((chunkSize - 1) * pos.y) - (chunkSize / 2) - (float(LOD - 1) / 2),
z + ((chunkSize - 1) * pos.z) - (chunkSize / 2) - (float(LOD - 1) / 2)),
glm::vec3(0, LOD, 0),
glm::vec3(0, 0, -LOD),
landmap_flags[x + y * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))],
LOD);
}
if ((faces[index] & (std::byte)Direction::East) != (std::byte)0)
{
_addRectangle(
glm::vec3( x + ((chunkSize - 1) * pos.x) - (chunkSize / 2) - (float(LOD - 1) / 2),
y + ((chunkSize - 1) * pos.y) - (chunkSize / 2) - (float(LOD - 1) / 2),
z + ((chunkSize - 1) * pos.z) - (chunkSize / 2) - (float(LOD - 1) / 2) + (float(LOD) / 2)),
glm::vec3(0, LOD, 0),
glm::vec3(LOD, 0, 0),
landmap_flags[x + y * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))],
LOD);
}
if ((faces[index] & (std::byte)Direction::South) != (std::byte)0)
{
_addRectangle(
glm::vec3( x + ((chunkSize - 1) * pos.x) - (chunkSize / 2) - (float(LOD - 1) / 2) - (float(LOD) / 2),
y + ((chunkSize - 1) * pos.y) - (chunkSize / 2) - (float(LOD - 1) / 2),
z + ((chunkSize - 1) * pos.z) - (chunkSize / 2) - (float(LOD - 1) / 2)),
glm::vec3(0, LOD, 0),
glm::vec3(0, 0, LOD),
landmap_flags[x + y * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))],
LOD);
}
if ((faces[index] & (std::byte)Direction::West) != (std::byte)0)
{
_addRectangle(
glm::vec3( x + ((chunkSize - 1) * pos.x) - (chunkSize / 2) - (float(LOD - 1) / 2),
y + ((chunkSize - 1) * pos.y) - (chunkSize / 2) - (float(LOD - 1) / 2),
z + ((chunkSize - 1) * pos.z) - (chunkSize / 2) - (float(LOD - 1) / 2) - (float(LOD) / 2)),
glm::vec3(0, LOD, 0),
glm::vec3(-LOD, 0, 0),
landmap_flags[x + y * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))],
LOD);
}
if ((faces[index] & (std::byte)Direction::Up) != (std::byte)0)
{
_addRectangle(
glm::vec3( x + ((chunkSize - 1) * pos.x) - (chunkSize / 2) - (float(LOD - 1) / 2),
y + ((chunkSize - 1) * pos.y) - (chunkSize / 2) - (float(LOD - 1) / 2) + (float(LOD) / 2),
z + ((chunkSize - 1) * pos.z) - (chunkSize / 2) - (float(LOD - 1) / 2)),
glm::vec3(LOD, 0, 0),
glm::vec3(0, 0, LOD),
landmap_flags[x + y * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))],
LOD);
}
if ((faces[index] & (std::byte)Direction::Down) != (std::byte)0)
{
_addRectangle(
glm::vec3( x + ((chunkSize - 1) * pos.x) - (chunkSize / 2) - (float(LOD - 1) / 2),
y + ((chunkSize - 1) * pos.y) - (chunkSize / 2) - (float(LOD - 1) / 2) - (float(LOD) / 2),
z + ((chunkSize - 1) * pos.z) - (chunkSize / 2) - (float(LOD - 1) / 2)),
glm::vec3(LOD, 0, 0),
glm::vec3(0, 0, -LOD),
landmap_flags[x + y * (chunkSize + (2 * LOD)) + z * (chunkSize + (2 * LOD)) * (chunkSize + (2 * LOD))],
LOD);
}
index++;
}
}
}
delete[]faces;
}
谢谢!
编辑:存储数据的一种可能更有效的方法是几种 float4 类型。例如:
const uint n = get_global_id(0);
float4 triangles{1, 2, 3, 4}; // calculated values for each vertex
//(float4 list[size];) from constructor
list[n] = triangles;