Moved material parameter into separate struct to be used for push constants (Fixes #328)

This commit is contained in:
saschawillems 2017-05-08 20:07:02 +02:00
parent fdac484726
commit fa18736ee5
2 changed files with 42 additions and 28 deletions

View file

@ -32,12 +32,21 @@
#define OBJ_DIM 0.05f #define OBJ_DIM 0.05f
struct Material { struct Material {
// Parameter block used as push constant block
struct PushBlock {
float roughness; float roughness;
float metallic; float metallic;
float r, g, b; // Color components as single floats because we use push constants float r, g, b;
} params;
std::string name; std::string name;
Material() {}; Material() {};
Material(std::string n, glm::vec3 c, float r, float m) : name(n), roughness(r), metallic(m), r(c.r), g(c.g), b(c.b) { }; Material(std::string n, glm::vec3 c, float r, float m) : name(n) {
params.roughness = r;
params.metallic = m;
params.r = c.r;
params.g = c.g;
params.b = c.b;
};
}; };
class VulkanExample : public VulkanExampleBase class VulkanExample : public VulkanExampleBase
@ -181,17 +190,16 @@ public:
Material mat = materials[materialIndex]; Material mat = materials[materialIndex];
//#define SINGLE_MESH 1 //#define SINGLE_ROW 1
#ifdef SINGLE_MESH #ifdef SINGLE_ROW
mat.metallic = 1.0; mat.params.metallic = 1.0;
mat.roughness = 0.1;
uint32_t objcount = 10; uint32_t objcount = 10;
for (uint32_t x = 0; x < objcount; x++) { for (uint32_t x = 0; x < objcount; x++) {
glm::vec3 pos = glm::vec3(float(x - (objcount / 2.0f)) * 2.5f, 0.0f, 0.0f); glm::vec3 pos = glm::vec3(float(x - (objcount / 2.0f)) * 2.5f, 0.0f, 0.0f);
mat.roughness = glm::clamp((float)x / (float)objcount, 0.005f, 1.0f); mat.params.roughness = glm::clamp((float)x / (float)objcount, 0.005f, 1.0f);
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::vec3), &pos); vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::vec3), &pos);
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(glm::vec3), sizeof(Material), &mat); vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(glm::vec3), sizeof(Material::PushBlock), &mat);
vkCmdDrawIndexed(drawCmdBuffers[i], models.objects[models.objectIndex].indexCount, 1, 0, 0, 0); vkCmdDrawIndexed(drawCmdBuffers[i], models.objects[models.objectIndex].indexCount, 1, 0, 0, 0);
} }
#else #else
@ -199,9 +207,9 @@ public:
for (uint32_t x = 0; x < GRID_DIM; x++) { for (uint32_t x = 0; x < GRID_DIM; x++) {
glm::vec3 pos = glm::vec3(float(x - (GRID_DIM / 2.0f)) * 2.5f, 0.0f, float(y - (GRID_DIM / 2.0f)) * 2.5f); glm::vec3 pos = glm::vec3(float(x - (GRID_DIM / 2.0f)) * 2.5f, 0.0f, float(y - (GRID_DIM / 2.0f)) * 2.5f);
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::vec3), &pos); vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::vec3), &pos);
mat.metallic = (float)x / (float)(GRID_DIM - 1); mat.params.metallic = (float)x / (float)(GRID_DIM - 1);
mat.roughness = glm::clamp((float)y / (float)(GRID_DIM - 1), 0.05f, 1.0f); mat.params.roughness = glm::clamp((float)y / (float)(GRID_DIM - 1), 0.05f, 1.0f);
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(glm::vec3), sizeof(Material), &mat); vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(glm::vec3), sizeof(Material::PushBlock), &mat);
vkCmdDrawIndexed(drawCmdBuffers[i], models.objects[models.objectIndex].indexCount, 1, 0, 0, 0); vkCmdDrawIndexed(drawCmdBuffers[i], models.objects[models.objectIndex].indexCount, 1, 0, 0, 0);
} }
} }
@ -242,7 +250,7 @@ public:
std::vector<VkPushConstantRange> pushConstantRanges = { std::vector<VkPushConstantRange> pushConstantRanges = {
vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::vec3), 0), vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::vec3), 0),
vks::initializers::pushConstantRange(VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(Material), sizeof(glm::vec3)), vks::initializers::pushConstantRange(VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(Material::PushBlock), sizeof(glm::vec3)),
}; };
pipelineLayoutCreateInfo.pushConstantRangeCount = 2; pipelineLayoutCreateInfo.pushConstantRangeCount = 2;

View file

