<commit_before>// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "SpirvShader.hpp"

#include "SamplerCore.hpp" // TODO: Figure out what's needed.
#include "System/Math.hpp"
#include "Vulkan/VkBuffer.hpp"
#include "Vulkan/VkDebug.hpp"
#include "Vulkan/VkDescriptorSet.hpp"
#include "Vulkan/VkPipelineLayout.hpp"
#include "Vulkan/VkImageView.hpp"
#include "Vulkan/VkSampler.hpp"
#include "Vulkan/VkDescriptorSetLayout.hpp"
#include "Device/Config.hpp"

#include <spirv/unified1/spirv.hpp>
#include <spirv/unified1/GLSL.std.450.h>

#include <climits>
#include <mutex>

namespace
{

struct SamplingRoutineKey
{
	uint32_t instruction;
	uint32_t sampler;
	uint32_t imageView;

	bool operator==(const SamplingRoutineKey &rhs) const
	{
		return instruction == rhs.instruction && sampler == rhs.sampler && imageView == rhs.imageView;
	}

	struct Hash
	{
		std::size_t operator()(const SamplingRoutineKey &key) const noexcept
		{
			return (key.instruction << 16) ^ (key.sampler << 8) ^ key.imageView;
		}
	};
};

}

namespace sw {

SpirvShader::ImageSampler *SpirvShader::getImageSampler(uint32_t inst, vk::SampledImageDescriptor const *imageDescriptor, const vk::Sampler *sampler)
{
	ImageInstruction instruction(inst);
	ASSERT(imageDescriptor->imageViewId != 0 && (sampler->id != 0 || instruction.samplerMethod == Fetch));

	// TODO(b/129523279): Move somewhere sensible.
	static std::unordered_map<SamplingRoutineKey, ImageSampler*, SamplingRoutineKey::Hash> cache;
	static std::mutex mutex;

	SamplingRoutineKey key = {inst, imageDescriptor->imageViewId, sampler->id};

	std::unique_lock<std::mutex> lock(mutex);
	auto it = cache.find(key);
	if (it != cache.end()) { return it->second; }

	auto type = imageDescriptor->type;

	Sampler samplerState = {};
	samplerState.textureType = convertTextureType(type);
	samplerState.textureFormat = imageDescriptor->format;
	samplerState.textureFilter = (instruction.samplerMethod == Gather) ? FILTER_GATHER : convertFilterMode(sampler);
	samplerState.border = sampler->borderColor;

	samplerState.addressingModeU = convertAddressingMode(0, sampler->addressModeU, type);
	samplerState.addressingModeV = convertAddressingMode(1, sampler->addressModeV, type);
	samplerState.addressingModeW = convertAddressingMode(2, sampler->addressModeW, type);

	samplerState.mipmapFilter = convertMipmapMode(sampler);
	samplerState.swizzle = imageDescriptor->swizzle;
	samplerState.gatherComponent = instruction.gatherComponent;
	samplerState.highPrecisionFiltering = false;
	samplerState.compareEnable = (sampler->compareEnable == VK_TRUE);
	samplerState.compareOp = sampler->compareOp;
	samplerState.unnormalizedCoordinates = (sampler->unnormalizedCoordinates == VK_TRUE);
	samplerState.largeTexture = (imageDescriptor->extent.width  > SHRT_MAX) ||
	                            (imageDescriptor->extent.height > SHRT_MAX) ||
	                            (imageDescriptor->extent.depth  > SHRT_MAX);

	if(sampler->ycbcrConversion)
	{
		samplerState.ycbcrModel = sampler->ycbcrConversion->ycbcrModel;
		samplerState.studioSwing = (sampler->ycbcrConversion->ycbcrRange == VK_SAMPLER_YCBCR_RANGE_ITU_NARROW);
		samplerState.swappedChroma = (sampler->ycbcrConversion->components.r != VK_COMPONENT_SWIZZLE_R);
	}

	if(sampler->anisotropyEnable != VK_FALSE)
	{
		UNSUPPORTED("anisotropyEnable");
	}

	auto fptr = emitSamplerFunction(instruction, samplerState);

	cache.emplace(key, fptr);
	return fptr;
}

SpirvShader::ImageSampler *SpirvShader::emitSamplerFunction(ImageInstruction instruction, const Sampler &samplerState)
{
	// TODO(b/129523279): Hold a separate mutex lock for the sampler being built.
	Function<Void(Pointer<Byte>, Pointer<Byte>, Pointer<SIMD::Float>, Pointer<SIMD::Float>, Pointer<Byte>)> function;
	{
		Pointer<Byte> texture = function.Arg<0>();
		Pointer<Byte> sampler = function.Arg<1>();
		Pointer<SIMD::Float> in = function.Arg<2>();
		Pointer<SIMD::Float> out = function.Arg<3>();
		Pointer<Byte> constants = function.Arg<4>();

		SIMD::Float uvw[4];
		SIMD::Float q;
		SIMD::Float lodOrBias;  // Explicit level-of-detail, or bias added to the implicit level-of-detail (depending on samplerMethod).
		Vector4f dsx;
		Vector4f dsy;
		Vector4f offset;
		SamplerFunction samplerFunction = instruction.getSamplerFunction();

		uint32_t i = 0;
		for( ; i < instruction.coordinates; i++)
		{
			uvw[i] = in[i];
		}

		if (instruction.isDref())
		{
			q = in[i];
			i++;
		}

		// TODO(b/134669567): Currently 1D textures are treated as 2D by setting the second coordinate to 0.
		// Implement optimized 1D sampling.
		if(samplerState.textureType == TEXTURE_1D)
		{
			uvw[1] = SIMD::Float(0);
		}
		else if(samplerState.textureType == TEXTURE_1D_ARRAY)
		{
			uvw[1] = SIMD::Float(0);
			uvw[2] = in[1];  // Move 1D layer coordinate to 2D layer coordinate index.
		}

		if(instruction.samplerMethod == Lod || instruction.samplerMethod == Bias || instruction.samplerMethod == Fetch)
		{<commit_msg>SpirvShader: Fully initialize variables in emitSamplerFunction()<commit_after>// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "SpirvShader.hpp"

#include "SamplerCore.hpp" // TODO: Figure out what's needed.
#include "System/Math.hpp"
#include "Vulkan/VkBuffer.hpp"
#include "Vulkan/VkDebug.hpp"
#include "Vulkan/VkDescriptorSet.hpp"
#include "Vulkan/VkPipelineLayout.hpp"
#include "Vulkan/VkImageView.hpp"
#include "Vulkan/VkSampler.hpp"
#include "Vulkan/VkDescriptorSetLayout.hpp"
#include "Device/Config.hpp"

#include <spirv/unified1/spirv.hpp>
#include <spirv/unified1/GLSL.std.450.h>

#include <climits>
#include <mutex>

namespace
{

struct SamplingRoutineKey
{
	uint32_t instruction;
	uint32_t sampler;
	uint32_t imageView;

	bool operator==(const SamplingRoutineKey &rhs) const
	{
		return instruction == rhs.instruction && sampler == rhs.sampler && imageView == rhs.imageView;
	}

	struct Hash
	{
		std::size_t operator()(const SamplingRoutineKey &key) const noexcept
		{
			return (key.instruction << 16) ^ (key.sampler << 8) ^ key.imageView;
		}
	};
};

}

namespace sw {

SpirvShader::ImageSampler *SpirvShader::getImageSampler(uint32_t inst, vk::SampledImageDescriptor const *imageDescriptor, const vk::Sampler *sampler)
{
	ImageInstruction instruction(inst);
	ASSERT(imageDescriptor->imageViewId != 0 && (sampler->id != 0 || instruction.samplerMethod == Fetch));

	// TODO(b/129523279): Move somewhere sensible.
	static std::unordered_map<SamplingRoutineKey, ImageSampler*, SamplingRoutineKey::Hash> cache;
	static std::mutex mutex;

	SamplingRoutineKey key = {inst, imageDescriptor->imageViewId, sampler->id};

	std::unique_lock<std::mutex> lock(mutex);
	auto it = cache.find(key);
	if (it != cache.end()) { return it->second; }

	auto type = imageDescriptor->type;

	Sampler samplerState = {};
	samplerState.textureType = convertTextureType(type);
	samplerState.textureFormat = imageDescriptor->format;
	samplerState.textureFilter = (instruction.samplerMethod == Gather) ? FILTER_GATHER : convertFilterMode(sampler);
	samplerState.border = sampler->borderColor;

	samplerState.addressingModeU = convertAddressingMode(0, sampler->addressModeU, type);
	samplerState.addressingModeV = convertAddressingMode(1, sampler->addressModeV, type);
	samplerState.addressingModeW = convertAddressingMode(2, sampler->addressModeW, type);

	samplerState.mipmapFilter = convertMipmapMode(sampler);
	samplerState.swizzle = imageDescriptor->swizzle;
	samplerState.gatherComponent = instruction.gatherComponent;
	samplerState.highPrecisionFiltering = false;
	samplerState.compareEnable = (sampler->compareEnable == VK_TRUE);
	samplerState.compareOp = sampler->compareOp;
	samplerState.unnormalizedCoordinates = (sampler->unnormalizedCoordinates == VK_TRUE);
	samplerState.largeTexture = (imageDescriptor->extent.width  > SHRT_MAX) ||
	                            (imageDescriptor->extent.height > SHRT_MAX) ||
	                            (imageDescriptor->extent.depth  > SHRT_MAX);

	if(sampler->ycbcrConversion)
	{
		samplerState.ycbcrModel = sampler->ycbcrConversion->ycbcrModel;
		samplerState.studioSwing = (sampler->ycbcrConversion->ycbcrRange == VK_SAMPLER_YCBCR_RANGE_ITU_NARROW);
		samplerState.swappedChroma = (sampler->ycbcrConversion->components.r != VK_COMPONENT_SWIZZLE_R);
	}

	if(sampler->anisotropyEnable != VK_FALSE)
	{
		UNSUPPORTED("anisotropyEnable");
	}

	auto fptr = emitSamplerFunction(instruction, samplerState);

	cache.emplace(key, fptr);
	return fptr;
}

SpirvShader::ImageSampler *SpirvShader::emitSamplerFunction(ImageInstruction instruction, const Sampler &samplerState)
{
	// TODO(b/129523279): Hold a separate mutex lock for the sampler being built.
	Function<Void(Pointer<Byte>, Pointer<Byte>, Pointer<SIMD::Float>, Pointer<SIMD::Float>, Pointer<Byte>)> function;
	{
		Pointer<Byte> texture = function.Arg<0>();
		Pointer<Byte> sampler = function.Arg<1>();
		Pointer<SIMD::Float> in = function.Arg<2>();
		Pointer<SIMD::Float> out = function.Arg<3>();
		Pointer<Byte> constants = function.Arg<4>();

		SIMD::Float uvw[4] = {0, 0, 0, 0};
		SIMD::Float q = 0;
		SIMD::Float lodOrBias = 0;  // Explicit level-of-detail, or bias added to the implicit level-of-detail (depending on samplerMethod).
		Vector4f dsx = {0, 0, 0, 0};
		Vector4f dsy = {0, 0, 0, 0};
		Vector4f offset = {0, 0, 0, 0};
		SamplerFunction samplerFunction = instruction.getSamplerFunction();

		uint32_t i = 0;
		for( ; i < instruction.coordinates; i++)
		{
			uvw[i] = in[i];
		}

		if (instruction.isDref())
		{
			q = in[i];
			i++;
		}

		// TODO(b/134669567): Currently 1D textures are treated as 2D by setting the second coordinate to 0.
		// Implement optimized 1D sampling.
		if(samplerState.textureType == TEXTURE_1D)
		{
			uvw[1] = SIMD::Float(0);
		}
		else if(samplerState.textureType == TEXTURE_1D_ARRAY)
		{
			uvw[1] = SIMD::Float(0);
			uvw[2] = in[1];  // Move 1D layer coordinate to 2D layer coordinate index.
		}

		if(instruction.samplerMethod == Lod || instruction.samplerMethod == Bias || instruction.samplerMethod == Fetch)
		{
{'fraction_non_alphanumeric': 0.1260428814927783, 'fraction_numerical': 0.016327262940701533, 'mean_word_length': 4.9424307036247335, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3852120', 'n_tokens_mistral': 3964, 'n_tokens_neox': 3703, 'n_words': 819}