Browse Source

vulkan: implement Shader::getVertexAttributeIndex

niki 2 years ago
parent
commit
57207606c0

+ 6 - 0
src/modules/graphics/vulkan/Graphics.cpp

@@ -322,6 +322,8 @@ bool Graphics::setMode(void* context, int width, int height, int pixelwidth, int
 	currentFrame = 0;
 
 	created = true;
+	drawCalls = 0;
+	drawCallsBatched = 0;
 
 	return true;
 }
@@ -462,6 +464,7 @@ void Graphics::draw(const DrawCommand& cmd) {
 	prepareDraw(*cmd.attributes, *cmd.buffers, cmd.texture, cmd.primitiveType, cmd.cullMode);
 
 	vkCmdDraw(commandBuffers.at(currentFrame), static_cast<uint32_t>(cmd.vertexCount), static_cast<uint32_t>(cmd.instanceCount), static_cast<uint32_t>(cmd.vertexStart), 0);
+	drawCalls++;
 }
 
 void Graphics::draw(const DrawIndexedCommand& cmd) {
@@ -469,6 +472,7 @@ void Graphics::draw(const DrawIndexedCommand& cmd) {
 
 	vkCmdBindIndexBuffer(commandBuffers.at(currentFrame), (VkBuffer)cmd.indexBuffer->getHandle(), static_cast<VkDeviceSize>(cmd.indexBufferOffset), Vulkan::getVulkanIndexBufferType(cmd.indexType));
 	vkCmdDrawIndexed(commandBuffers.at(currentFrame), static_cast<uint32_t>(cmd.indexCount), static_cast<uint32_t>(cmd.instanceCount), 0, 0, 0);
+	drawCalls++;
 }
 
 void Graphics::drawQuads(int start, int count, const VertexAttributes& attributes, const BufferBindings& buffers, graphics::Texture* texture) {
@@ -486,6 +490,8 @@ void Graphics::drawQuads(int start, int count, const VertexAttributes& attribute
 
 		vkCmdDrawIndexed(commandBuffers.at(currentFrame), static_cast<uint32_t>(quadcount * 6), 1, 0, baseVertex, 0);
 		baseVertex += quadcount * 4;
+
+		drawCalls++;
 	}
 }
 

+ 13 - 3
src/modules/graphics/vulkan/Shader.cpp

@@ -118,7 +118,7 @@ static const TBuiltInResource defaultTBuiltInResource = {
 };
 
 static const uint32_t STREAMBUFFER_DEFAULT_SIZE = 16;
-static const uint32_t DESCRIPTOR_POOL_SIZE = 16;
+static const uint32_t DESCRIPTOR_POOL_SIZE = 1;
 
 static VkShaderStageFlagBits getStageBit(ShaderStageType type) {
 	switch (type) {
@@ -394,7 +394,8 @@ void Shader::attach() {
 }
 
 int Shader::getVertexAttributeIndex(const std::string& name) {
-	return 0;
+	auto it = attributes.find(name);
+	return it == attributes.end() ? -1 : it->second;
 }
 
 const Shader::UniformInfo* Shader::getUniformInfo(const std::string& name) const {
@@ -564,7 +565,8 @@ void Shader::compileShaders() {
 	uniformInfos.clear();
 
 	for (int i = 0; i < SHADERSTAGE_MAX_ENUM; i++) {
-		auto glslangStage = getGlslShaderType((ShaderStageType)i);
+		auto shaderStage = (ShaderStageType)i;
+		auto glslangStage = getGlslShaderType(shaderStage);
 		auto intermediate = program->getIntermediate(glslangStage);
 		if (intermediate == nullptr) {
 			continue;
@@ -746,6 +748,14 @@ void Shader::compileShaders() {
 
 			uniformInfos[u.name] = u;
 		}
+
+		if (shaderStage == SHADERSTAGE_VERTEX) {
+			for (const auto& r : shaderResources.stage_inputs) {
+				const auto& name = r.name;
+				const int attributeLocation = static_cast<int>(comp.get_decoration(r.id, spv::DecorationLocation));
+				attributes[name] = attributeLocation;
+			}
+		}
 	}
 
 	delete program;

+ 2 - 0
src/modules/graphics/vulkan/Shader.h

@@ -107,6 +107,8 @@ private:
 	uint32_t uniformLocation;
 	size_t builtinUniformDataOffset;
 
+	std::unordered_map<std::string, int> attributes;
+
 	uint32_t currentFrame;
 	uint32_t currentUsedUniformStreamBuffersCount;
 	uint32_t currentUsedDescriptorSetsCount;