@ -34,14 +34,20 @@
#define GRID_DIM 7 #define GRID_DIM 7
struct Material { struct Material {
// Set in object rendering loop // Parameter block used as push constant block
struct PushBlock {
float roughness = 0.0f; float roughness = 0.0f;
float metallic = 0.0f; float metallic = 0.0f;
float specular = 0.0f; float specular = 0.0f;
float r, g, b; float r, g, b;
} params;
std::string name; std::string name;
Material() {}; Material() {};
Material(std::string n, glm::vec3 c) : r(c.r), g(c.g), b(c.b), name(n) { }; Material(std::string n, glm::vec3 c) : name(n) {
params.r = c.r;
params.g = c.g;
params.b = c.b;
};
}; };
class VulkanExample : public VulkanExampleBase class VulkanExample : public VulkanExampleBase
@ -221,25 +227,25 @@ public:
#define SINGLE_ROW 1 #define SINGLE_ROW 1
#ifdef SINGLE_ROW #ifdef SINGLE_ROW
mat.metallic = 1.0; mat.params.metallic = 1.0;
uint32_t objcount = 10; uint32_t objcount = 10;
for (uint32_t x = 0; x < objcount; x++) { for (uint32_t x = 0; x < objcount; x++) {
glm::vec3 pos = glm::vec3(float(x - (objcount / 2.0f)) * 2.15f, 0.0f, 0.0f); glm::vec3 pos = glm::vec3(float(x - (objcount / 2.0f)) * 2.15f, 0.0f, 0.0f);
mat.roughness = glm::clamp((float)x / (float)objcount, 0.005f, 1.0f); mat.params.roughness = glm::clamp((float)x / (float)objcount, 0.005f, 1.0f);
mat.metallic = 1.0f - glm::clamp((float)x / (float)objcount, 0.005f, 1.0f); mat.params.metallic = 1.0f - glm::clamp((float)x / (float)objcount, 0.005f, 1.0f);
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::vec3), &pos); vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::vec3), &pos);
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(glm::vec3), sizeof(Material), &mat); vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(glm::vec3), sizeof(Material::PushBlock), &mat);
vkCmdDrawIndexed(drawCmdBuffers[i], models.objects[models.objectIndex].indexCount, 1, 0, 0, 0); vkCmdDrawIndexed(drawCmdBuffers[i], models.objects[models.objectIndex].indexCount, 1, 0, 0, 0);
} }
#else #else
for (uint32_t y = 0; y < GRID_DIM; y++) { for (uint32_t y = 0; y < GRID_DIM; y++) {
mat.metallic = (float)y / (float)(GRID_DIM); mat.params.metallic = (float)y / (float)(GRID_DIM);
for (uint32_t x = 0; x < GRID_DIM; x++) { for (uint32_t x = 0; x < GRID_DIM; x++) {
glm::vec3 pos = glm::vec3(float(x - (GRID_DIM / 2.0f)) * 2.5f, 0.0f, float(y - (GRID_DIM / 2.0f)) * 2.5f); glm::vec3 pos = glm::vec3(float(x - (GRID_DIM / 2.0f)) * 2.5f, 0.0f, float(y - (GRID_DIM / 2.0f)) * 2.5f);
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::vec3), &pos); vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::vec3), &pos);
mat.roughness = glm::clamp((float)x / (float)(GRID_DIM), 0.05f, 1.0f); mat.params.roughness = glm::clamp((float)x / (float)(GRID_DIM), 0.05f, 1.0f);
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(glm::vec3), sizeof(Material), &mat); vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(glm::vec3), sizeof(Material::PushBlock), &mat);
vkCmdDrawIndexed(drawCmdBuffers[i], models.objects[models.objectIndex].indexCount, 1, 0, 0, 0); vkCmdDrawIndexed(drawCmdBuffers[i], models.objects[models.objectIndex].indexCount, 1, 0, 0, 0);
} }
} }
@ -344,7 +350,7 @@ public:
// Push constant ranges // Push constant ranges
std::vector<VkPushConstantRange> pushConstantRanges = { std::vector<VkPushConstantRange> pushConstantRanges = {
vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::vec3), 0), vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::vec3), 0),
vks::initializers::pushConstantRange(VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(Material), sizeof(glm::vec3)), vks::initializers::pushConstantRange(VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(Material::PushBlock), sizeof(glm::vec3)),
}; };
pipelineLayoutCreateInfo.pushConstantRangeCount = 2; pipelineLayoutCreateInfo.pushConstantRangeCount = 2;
pipelineLayoutCreateInfo.pPushConstantRanges = pushConstantRanges.data(); pipelineLayoutCreateInfo.pPushConstantRanges = pushConstantRanges.data();