コード例 #1
0
ファイル: PhongMaterial.cpp プロジェクト: YoutaVen/Vulkan
IDescriptorSetsSP PhongMaterial::createDescriptorSetsByName(const std::string& nodeName)
{
	if (allDescriptorSets.contains(nodeName))
	{
		return allDescriptorSets[nodeName];
	}

	//

	if (this->nodeName == "")
	{
		this->nodeName = nodeName;

		allDescriptorPools[nodeName] = descriptorPool;

		allDescriptorSets[nodeName] = descriptorSets;

		return descriptorSets;
	}

	//

	auto currentDescriptorPool = descriptorPoolCreate(descriptorPool->getDevice(), descriptorPool->getFlags(), descriptorPool->getMaxSets(), descriptorPool->getPoolSizeCount(), descriptorPool->getPoolSizes());

    if (!currentDescriptorPool.get())
    {
        return IDescriptorSetsSP();
    }

	auto currentDescriptorSets = descriptorSetsCreate(currentDescriptorPool->getDevice(), currentDescriptorPool->getDescriptorPool(), descriptorSets->getDescriptorSetCount(), descriptorSets->getSetLayouts());

    if (!currentDescriptorSets.get())
    {
        return IDescriptorSetsSP();
    }

    allDescriptorPools[nodeName] = currentDescriptorPool;

    allDescriptorSets[nodeName] = currentDescriptorSets;

    //

    return allDescriptorSets[nodeName];
}
コード例 #2
0
void vkeGameRendererDynamic::initDescriptorSets(){

	VulkanDC *dc = VulkanDC::Get();
	VulkanDC::Device *device = dc->getDefaultDevice();
	uint32_t count = 1;
	if (!dc) return;


	initCamera();

	vkResetDescriptorPool(device->getVKDevice(), getDescriptorPool(), 0);

	VkWriteDescriptorSet writes[5];

	/*----------------------------------------------------------
	Get the resource data for the bindings.
	----------------------------------------------------------*/


	/*
	Terrain textures.
	*/
	VkeTexture::Data terrain = m_textures.getTexture(0)->getData();

	/*
	Camera and Light uniforms
	*/
	VkDescriptorBufferInfo camInfo = m_camera->getDescriptor();
	VkDescriptorBufferInfo lightInfo = m_light->getDescriptor();

	/*
	Cube map texture.
	*/
	VkeCubeTexture::Data cube = m_cube_textures.getTexture(1)->getData();

	/*
	Create descriptor image info array
	for the terrain textures.
	*/
	VkDescriptorImageInfo fpSampler = { terrain.sampler, terrain.view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL };


	VkeMaterial *material = m_materials->getMaterial(0);


	/*
	Create descriptor image info array
	for the scene textures.
	*/
	uint32_t texCount = m_materials->count();

	VkDescriptorImageInfo *texImageInfo = (VkDescriptorImageInfo*)malloc(texCount * sizeof(VkDescriptorImageInfo));

	for (uint32_t i = 0; i < texCount; ++i){
		VkeMaterial *mtrl = m_materials->getMaterial(i);
		VkeTexture::Data texData = mtrl->getTextures().getTexture(0)->getData();
		texImageInfo[i].imageView = texData.view;
		texImageInfo[i].sampler = texData.sampler;
	}

	/*
	Create descriptor image info for
	the cube map texture.
	*/

	VkDescriptorImageInfo cubeTexture;
	cubeTexture.sampler = cube.sampler;
	cubeTexture.imageView = cube.view;
	cubeTexture.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;

	/*
	Capture the layouts for the scene descriptor
	sets and the texture descriptor sets.
	*/
	VkDescriptorSetLayout *textureLayouts = (VkDescriptorSetLayout*)malloc(sizeof(VkDescriptorSetLayout) * texCount);

	for (uint32_t i = 0; i < texCount; ++i){
		textureLayouts[i] = m_texture_descriptor_set_layout;
	}

	/*
	Allocate storage for the scene and
	texture descriptor sets.
	*/



	/*----------------------------------------------------------
	Allocate the descriptor sets.
	----------------------------------------------------------*/
	m_texture_descriptor_sets = (VkDescriptorSet*)malloc(sizeof(VkDescriptorSet));

	/*
	Set up the alocate info structure for
	the descriptor sets.
	*/
	VkDescriptorSetAllocateInfo descAlloc;
	memset(&descAlloc, 0, sizeof(descAlloc));
	descAlloc.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
	descAlloc.descriptorPool = getDescriptorPool();
	descAlloc.pSetLayouts = &m_scene_descriptor_layout;
	descAlloc.descriptorSetCount = 1;


	VKA_CHECK_ERROR(vkAllocateDescriptorSets(device->getVKDevice(), &descAlloc, &m_scene_descriptor_set), "Could not allocate descriptor sets.\n");

	/*
	Set up the texture descriptor sets.
	*/
	descAlloc.pSetLayouts = &m_texture_descriptor_set_layout;
	descAlloc.descriptorSetCount = 1;

	VKA_CHECK_ERROR(vkAllocateDescriptorSets(device->getVKDevice(), &descAlloc, m_texture_descriptor_sets), "Could not allocate texture descriptor sets.\n");

	/*
	Set up the skybox descriptor sets.
	*/
	descAlloc.pSetLayouts = &m_quad_descriptor_set_layout;
	descAlloc.descriptorSetCount = 1;

	VKA_CHECK_ERROR(vkAllocateDescriptorSets(device->getVKDevice(), &descAlloc, &m_quad_descriptor_set), "Could not allocate descriptor sets.\n");

	/*
	Set up the terrian descriptor sets.
	*/
	descAlloc.pSetLayouts = &m_terrain_descriptor_set_layout;
	descAlloc.descriptorSetCount = 1;

	VKA_CHECK_ERROR(vkAllocateDescriptorSets(device->getVKDevice(), &descAlloc, &m_terrain_descriptor_set), "Could not allocate descriptor sets.\n");


	/*
	Set up the transforms descriptor sets.
	*/
	descAlloc.pSetLayouts = &m_transform_descriptor_layout;
	descAlloc.descriptorSetCount = 1;

	VKA_CHECK_ERROR(vkAllocateDescriptorSets(device->getVKDevice(), &descAlloc, &m_transform_descriptor_set), "Could not allocate descriptor sets.\n");


	/*----------------------------------------------------------
	Update the descriptor sets with resource bindings.
	----------------------------------------------------------*/

	/*
	Scene layout bindings (set 0)
	Binding 0:		Environment Cube Map
	Binding 1:		Camera Matrix
	Binding 2:		Model Matrix
	Binding 3:		Material
	*/

	descriptorSetWrite(&writes[0], 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_NULL_HANDLE, &cubeTexture, 0, m_scene_descriptor_set);//cubemap
	descriptorSetWrite(&writes[1], 1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, &camInfo, VK_NULL_HANDLE, 0, m_scene_descriptor_set); //Camera
	descriptorSetWrite(&writes[2], 2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, &m_uniforms_descriptor, VK_NULL_HANDLE, 0, m_scene_descriptor_set);//modelview
	descriptorSetWrite(&writes[3], 3, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, &material->getDescriptor(), VK_NULL_HANDLE, 0, m_scene_descriptor_set);//material

	vkUpdateDescriptorSets(device->getVKDevice(), 4, writes, 0, NULL);

	/*
	Transform layout bindings (set 0)
	Binding 0:		Transform
	*/

	descriptorSetWrite(&writes[0], 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, &m_transforms_descriptor, VK_NULL_HANDLE, 0, m_transform_descriptor_set);//transform
	vkUpdateDescriptorSets(device->getVKDevice(), 1, writes, 0, NULL);


	/*
	Scene layout bindings (set 1)
	Binding 0:		Scene texture array
	*/
	descriptorSetWrite(&writes[0], 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, texCount, VK_NULL_HANDLE, texImageInfo, 0, m_texture_descriptor_sets[0]);//cubemap

	vkUpdateDescriptorSets(device->getVKDevice(), (uint32_t)1, writes, 0, NULL);
	//Free the texture image info allocated earlier.
	free(texImageInfo);



	/*
	Skybox layout bindings (set 0)
	Binding 0:		Skybox Uniforms
	Binding 1:		Skybox Textures
	Binding 2:		Camera uniforms
	*/

	descriptorSetWrite(&writes[0], 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, &m_screen_quad.getData().descriptor, VK_NULL_HANDLE, 0, m_quad_descriptor_set);
	descriptorSetWrite(&writes[1], 1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_NULL_HANDLE, &cubeTexture, 0, m_quad_descriptor_set);
	descriptorSetWrite(&writes[2], 2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, &camInfo, VK_NULL_HANDLE, 0, m_quad_descriptor_set);

	vkUpdateDescriptorSets(device->getVKDevice(), 3, writes, 0, NULL);


	/*
	Terrain layout bindings (set 0)
	Binding 0:		Terrain Uniforms
	Binding 1:		Camera Uniforms
	Binding 2:		Terrain texture array
	*/

	descriptorSetWrite(&writes[0], 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, &m_terrain_quad.getData().descriptor, VK_NULL_HANDLE, 0, m_terrain_descriptor_set);
	descriptorSetWrite(&writes[1], 1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, &camInfo, VK_NULL_HANDLE, 0, m_terrain_descriptor_set);
	descriptorSetWrite(&writes[2], 2, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1, VK_NULL_HANDLE, &fpSampler, 0, m_terrain_descriptor_set);
	descriptorSetWrite(&writes[3], 3, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1, VK_NULL_HANDLE, &cubeTexture, 0, m_terrain_descriptor_set);

	vkUpdateDescriptorSets(device->getVKDevice(), 4, writes, 0, NULL);


	/*----------------------------------------------------------
	Initialise the terrain and scene command buffers.
	----------------------------------------------------------*/
	initTerrainCommand();

	for (uint32_t i = 0; i < m_max_draw_calls; ++i){
		m_draw_calls[i]->initDescriptor();
	}

	//m_test_drawcall->initDrawCommands(m_node_data->count());

	//this needs to happen in the thread.
	//m_draw_calls[0]->initDrawCommands(m_node_data->count());


}
コード例 #3
0
ファイル: PhongMaterial.cpp プロジェクト: YoutaVen/Vulkan
PhongMaterial::PhongMaterial(const PhongMaterial& other) :
    IPhongMaterial(), name(other.name), alpha(other.alpha), displacement(other.displacement), normal(other.normal), ambient(other.ambient), emissive(other.emissive), diffuse(other.diffuse), specular(other.specular), specularShininess(other.specularShininess), transparent(other.transparent), descriptorPool(), descriptorSets(), nodeName(), allDescriptorPools(), allDescriptorSets()
{
    // Textures cannot be cloned, just replaced.

    descriptorPool = descriptorPoolCreate(other.descriptorPool->getDevice(), other.descriptorPool->getFlags(), other.descriptorPool->getMaxSets(), other.descriptorPool->getPoolSizeCount(), other.descriptorPool->getPoolSizes());

    if (!descriptorPool.get())
    {
        destroy();

        return;
    }

    descriptorSets = descriptorSetsCreate(descriptorPool->getDevice(), descriptorPool->getDescriptorPool(), other.descriptorSets->getDescriptorSetCount(), other.descriptorSets->getSetLayouts());

    if (!descriptorSets.get())
    {
    	destroy();

        return;
    }

    //

    if (other.allDescriptorPools.size() != other.allDescriptorSets.size())
    {
    	destroy();

        return;
    }


    for (size_t i = 0; i < other.allDescriptorPools.size(); i++)
    {
    	auto currentDescriptorPool = descriptorPoolCreate(other.allDescriptorPools.valueAt(i)->getDevice(), other.allDescriptorPools.valueAt(i)->getFlags(), other.allDescriptorPools.valueAt(i)->getMaxSets(), other.allDescriptorPools.valueAt(i)->getPoolSizeCount(), other.allDescriptorPools.valueAt(i)->getPoolSizes());

        if (!currentDescriptorPool.get())
        {
        	destroy();

            return;
        }

        allDescriptorPools[other.allDescriptorPools.keyAt(i)] = currentDescriptorPool;

        //

        auto currentDescriptorSets = descriptorSetsCreate(currentDescriptorPool->getDevice(), currentDescriptorPool->getDescriptorPool(), other.allDescriptorSets.valueAt(i)->getDescriptorSetCount(), other.allDescriptorSets.valueAt(i)->getSetLayouts());

        if (!currentDescriptorSets.get())
        {
        	destroy();

            return;
        }

        allDescriptorSets[other.allDescriptorSets.keyAt(i)] = currentDescriptorSets;
    }

    //

    updateDescriptorImageInfo(VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_EMISSIVE - VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_EMISSIVE, this->emissive->getSampler()->getSampler(), this->emissive->getImageView()->getImageView(), this->emissive->getMemoryImage()->getImage()->getImageLayout());

    updateDescriptorImageInfo(VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_ALPHA - VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_EMISSIVE, this->alpha->getSampler()->getSampler(), this->alpha->getImageView()->getImageView(), this->alpha->getMemoryImage()->getImage()->getImageLayout());

    updateDescriptorImageInfo(VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_DISPLACEMENT - VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_EMISSIVE, this->displacement->getSampler()->getSampler(), this->displacement->getImageView()->getImageView(), this->displacement->getMemoryImage()->getImage()->getImageLayout());

    updateDescriptorImageInfo(VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_NORMAL - VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_EMISSIVE, this->normal->getSampler()->getSampler(), this->normal->getImageView()->getImageView(), this->normal->getMemoryImage()->getImage()->getImageLayout());

    //

    updateDescriptorImageInfo(VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_PHONG_AMBIENT - VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_EMISSIVE, this->ambient->getSampler()->getSampler(), this->ambient->getImageView()->getImageView(), this->ambient->getMemoryImage()->getImage()->getImageLayout());

    updateDescriptorImageInfo(VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_PHONG_DIFFUSE - VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_EMISSIVE, this->diffuse->getSampler()->getSampler(), this->diffuse->getImageView()->getImageView(), this->diffuse->getMemoryImage()->getImage()->getImageLayout());

    updateDescriptorImageInfo(VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_PHONG_SPECULAR - VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_EMISSIVE, this->specular->getSampler()->getSampler(), this->specular->getImageView()->getImageView(), this->specular->getMemoryImage()->getImage()->getImageLayout());

    updateDescriptorImageInfo(VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_PHONG_SPECULAR_SHININESS - VKTS_BINDING_UNIFORM_SAMPLER_FRAGMENT_EMISSIVE, this->specularShininess->getSampler()->getSampler(), this->specularShininess->getImageView()->getImageView(), this->specularShininess->getMemoryImage()->getImage()->getImageLayout());
}