2017-11-29 21:46:09 +01:00
|
|
|
#include "dxgi_presenter.h"
|
|
|
|
|
|
|
|
#include "../spirv/spirv_module.h"
|
|
|
|
|
|
|
|
namespace dxvk {
|
|
|
|
|
|
|
|
DxgiPresenter::DxgiPresenter(
|
|
|
|
const Rc<DxvkDevice>& device,
|
|
|
|
HWND window,
|
2017-12-04 22:21:02 +01:00
|
|
|
uint32_t bufferWidth,
|
|
|
|
uint32_t bufferHeight,
|
|
|
|
DXGI_FORMAT bufferFormat)
|
2017-12-01 10:08:49 +01:00
|
|
|
: m_device (device),
|
|
|
|
m_context (device->createContext()) {
|
2017-11-29 21:46:09 +01:00
|
|
|
|
|
|
|
// Create Vulkan surface for the window
|
|
|
|
HINSTANCE instance = reinterpret_cast<HINSTANCE>(
|
|
|
|
GetWindowLongPtr(window, GWLP_HINSTANCE));
|
|
|
|
|
|
|
|
m_surface = m_device->adapter()->createSurface(instance, window);
|
|
|
|
|
|
|
|
// Create swap chain for the surface
|
|
|
|
DxvkSwapchainProperties swapchainProperties;
|
2017-12-04 22:21:02 +01:00
|
|
|
swapchainProperties.preferredSurfaceFormat = this->pickFormat(bufferFormat);
|
2017-12-19 18:12:18 +01:00
|
|
|
swapchainProperties.preferredPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
|
2017-12-04 22:21:02 +01:00
|
|
|
swapchainProperties.preferredBufferSize.width = bufferWidth;
|
|
|
|
swapchainProperties.preferredBufferSize.height = bufferHeight;
|
2017-11-29 21:46:09 +01:00
|
|
|
|
|
|
|
m_swapchain = m_device->createSwapchain(
|
|
|
|
m_surface, swapchainProperties);
|
|
|
|
|
2017-12-03 20:23:26 +01:00
|
|
|
// Sampler for presentation
|
|
|
|
DxvkSamplerCreateInfo samplerInfo;
|
|
|
|
samplerInfo.magFilter = VK_FILTER_NEAREST;
|
|
|
|
samplerInfo.minFilter = VK_FILTER_NEAREST;
|
|
|
|
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
|
|
|
|
samplerInfo.mipmapLodBias = 0.0f;
|
|
|
|
samplerInfo.mipmapLodMin = 0.0f;
|
|
|
|
samplerInfo.mipmapLodMax = 0.0f;
|
|
|
|
samplerInfo.useAnisotropy = VK_FALSE;
|
|
|
|
samplerInfo.maxAnisotropy = 1.0f;
|
|
|
|
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
|
|
|
|
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
|
|
|
|
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
|
|
|
|
samplerInfo.compareToDepth = VK_FALSE;
|
|
|
|
samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
|
|
|
|
samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
|
|
|
|
samplerInfo.usePixelCoord = VK_FALSE;
|
|
|
|
|
|
|
|
m_sampler = m_device->createSampler(samplerInfo);
|
|
|
|
|
2017-11-29 21:46:09 +01:00
|
|
|
// Set up context state. The shader bindings and the
|
|
|
|
// constant state objects will never be modified.
|
2017-12-08 00:02:43 +01:00
|
|
|
DxvkInputAssemblyState iaState;
|
|
|
|
iaState.primitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
|
|
|
|
iaState.primitiveRestart = VK_FALSE;
|
|
|
|
m_context->setInputAssemblyState(iaState);
|
2017-11-29 21:46:09 +01:00
|
|
|
|
|
|
|
m_context->setInputLayout(
|
2017-12-08 00:44:58 +01:00
|
|
|
0, nullptr, 0, nullptr);
|
2017-11-29 21:46:09 +01:00
|
|
|
|
2017-12-08 00:02:43 +01:00
|
|
|
DxvkRasterizerState rsState;
|
|
|
|
rsState.enableDepthClamp = VK_FALSE;
|
|
|
|
rsState.enableDiscard = VK_FALSE;
|
|
|
|
rsState.polygonMode = VK_POLYGON_MODE_FILL;
|
|
|
|
rsState.cullMode = VK_CULL_MODE_BACK_BIT;
|
|
|
|
rsState.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
|
|
|
rsState.depthBiasEnable = VK_FALSE;
|
|
|
|
rsState.depthBiasConstant = 0.0f;
|
|
|
|
rsState.depthBiasClamp = 0.0f;
|
|
|
|
rsState.depthBiasSlope = 0.0f;
|
|
|
|
m_context->setRasterizerState(rsState);
|
2017-11-29 21:46:09 +01:00
|
|
|
|
2017-12-08 00:02:43 +01:00
|
|
|
DxvkMultisampleState msState;
|
2017-12-11 13:03:07 +01:00
|
|
|
msState.sampleMask = 0xffffffff;
|
2017-12-08 00:02:43 +01:00
|
|
|
msState.enableAlphaToCoverage = VK_FALSE;
|
|
|
|
msState.enableAlphaToOne = VK_FALSE;
|
|
|
|
msState.enableSampleShading = VK_FALSE;
|
|
|
|
msState.minSampleShading = 0.0f;
|
|
|
|
m_context->setMultisampleState(msState);
|
2017-11-29 21:46:09 +01:00
|
|
|
|
|
|
|
VkStencilOpState stencilOp;
|
|
|
|
stencilOp.failOp = VK_STENCIL_OP_KEEP;
|
|
|
|
stencilOp.passOp = VK_STENCIL_OP_KEEP;
|
|
|
|
stencilOp.depthFailOp = VK_STENCIL_OP_KEEP;
|
|
|
|
stencilOp.compareOp = VK_COMPARE_OP_ALWAYS;
|
|
|
|
stencilOp.compareMask = 0xFFFFFFFF;
|
|
|
|
stencilOp.writeMask = 0xFFFFFFFF;
|
|
|
|
stencilOp.reference = 0;
|
|
|
|
|
2017-12-08 00:02:43 +01:00
|
|
|
DxvkDepthStencilState dsState;
|
2017-12-08 01:06:48 +01:00
|
|
|
dsState.enableDepthTest = VK_FALSE;
|
|
|
|
dsState.enableDepthWrite = VK_FALSE;
|
|
|
|
dsState.enableDepthBounds = VK_FALSE;
|
|
|
|
dsState.enableStencilTest = VK_FALSE;
|
|
|
|
dsState.depthCompareOp = VK_COMPARE_OP_ALWAYS;
|
|
|
|
dsState.stencilOpFront = stencilOp;
|
|
|
|
dsState.stencilOpBack = stencilOp;
|
|
|
|
dsState.depthBoundsMin = 0.0f;
|
|
|
|
dsState.depthBoundsMax = 1.0f;
|
|
|
|
|
|
|
|
DxvkLogicOpState loState;
|
|
|
|
loState.enableLogicOp = VK_FALSE;
|
|
|
|
loState.logicOp = VK_LOGIC_OP_NO_OP;
|
|
|
|
m_context->setLogicOpState(loState);
|
|
|
|
|
|
|
|
DxvkBlendMode blendMode;
|
|
|
|
blendMode.enableBlending = VK_FALSE;
|
|
|
|
blendMode.colorSrcFactor = VK_BLEND_FACTOR_ONE;
|
|
|
|
blendMode.colorDstFactor = VK_BLEND_FACTOR_ZERO;
|
|
|
|
blendMode.colorBlendOp = VK_BLEND_OP_ADD;
|
|
|
|
blendMode.alphaSrcFactor = VK_BLEND_FACTOR_ONE;
|
|
|
|
blendMode.alphaDstFactor = VK_BLEND_FACTOR_ZERO;
|
|
|
|
blendMode.alphaBlendOp = VK_BLEND_OP_ADD;
|
|
|
|
blendMode.writeMask = VK_COLOR_COMPONENT_R_BIT
|
|
|
|
| VK_COLOR_COMPONENT_G_BIT
|
|
|
|
| VK_COLOR_COMPONENT_B_BIT
|
|
|
|
| VK_COLOR_COMPONENT_A_BIT;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < DxvkLimits::MaxNumRenderTargets; i++)
|
|
|
|
m_context->setBlendMode(i, blendMode);
|
2017-12-07 09:38:31 +01:00
|
|
|
|
|
|
|
m_context->bindShader(
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT,
|
|
|
|
this->createVertexShader());
|
|
|
|
|
|
|
|
m_context->bindShader(
|
|
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
|
|
this->createFragmentShader());
|
2017-11-29 21:46:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxgiPresenter::~DxgiPresenter() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-02 11:46:25 +01:00
|
|
|
void DxgiPresenter::initBackBuffer(const Rc<DxvkImage>& image) {
|
2017-12-05 13:00:06 +01:00
|
|
|
VkImageSubresourceRange sr;
|
|
|
|
sr.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
sr.baseMipLevel = 0;
|
|
|
|
sr.levelCount = image->info().mipLevels;
|
|
|
|
sr.baseArrayLayer = 0;
|
|
|
|
sr.layerCount = image->info().numLayers;
|
|
|
|
|
2017-12-02 11:46:25 +01:00
|
|
|
m_context->beginRecording(
|
|
|
|
m_device->createCommandList());
|
2017-12-05 13:00:06 +01:00
|
|
|
m_context->initImage(image, sr);
|
2017-12-02 11:46:25 +01:00
|
|
|
m_device->submitCommandList(
|
|
|
|
m_context->endRecording(),
|
|
|
|
nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 00:27:49 +01:00
|
|
|
void DxgiPresenter::presentImage() {
|
2017-12-01 10:08:49 +01:00
|
|
|
m_context->beginRecording(
|
|
|
|
m_device->createCommandList());
|
|
|
|
|
2017-12-12 00:27:49 +01:00
|
|
|
VkImageSubresourceLayers resolveSubresources;
|
|
|
|
resolveSubresources.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
resolveSubresources.mipLevel = 0;
|
|
|
|
resolveSubresources.baseArrayLayer = 0;
|
|
|
|
resolveSubresources.layerCount = 1;
|
|
|
|
|
|
|
|
if (m_backBufferResolve != nullptr) {
|
|
|
|
m_context->resolveImage(
|
|
|
|
m_backBufferResolve, resolveSubresources,
|
|
|
|
m_backBuffer, resolveSubresources);
|
|
|
|
}
|
|
|
|
|
2017-12-16 21:30:48 +01:00
|
|
|
const DxvkSwapSemaphores sem = m_swapchain->getSemaphorePair();
|
|
|
|
|
|
|
|
auto framebuffer = m_swapchain->getFramebuffer(sem.acquireSync);
|
2017-11-29 21:46:09 +01:00
|
|
|
auto framebufferSize = framebuffer->size();
|
2017-12-05 13:00:06 +01:00
|
|
|
|
2017-11-29 21:46:09 +01:00
|
|
|
m_context->bindFramebuffer(framebuffer);
|
|
|
|
|
|
|
|
VkViewport viewport;
|
|
|
|
viewport.x = 0.0f;
|
|
|
|
viewport.y = 0.0f;
|
|
|
|
viewport.width = static_cast<float>(framebufferSize.width);
|
|
|
|
viewport.height = static_cast<float>(framebufferSize.height);
|
|
|
|
viewport.minDepth = 0.0f;
|
|
|
|
viewport.maxDepth = 1.0f;
|
|
|
|
|
|
|
|
VkRect2D scissor;
|
|
|
|
scissor.offset.x = 0;
|
|
|
|
scissor.offset.y = 0;
|
|
|
|
scissor.extent.width = framebufferSize.width;
|
|
|
|
scissor.extent.height = framebufferSize.height;
|
|
|
|
|
|
|
|
m_context->setViewports(1, &viewport, &scissor);
|
|
|
|
|
2017-12-23 15:11:23 +01:00
|
|
|
m_context->bindResourceSampler(BindingIds::Sampler, m_sampler);
|
|
|
|
m_context->bindResourceImage (BindingIds::Texture, m_backBufferView);
|
2017-11-29 21:46:09 +01:00
|
|
|
m_context->draw(4, 1, 0, 0);
|
|
|
|
|
2017-12-01 10:08:49 +01:00
|
|
|
m_device->submitCommandList(
|
|
|
|
m_context->endRecording(),
|
2017-12-16 21:30:48 +01:00
|
|
|
sem.acquireSync, sem.presentSync);
|
2017-11-29 21:46:09 +01:00
|
|
|
|
2017-12-16 21:30:48 +01:00
|
|
|
m_swapchain->present(sem.presentSync);
|
2017-11-29 21:46:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-19 16:01:50 +01:00
|
|
|
void DxgiPresenter::updateBackBuffer(const Rc<DxvkImage>& image) {
|
2017-12-12 00:27:49 +01:00
|
|
|
// Explicitly destroy the old stuff
|
2017-12-19 16:01:50 +01:00
|
|
|
m_backBuffer = image;
|
2017-12-12 00:27:49 +01:00
|
|
|
m_backBufferResolve = nullptr;
|
|
|
|
m_backBufferView = nullptr;
|
|
|
|
|
|
|
|
// If a multisampled back buffer was requested, we also need to
|
|
|
|
// create a resolve image with otherwise identical properties.
|
|
|
|
// Multisample images cannot be sampled from.
|
2017-12-19 16:01:50 +01:00
|
|
|
if (image->info().sampleCount != VK_SAMPLE_COUNT_1_BIT) {
|
2017-12-12 00:27:49 +01:00
|
|
|
DxvkImageCreateInfo resolveInfo;
|
|
|
|
resolveInfo.type = VK_IMAGE_TYPE_2D;
|
2017-12-19 16:01:50 +01:00
|
|
|
resolveInfo.format = image->info().format;
|
2017-12-12 00:27:49 +01:00
|
|
|
resolveInfo.flags = 0;
|
|
|
|
resolveInfo.sampleCount = VK_SAMPLE_COUNT_1_BIT;
|
2017-12-19 16:01:50 +01:00
|
|
|
resolveInfo.extent = image->info().extent;
|
2017-12-12 00:27:49 +01:00
|
|
|
resolveInfo.numLayers = 1;
|
|
|
|
resolveInfo.mipLevels = 1;
|
|
|
|
resolveInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT
|
|
|
|
| VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
|
|
|
resolveInfo.stages = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
|
|
|
|
| VK_PIPELINE_STAGE_TRANSFER_BIT;
|
|
|
|
resolveInfo.access = VK_ACCESS_SHADER_READ_BIT
|
|
|
|
| VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
|
|
resolveInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
|
|
|
resolveInfo.layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
|
|
|
|
m_backBufferResolve = m_device->createImage(
|
|
|
|
resolveInfo, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an image view that allows the
|
|
|
|
// image to be bound as a shader resource.
|
|
|
|
DxvkImageViewCreateInfo viewInfo;
|
|
|
|
viewInfo.type = VK_IMAGE_VIEW_TYPE_2D;
|
2017-12-19 16:01:50 +01:00
|
|
|
viewInfo.format = image->info().format;
|
2017-12-12 00:27:49 +01:00
|
|
|
viewInfo.aspect = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
viewInfo.minLevel = 0;
|
|
|
|
viewInfo.numLevels = 1;
|
|
|
|
viewInfo.minLayer = 0;
|
|
|
|
viewInfo.numLayers = 1;
|
|
|
|
|
|
|
|
m_backBufferView = m_device->createImageView(
|
|
|
|
m_backBufferResolve != nullptr
|
|
|
|
? m_backBufferResolve
|
|
|
|
: m_backBuffer,
|
|
|
|
viewInfo);
|
|
|
|
|
|
|
|
// TODO move this elsewhere
|
|
|
|
this->initBackBuffer(m_backBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-04 22:21:02 +01:00
|
|
|
void DxgiPresenter::recreateSwapchain(
|
|
|
|
uint32_t bufferWidth,
|
|
|
|
uint32_t bufferHeight,
|
|
|
|
DXGI_FORMAT bufferFormat) {
|
|
|
|
DxvkSwapchainProperties swapchainProperties;
|
|
|
|
swapchainProperties.preferredSurfaceFormat = this->pickFormat(bufferFormat);
|
2017-12-19 18:12:18 +01:00
|
|
|
swapchainProperties.preferredPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
|
2017-12-04 22:21:02 +01:00
|
|
|
swapchainProperties.preferredBufferSize.width = bufferWidth;
|
|
|
|
swapchainProperties.preferredBufferSize.height = bufferHeight;
|
|
|
|
|
|
|
|
m_swapchain->changeProperties(swapchainProperties);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VkSurfaceFormatKHR DxgiPresenter::pickFormat(DXGI_FORMAT fmt) const {
|
|
|
|
std::vector<VkSurfaceFormatKHR> formats;
|
|
|
|
|
|
|
|
switch (fmt) {
|
|
|
|
case DXGI_FORMAT_R8G8B8A8_UNORM:
|
|
|
|
case DXGI_FORMAT_B8G8R8A8_UNORM: {
|
|
|
|
formats.push_back({ VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR });
|
|
|
|
formats.push_back({ VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR });
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
|
|
|
|
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB: {
|
|
|
|
formats.push_back({ VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR });
|
|
|
|
formats.push_back({ VK_FORMAT_B8G8R8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR });
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Logger::warn(str::format("DxgiPresenter: Unknown format: ", fmt));
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_surface->pickSurfaceFormat(
|
|
|
|
formats.size(), formats.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-29 21:46:09 +01:00
|
|
|
Rc<DxvkShader> DxgiPresenter::createVertexShader() {
|
|
|
|
SpirvModule module;
|
|
|
|
|
|
|
|
// Set up basic vertex shader capabilities
|
|
|
|
module.enableCapability(spv::CapabilityShader);
|
|
|
|
module.setMemoryModel(
|
|
|
|
spv::AddressingModelLogical,
|
|
|
|
spv::MemoryModelGLSL450);
|
|
|
|
|
|
|
|
// ID of the entry point (function)
|
|
|
|
uint32_t entryPointId = module.allocateId();
|
|
|
|
|
|
|
|
// Data type definitions
|
|
|
|
uint32_t typeVoid = module.defVoidType();
|
|
|
|
uint32_t typeU32 = module.defIntType(32, 0);
|
|
|
|
uint32_t typeF32 = module.defFloatType(32);
|
|
|
|
uint32_t typeVec2 = module.defVectorType(typeF32, 2);
|
|
|
|
uint32_t typeVec4 = module.defVectorType(typeF32, 4);
|
|
|
|
uint32_t typeVec4Arr4 = module.defArrayType(typeVec4, module.constu32(4));
|
|
|
|
uint32_t typeFn = module.defFunctionType(typeVoid, 0, nullptr);
|
|
|
|
|
|
|
|
// Pointer type definitions
|
|
|
|
uint32_t ptrInputU32 = module.defPointerType(typeU32, spv::StorageClassInput);
|
|
|
|
uint32_t ptrOutputVec2 = module.defPointerType(typeVec2, spv::StorageClassOutput);
|
|
|
|
uint32_t ptrOutputVec4 = module.defPointerType(typeVec4, spv::StorageClassOutput);
|
|
|
|
uint32_t ptrPrivateVec4 = module.defPointerType(typeVec4, spv::StorageClassPrivate);
|
|
|
|
uint32_t ptrPrivateArr4 = module.defPointerType(typeVec4Arr4, spv::StorageClassPrivate);
|
|
|
|
|
|
|
|
// Input variable: VertexIndex
|
|
|
|
uint32_t inVertexId = module.newVar(
|
|
|
|
ptrInputU32, spv::StorageClassInput);
|
|
|
|
module.decorateBuiltIn(inVertexId, spv::BuiltInVertexIndex);
|
|
|
|
|
|
|
|
// Output variable: Position
|
|
|
|
uint32_t outPosition = module.newVar(
|
|
|
|
ptrOutputVec4, spv::StorageClassOutput);
|
|
|
|
module.decorateBuiltIn(outPosition, spv::BuiltInPosition);
|
|
|
|
|
|
|
|
// Output variable: Texture coordinates
|
|
|
|
uint32_t outTexCoord = module.newVar(
|
|
|
|
ptrOutputVec2, spv::StorageClassOutput);
|
|
|
|
module.decorateLocation(outTexCoord, 0);
|
|
|
|
|
|
|
|
// Temporary variable: Vertex array
|
|
|
|
uint32_t varVertexArray = module.newVar(
|
|
|
|
ptrPrivateArr4, spv::StorageClassPrivate);
|
|
|
|
|
|
|
|
// Scalar constants
|
|
|
|
uint32_t constF32Zero = module.constf32( 0.0f);
|
|
|
|
uint32_t constF32Half = module.constf32( 0.5f);
|
|
|
|
uint32_t constF32Pos1 = module.constf32( 1.0f);
|
|
|
|
uint32_t constF32Neg1 = module.constf32(-1.0f);
|
|
|
|
|
|
|
|
// Vector constants
|
|
|
|
uint32_t constVec2HalfIds[2] = { constF32Half, constF32Half };
|
|
|
|
uint32_t constVec2Half = module.constComposite(typeVec2, 2, constVec2HalfIds);
|
|
|
|
|
|
|
|
// Construct vertex array
|
|
|
|
uint32_t vertexData[16] = {
|
|
|
|
constF32Neg1, constF32Neg1, constF32Zero, constF32Pos1,
|
|
|
|
constF32Neg1, constF32Pos1, constF32Zero, constF32Pos1,
|
|
|
|
constF32Pos1, constF32Neg1, constF32Zero, constF32Pos1,
|
|
|
|
constF32Pos1, constF32Pos1, constF32Zero, constF32Pos1,
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t vertexConstants[4] = {
|
|
|
|
module.constComposite(typeVec4, 4, vertexData + 0),
|
|
|
|
module.constComposite(typeVec4, 4, vertexData + 4),
|
|
|
|
module.constComposite(typeVec4, 4, vertexData + 8),
|
|
|
|
module.constComposite(typeVec4, 4, vertexData + 12),
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t vertexArray = module.constComposite(
|
|
|
|
typeVec4Arr4, 4, vertexConstants);
|
|
|
|
|
|
|
|
|
|
|
|
// Function header
|
|
|
|
module.functionBegin(typeVoid, entryPointId, typeFn, spv::FunctionControlMaskNone);
|
|
|
|
module.opLabel(module.allocateId());
|
|
|
|
module.opStore(varVertexArray, vertexArray);
|
|
|
|
|
|
|
|
// Load position of the current vertex
|
|
|
|
uint32_t tmpVertexId = module.opLoad(typeU32, inVertexId);
|
|
|
|
uint32_t tmpVertexPtr = module.opAccessChain(
|
|
|
|
ptrPrivateVec4, varVertexArray, 1, &tmpVertexId);
|
|
|
|
uint32_t tmpVertexPos = module.opLoad(typeVec4, tmpVertexPtr);
|
|
|
|
module.opStore(outPosition, tmpVertexPos);
|
|
|
|
|
|
|
|
// Compute texture coordinates
|
|
|
|
uint32_t swizzleIndices[2] = { 0, 1 };
|
|
|
|
uint32_t tmpTexCoord = module.opVectorShuffle(typeVec2,
|
|
|
|
tmpVertexPos, tmpVertexPos, 2, swizzleIndices);
|
|
|
|
tmpTexCoord = module.opFMul(typeVec2, tmpTexCoord, constVec2Half);
|
|
|
|
tmpTexCoord = module.opFAdd(typeVec2, tmpTexCoord, constVec2Half);
|
|
|
|
module.opStore(outTexCoord, tmpTexCoord);
|
|
|
|
|
|
|
|
module.opReturn();
|
|
|
|
module.functionEnd();
|
|
|
|
|
|
|
|
// Register function entry point
|
|
|
|
std::array<uint32_t, 3> interfaces = {
|
|
|
|
inVertexId, outPosition, outTexCoord,
|
|
|
|
};
|
|
|
|
|
|
|
|
module.addEntryPoint(entryPointId, spv::ExecutionModelVertex,
|
|
|
|
"main", interfaces.size(), interfaces.data());
|
|
|
|
|
|
|
|
// Create the actual shader module
|
|
|
|
return m_device->createShader(
|
2017-12-07 09:38:31 +01:00
|
|
|
VK_SHADER_STAGE_VERTEX_BIT,
|
|
|
|
0, nullptr, module.compile());
|
2017-11-29 21:46:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Rc<DxvkShader> DxgiPresenter::createFragmentShader() {
|
|
|
|
SpirvModule module;
|
|
|
|
|
|
|
|
module.enableCapability(spv::CapabilityShader);
|
|
|
|
module.setMemoryModel(
|
|
|
|
spv::AddressingModelLogical,
|
|
|
|
spv::MemoryModelGLSL450);
|
|
|
|
|
|
|
|
uint32_t entryPointId = module.allocateId();
|
|
|
|
|
|
|
|
// Data type definitions
|
|
|
|
uint32_t typeVoid = module.defVoidType();
|
|
|
|
uint32_t typeF32 = module.defFloatType(32);
|
|
|
|
uint32_t typeVec2 = module.defVectorType(typeF32, 2);
|
|
|
|
uint32_t typeVec4 = module.defVectorType(typeF32, 4);
|
|
|
|
uint32_t typeFn = module.defFunctionType(typeVoid, 0, nullptr);
|
2017-12-03 20:23:26 +01:00
|
|
|
uint32_t typeSampler = module.defSamplerType();
|
|
|
|
uint32_t typeTexture = module.defImageType(
|
|
|
|
typeF32, spv::Dim2D, 0, 0, 0, 1, spv::ImageFormatUnknown);
|
|
|
|
uint32_t typeSampledTex = module.defSampledImageType(typeTexture);
|
2017-11-29 21:46:09 +01:00
|
|
|
|
|
|
|
// Pointer type definitions
|
|
|
|
uint32_t ptrInputVec2 = module.defPointerType(typeVec2, spv::StorageClassInput);
|
|
|
|
uint32_t ptrOutputVec4 = module.defPointerType(typeVec4, spv::StorageClassOutput);
|
2017-12-03 20:23:26 +01:00
|
|
|
uint32_t ptrSampler = module.defPointerType(typeSampler, spv::StorageClassUniformConstant);
|
|
|
|
uint32_t ptrTexture = module.defPointerType(typeTexture, spv::StorageClassUniformConstant);
|
|
|
|
|
|
|
|
// Sampler
|
|
|
|
uint32_t rcSampler = module.newVar(ptrSampler, spv::StorageClassUniformConstant);
|
|
|
|
module.decorateDescriptorSet(rcSampler, 0);
|
|
|
|
module.decorateBinding(rcSampler, BindingIds::Sampler);
|
|
|
|
|
|
|
|
// Texture
|
|
|
|
uint32_t rcTexture = module.newVar(ptrTexture, spv::StorageClassUniformConstant);
|
|
|
|
module.decorateDescriptorSet(rcTexture, 0);
|
|
|
|
module.decorateBinding(rcTexture, BindingIds::Texture);
|
2017-11-29 21:46:09 +01:00
|
|
|
|
|
|
|
// Input variable: Texture coordinates
|
|
|
|
uint32_t inTexCoord = module.newVar(
|
|
|
|
ptrInputVec2, spv::StorageClassInput);
|
|
|
|
module.decorateLocation(inTexCoord, 0);
|
|
|
|
|
|
|
|
// Output variable: Final color
|
|
|
|
uint32_t outColor = module.newVar(
|
|
|
|
ptrOutputVec4, spv::StorageClassOutput);
|
|
|
|
module.decorateLocation(outColor, 0);
|
|
|
|
|
|
|
|
// Function header
|
|
|
|
module.functionBegin(typeVoid, entryPointId, typeFn, spv::FunctionControlMaskNone);
|
|
|
|
module.opLabel(module.allocateId());
|
|
|
|
|
|
|
|
// Load texture coordinates
|
2017-12-03 20:23:26 +01:00
|
|
|
module.opStore(outColor,
|
|
|
|
module.opImageSampleImplicitLod(
|
|
|
|
typeVec4,
|
|
|
|
module.opSampledImage(
|
|
|
|
typeSampledTex,
|
|
|
|
module.opLoad(typeTexture, rcTexture),
|
|
|
|
module.opLoad(typeSampler, rcSampler)),
|
2017-12-20 20:21:44 +01:00
|
|
|
module.opLoad(typeVec2, inTexCoord),
|
|
|
|
SpirvImageOperands()));
|
2017-11-29 21:46:09 +01:00
|
|
|
|
|
|
|
module.opReturn();
|
|
|
|
module.functionEnd();
|
|
|
|
|
|
|
|
// Register function entry point
|
|
|
|
std::array<uint32_t, 2> interfaces = { inTexCoord, outColor };
|
|
|
|
|
|
|
|
module.addEntryPoint(entryPointId, spv::ExecutionModelFragment,
|
|
|
|
"main", interfaces.size(), interfaces.data());
|
|
|
|
|
2017-12-07 09:38:31 +01:00
|
|
|
// Shader resource slots
|
|
|
|
std::array<DxvkResourceSlot, 2> resourceSlots = {{
|
|
|
|
{ BindingIds::Sampler, VK_DESCRIPTOR_TYPE_SAMPLER },
|
|
|
|
{ BindingIds::Texture, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE },
|
|
|
|
}};
|
2017-11-29 21:46:09 +01:00
|
|
|
|
|
|
|
// Create the actual shader module
|
|
|
|
return m_device->createShader(
|
2017-12-07 09:38:31 +01:00
|
|
|
VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
|
|
resourceSlots.size(),
|
|
|
|
resourceSlots.data(),
|
|
|
|
module.compile());
|
2017-12-03 00:40:58 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 21:46:09 +01:00
|
|
|
}
|