Reusing Shaders

Started by
7 comments, last by Karutoh 7 years, 4 months ago

So the problem is, when I try to render multiple objects that re-use the same shader, it only renders the last object. I assume that I need to sumbit the command buffers each time I render an object?


void Button::Render()
{
    vk::Renderer *ren = GetParent()->GetParent<vk::Renderer>();
    Level *level = GetParent<Level>();


    float *data = new float[52];


    memcpy(((float *)data + 0), level->GetCurrentCamera()->GetPosition().GetData(), sizeof(float) * 4);
    memcpy(((float *)data + 4), &transform.data[0][0], sizeof(float) * 16);
    memcpy(((float *)data + 20), &level->GetCurrentCamera()->GetTransform().data[0][0], sizeof(float) * 16);
    memcpy(((float *)data + 36), &level->GetProj().data[0][0], sizeof(float) * 16);


    shader->SetUniform(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, sizeof(float) * 52, data, 0);


    delete[] data;


    shader->SetUniform(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, 0, 0, diamondBlock);
    shader->SetUniform(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2, 0, 0, diamondBlockSpec);


    data = new float[16];
    data[0] = 0.1f;
    data[1] = 0.1f;
    data[2] = 0.1f;
    data[3] = 1.0f;


    data[4] = 0.8f;
    data[5] = 0.8f;
    data[6] = 0.8f;
    data[7] = 1.0f;


    data[8] = 1.0f;
    data[9] = 1.0f;
    data[10] = 1.0f;
    data[11] = 1.0f;


    data[12] = lightPos.GetX();
    data[13] = lightPos.GetY();
    data[14] = lightPos.GetZ();
    data[15] = 1.0f;


    shader->SetUniform(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3, sizeof(float) * 16, data, 0);


    delete[] data;


    UploadMaterial(shader, 4);


    ren->Begin(0);
    shader->Begin();
    model->Begin();
    ren->End();


    ren->Submit();
}
Advertisement

What does your SetUniform function do, and how do you manage the memory for your uniform buffers?

Thank you for replying, to make things simpler I will post the Shader file so you can see exactly what it does, as it is difficult to explain.

http://puu.sh/sDHUI/745c019305.cpp

I also just recently discovered these errors. They occur when I render the second object. At the moment I do not use "vkQueueSubmit" after adding commands to the CommandBuffer after each object.


3854 | 6 | You must call vkBeginCommandBuffer() before this call to vkCmdBindPipeline()


3854 | 6 | You must call vkBeginCommandBuffer() before this call to vkCmdBindDescriptorSets()


3854 | 6 | You must call vkBeginCommandBuffer() before this call to vkCmdBindVertexBuffer()


3854 | 6 | You must call vkBeginCommandBuffer() before this call to vkCmdDraw()


3854 | 6 | You must call vkBeginCommandBuffer() before this call to vkCmdEndRenderPass()


3854 | 6 | You must call vkBeginCommandBuffer() before this call to vkEndCommandBuffer()

Your loop to create your draw calls is fundamentally broken (and has nothing to do with your shader per-se). The errors are pointing you exactly to the immediate problem, there could be more though.

It'd look into how you fill in your renderpass with all the draw instructions.

Honestly I'm not surprised most of the tutorials I've gone through only recorded commands to the Graphics Queue once. I had to experiment a lot to get to what it is now. Right now I'm at work, but once I get home I will post where I use and maintain my gameloop, including the renderpass.

Honestly I'm not surprised most of the tutorials I've gone through only recorded commands to the Graphics Queue once. I had to experiment a lot to get to what it is now. Right now I'm at work, but once I get home I will post where I use and maintain my gameloop, including the renderpass.

Vulkan is not the kind of API you want to figure out by experimentation. Unless you spend the time to develop a deep understanding of the features you're using, it's going to punish you pretty hard. The validation layers are pretty good, but there are still loads of opportunities to get things wrong and end up in a situation where it appears to work, but it's actually only working by luck and timing.

Yeah I don't enjoy it. I'd much rather have a diverse tutorial with a lot of examples, but Vulkan was just recently released to the public, so I don't expect much.

Here's my files where I have my RenderPass at. I appreciate your guys' help!

Renderer.h

http://puu.sh/sFD0a/445d248ab7.h

Renderer.cpp

http://puu.sh/sFD19/cc3086f95f.cpp

I think I fixed the issue, I updated my engine with Primary and Secondary command buffers. I'm also using Dynamic Uniform Buffers, and Descriptor Set Offsets.

Edit:

That did not solve my issue.

This topic is closed to new replies.

Advertisement