Hi! I have a bug…
It draws well excepts when instance count is 1.
void PerPixelLinkedListRenderComponent::drawInstances() {
for (unsigned int i = 0; i < Batcher::nbPrimitiveTypes; i++) {
vbBindlessTex[i].clear();
materialDatas[i].clear();
modelDatas[i].clear();
}
std::array<std::vector<DrawArraysIndirectCommand>, Batcher::nbPrimitiveTypes> drawArraysIndirectCommands;
std::array<unsigned int, Batcher::nbPrimitiveTypes> firstIndex, baseInstance;
for (unsigned int i = 0; i < firstIndex.size(); i++) {
firstIndex[i] = 0;
}
for (unsigned int i = 0; i < baseInstance.size(); i++) {
baseInstance[i] = 0;
}
for (unsigned int i = 0; i < m_normals.size(); i++) {
if (m_normals[i].getAllVertices().getVertexCount() > 0) {
DrawArraysIndirectCommand drawArraysIndirectCommand;
//std::cout<<"next frame draw normal"<<std::endl;
if (core::Application::app != nullptr) {
float time = core::Application::getTimeClk().getElapsedTime().asSeconds();
indirectDrawPushConsts.time = time;
}
unsigned int p = m_normals[i].getAllVertices().getPrimitiveType();
/*if (m_normals[i].getVertexArrays()[0]->getEntity()->getRootType() == "E_MONSTER") {
std::cout<<"tex coords : "<<(*m_normals[i].getVertexArrays()[0])[0].texCoords.x<<","<<(*m_normals[i].getVertexArrays()[0])[0].texCoords.y<<std::endl;
}*/
unsigned int vertexCount = 0;
MaterialData material;
material.textureIndex = (m_normals[i].getMaterial().getTexture() != nullptr) ? m_normals[i].getMaterial().getTexture()->getId() : 0;
material.materialType = m_normals[i].getMaterial().getType();
materialDatas[p].push_back(material);
for (unsigned int j = 0; j < m_normals[i].getAllVertices().getVertexCount(); j++) {
vbBindlessTex[p].append(m_normals[i].getAllVertices()[j]);
vertexCount++;
}
TransformMatrix tm;
ModelData modelData;
modelData.worldMat = tm.getMatrix().transpose();
modelDatas[p].push_back(modelData);
drawArraysIndirectCommand.count = vertexCount;
drawArraysIndirectCommand.firstIndex = firstIndex[p];
drawArraysIndirectCommand.baseInstance = baseInstance[p];
drawArraysIndirectCommand.instanceCount = 1;
drawArraysIndirectCommands[p].push_back(drawArraysIndirectCommand);
firstIndex[p] += vertexCount;
baseInstance[p] += 1;
/*for (unsigned int j = 0; j < m_normals[i].getVertexArrays().size(); j++) {
if (m_normals[i].getVertexArrays()[j]->getEntity() != nullptr && m_normals[i].getVertexArrays()[j]->getEntity()->getRootType() == "E_HERO") {
for (unsigned int n = 0; n < m_normals[i].getVertexArrays()[j]->getVertexCount(); n++)
std::cout<<"position hero : "<<(*m_normals[i].getVertexArrays()[j])[n].position.x<<","<<(*m_normals[i].getVertexArrays()[j])[n].position.y<<","<<(*m_normals[i].getVertexArrays()[j])[n].position.z<<std::endl;
}
}*/
}
}
for (unsigned int i = 0; i < m_instances.size(); i++) {
if (m_instances[i].getAllVertices().getVertexCount() > 0) {
DrawArraysIndirectCommand drawArraysIndirectCommand;
unsigned int p = m_instances[i].getAllVertices().getPrimitiveType();
if (core::Application::app != nullptr) {
float time = core::Application::getTimeClk().getElapsedTime().asSeconds();
indirectDrawPushConsts.time = time;
}
std::vector<TransformMatrix*> tm = m_instances[i].getTransforms();
for (unsigned int j = 0; j < tm.size(); j++) {
tm[j]->update();
ModelData modelData;
modelData.worldMat = tm[j]->getMatrix().transpose();
modelDatas[p].push_back(modelData);
}
MaterialData materialData;
materialData.textureIndex = (m_instances[i].getMaterial().getTexture() != nullptr) ? m_instances[i].getMaterial().getTexture()->getId() : 0;
materialData.materialType = m_instances[i].getMaterial().getType();
materialDatas[p].push_back(materialData);
unsigned int vertexCount = 0;
if (m_instances[i].getVertexArrays().size() > 0) {
Entity* entity = m_instances[i].getVertexArrays()[0]->getEntity();
for (unsigned int j = 0; j < m_instances[i].getVertexArrays().size(); j++) {
if (entity == m_instances[i].getVertexArrays()[j]->getEntity()) {
for (unsigned int k = 0; k < m_instances[i].getVertexArrays()[j]->getVertexCount(); k++) {
vertexCount++;
vbBindlessTex[p].append((*m_instances[i].getVertexArrays()[j])[k]);
}
}
}
}
drawArraysIndirectCommand.count = vertexCount;
drawArraysIndirectCommand.firstIndex = firstIndex[p];
drawArraysIndirectCommand.baseInstance = baseInstance[p];
drawArraysIndirectCommand.instanceCount = tm.size();
drawArraysIndirectCommands[p].push_back(drawArraysIndirectCommand);
firstIndex[p] += vertexCount;
baseInstance[p] += tm.size();
//std::cout<<"texture : "<<m_instances[i].getMaterial().getTexture()<<std::endl;
//std::cout<<"entity : "<<m_instances[i].getVertexArrays()[0]->getEntity()->getRootEntity()->getType()<<std::endl;
}
}
RenderStates currentStates;
currentStates.blendMode = sf::BlendNone;
currentStates.shader = &indirectRenderingShader;
currentStates.texture = nullptr;
for (unsigned int p = 0; p < Batcher::nbPrimitiveTypes; p++) {
if (vbBindlessTex[p].getVertexCount() > 0) {
vbBindlessTex[p].update();
VkDeviceSize bufferSize = sizeof(ModelData) * modelDatas[p].size();
VkBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
void* data;
vkMapMemory(vkDevice.getDevice(), stagingBufferMemory, 0, bufferSize, 0, &data);
memcpy(data, modelDatas[p].data(), (size_t)bufferSize);
vkUnmapMemory(vkDevice.getDevice(), stagingBufferMemory);
modelDataShaderStorageBuffers.resize(frameBuffer.getMaxFramesInFlight());
modelDataShaderStorageBuffersMemory.resize(frameBuffer.getMaxFramesInFlight());
for (unsigned int i = 0; i < frameBuffer.getMaxFramesInFlight(); i++) {
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, modelDataShaderStorageBuffers[i], modelDataShaderStorageBuffersMemory[i]);
copyBuffer(stagingBuffer, modelDataShaderStorageBuffers[i], bufferSize);
}
vkDestroyBuffer(vkDevice.getDevice(), stagingBuffer, nullptr);
vkFreeMemory(vkDevice.getDevice(), stagingBufferMemory, nullptr);
bufferSize = sizeof(MaterialData) * materialDatas[p].size();
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
vkMapMemory(vkDevice.getDevice(), stagingBufferMemory, 0, bufferSize, 0, &data);
memcpy(data, materialDatas[p].data(), (size_t)bufferSize);
vkUnmapMemory(vkDevice.getDevice(), stagingBufferMemory);
materialDataShaderStorageBuffers.resize(frameBuffer.getMaxFramesInFlight());
materialDataShaderStorageBuffersMemory.resize(frameBuffer.getMaxFramesInFlight());
for (unsigned int i = 0; i < frameBuffer.getMaxFramesInFlight(); i++) {
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, materialDataShaderStorageBuffers[i], materialDataShaderStorageBuffersMemory[i]);
copyBuffer(stagingBuffer, materialDataShaderStorageBuffers[i], bufferSize);
}
vkDestroyBuffer(vkDevice.getDevice(), stagingBuffer, nullptr);
vkFreeMemory(vkDevice.getDevice(), stagingBufferMemory, nullptr);
bufferSize = sizeof(DrawArraysIndirectCommand) * drawArraysIndirectCommands[p].size();
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
vkMapMemory(vkDevice.getDevice(), stagingBufferMemory, 0, bufferSize, 0, &data);
memcpy(data, drawArraysIndirectCommands[p].data(), (size_t)bufferSize);
vkUnmapMemory(vkDevice.getDevice(), stagingBufferMemory);
createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vboIndirect, vboIndirectMemory);
copyBuffer(stagingBuffer, vboIndirect, bufferSize);
vkDestroyBuffer(vkDevice.getDevice(), stagingBuffer, nullptr);
vkFreeMemory(vkDevice.getDevice(), stagingBufferMemory, nullptr);
createDescriptorPool();
createDescriptorSetLayout();
createDescriptorSets(p);
frameBuffer.updateDescriptors(descriptorPool, descriptorSetLayout, descriptorSets);
VkPushConstantRange push_constant;
//this push constant range starts at the beginning
push_constant.offset = 0;
//this push constant range takes up the size of a MeshPushConstants struct
push_constant.size = sizeof(IndirectDrawPushConsts);
//this push constant range is accessible only in the vertex shader
push_constant.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
VkPipelineLayoutCreateInfo& pipelineLayoutInfo = frameBuffer.getPipelineLayoutCreateInfo();
pipelineLayoutInfo.pPushConstantRanges = &push_constant;
pipelineLayoutInfo.pushConstantRangeCount = 1;
frameBuffer.createGraphicPipeline(vbBindlessTex[p].getPrimitiveType(), currentStates);
createCommandBuffersIndirect(p, drawArraysIndirectCommands[p].size(), currentStates);
vbBindlessTex[p].clear();
}
}
}
for m_instances everything is drawn correctly, but for m_normals when I draw everything with an instance count of 1 nothing is drawn.
Why ?
Thanks.