Sign in to follow this  
Karutoh

Reusing Shaders

Recommended Posts

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();
}
Edited by Karutoh

Share this post


Link to post
Share on other sites

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()
Edited by Karutoh

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by Karutoh

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this