Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Common/GPU/D3D11/thin3d_d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

namespace Draw {

static constexpr int MAX_BOUND_TEXTURES = 8;

// A problem is that we can't get the D3Dcompiler.dll without using a later SDK than 7.1, which was the last that
// supported XP. A possible solution might be here:
// https://tedwvc.wordpress.com/2014/01/01/how-to-target-xp-with-vc2012-or-vc2013-and-continue-to-use-the-windows-8-x-sdk/
Expand Down Expand Up @@ -1162,6 +1164,7 @@ void D3D11DrawContext::UpdateBuffer(Buffer *buffer, const uint8_t *data, size_t
}

void D3D11DrawContext::BindVertexBuffers(int start, int count, Buffer **buffers, int *offsets) {
_assert_(start + count <= ARRAY_SIZE(nextVertexBuffers_));
// Lazy application
for (int i = 0; i < count; i++) {
D3D11Buffer *buf = (D3D11Buffer *)buffers[i];
Expand Down Expand Up @@ -1329,6 +1332,7 @@ Framebuffer *D3D11DrawContext::CreateFramebuffer(const FramebufferDesc &desc) {
void D3D11DrawContext::BindTextures(int start, int count, Texture **textures) {
// Collect the resource views from the textures.
ID3D11ShaderResourceView *views[8];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might as well change this array length to MAX_BOUND_TEXTURES since you're here poking around

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, agreed. Went ahead and cleaned up some other GL checks in Draw too along the same lines.

-[Unknown]

_assert_(start + count <= ARRAY_SIZE(views));
for (int i = 0; i < count; i++) {
D3D11Texture *tex = (D3D11Texture *)textures[i];
views[i] = tex ? tex->view : nullptr;
Expand All @@ -1338,6 +1342,7 @@ void D3D11DrawContext::BindTextures(int start, int count, Texture **textures) {

void D3D11DrawContext::BindSamplerStates(int start, int count, SamplerState **states) {
ID3D11SamplerState *samplers[8];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

_assert_(start + count <= ARRAY_SIZE(samplers));
for (int i = 0; i < count; i++) {
D3D11SamplerState *samp = (D3D11SamplerState *)states[i];
samplers[i] = samp->ss;
Expand Down Expand Up @@ -1613,6 +1618,7 @@ void D3D11DrawContext::BindFramebufferAsRenderTarget(Framebuffer *fbo, const Ren
}

void D3D11DrawContext::BindFramebufferAsTexture(Framebuffer *fbo, int binding, FBChannel channelBit, int attachment) {
_assert_(binding < MAX_BOUND_TEXTURES);
D3D11Framebuffer *fb = (D3D11Framebuffer *)fbo;
switch (channelBit) {
case FBChannel::FB_COLOR_BIT:
Expand Down
6 changes: 6 additions & 0 deletions Common/GPU/D3D9/thin3d_d3d9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

namespace Draw {

static constexpr int MAX_BOUND_TEXTURES = 8;

// Could be declared as u8
static const D3DCMPFUNC compareToD3D9[] = {
D3DCMP_NEVER,
Expand Down Expand Up @@ -525,12 +527,14 @@ class D3D9Context : public DrawContext {

void BindTextures(int start, int count, Texture **textures) override;
void BindSamplerStates(int start, int count, SamplerState **states) override {
_assert_(start + count <= MAX_BOUND_TEXTURES);
for (int i = 0; i < count; ++i) {
D3D9SamplerState *s = static_cast<D3D9SamplerState *>(states[i]);
s->Apply(device_, start + i);
}
}
void BindVertexBuffers(int start, int count, Buffer **buffers, int *offsets) override {
_assert_(start + count <= ARRAY_SIZE(curVBuffers_));
for (int i = 0; i < count; i++) {
curVBuffers_[i + start] = (D3D9Buffer *)buffers[i];
curVBufferOffsets_[i + start] = offsets ? offsets[i] : 0;
Expand Down Expand Up @@ -785,6 +789,7 @@ Texture *D3D9Context::CreateTexture(const TextureDesc &desc) {
}

void D3D9Context::BindTextures(int start, int count, Texture **textures) {
_assert_(start + count <= MAX_BOUND_TEXTURES);
for (int i = start; i < start + count; i++) {
D3D9Texture *tex = static_cast<D3D9Texture *>(textures[i - start]);
if (tex) {
Expand Down Expand Up @@ -1182,6 +1187,7 @@ uintptr_t D3D9Context::GetFramebufferAPITexture(Framebuffer *fbo, int channelBit
}

void D3D9Context::BindFramebufferAsTexture(Framebuffer *fbo, int binding, FBChannel channelBit, int color) {
_assert_(binding < MAX_BOUND_TEXTURES);
D3D9Framebuffer *fb = (D3D9Framebuffer *)fbo;
switch (channelBit) {
case FB_DEPTH_BIT:
Expand Down
12 changes: 6 additions & 6 deletions Common/GPU/OpenGL/thin3d_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,7 @@ class OpenGLContext : public DrawContext {
void GetFramebufferDimensions(Framebuffer *fbo, int *w, int *h) override;

void BindSamplerStates(int start, int count, SamplerState **states) override {
if (start + count > MAX_TEXTURE_SLOTS) {
return;
}
_assert_(start + count <= MAX_TEXTURE_SLOTS);
for (int i = 0; i < count; i++) {
int index = i + start;
boundSamplers_[index] = static_cast<OpenGLSamplerState *>(states[i]);
Expand Down Expand Up @@ -402,6 +400,7 @@ class OpenGLContext : public DrawContext {
void BindTextures(int start, int count, Texture **textures) override;
void BindPipeline(Pipeline *pipeline) override;
void BindVertexBuffers(int start, int count, Buffer **buffers, int *offsets) override {
_assert_(start + count <= ARRAY_SIZE(curVBuffers_));
for (int i = 0; i < count; i++) {
curVBuffers_[i + start] = (OpenGLBuffer *)buffers[i];
curVBufferOffsets_[i + start] = offsets ? offsets[i] : 0;
Expand Down Expand Up @@ -1070,9 +1069,7 @@ Pipeline *OpenGLContext::CreateGraphicsPipeline(const PipelineDesc &desc) {
}

void OpenGLContext::BindTextures(int start, int count, Texture **textures) {
if (start + count > MAX_TEXTURE_SLOTS) {
return;
}
_assert_(start + count <= MAX_TEXTURE_SLOTS);
for (int i = start; i < start + count; i++) {
OpenGLTexture *glTex = static_cast<OpenGLTexture *>(textures[i - start]);
if (!glTex) {
Expand Down Expand Up @@ -1151,12 +1148,14 @@ bool OpenGLPipeline::LinkShaders() {
std::vector<GLRProgram::UniformLocQuery> queries;
queries.push_back({ &samplerLocs_[0], "sampler0" });
queries.push_back({ &samplerLocs_[1], "sampler1" });
queries.push_back({ &samplerLocs_[2], "sampler2" });
for (size_t i = 0; i < dynamicUniforms.uniforms.size(); ++i) {
queries.push_back({ &dynamicUniformLocs_[i], dynamicUniforms.uniforms[i].name });
}
std::vector<GLRProgram::Initializer> initialize;
initialize.push_back({ &samplerLocs_[0], 0, 0 });
initialize.push_back({ &samplerLocs_[1], 0, 1 });
initialize.push_back({ &samplerLocs_[2], 0, 2 });
program_ = render_->CreateProgram(linkShaders, semantics, queries, initialize, false);
return true;
}
Expand Down Expand Up @@ -1354,6 +1353,7 @@ bool OpenGLContext::BlitFramebuffer(Framebuffer *fbsrc, int srcX1, int srcY1, in

void OpenGLContext::BindFramebufferAsTexture(Framebuffer *fbo, int binding, FBChannel channelBit, int color) {
OpenGLFramebuffer *fb = (OpenGLFramebuffer *)fbo;
_assert_(binding < MAX_TEXTURE_SLOTS);

GLuint aspect = 0;
if (channelBit & FB_COLOR_BIT) {
Expand Down
27 changes: 16 additions & 11 deletions Common/GPU/ShaderTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ cbuffer data : register(b0) {
float2 u_texelDelta;
float2 u_pixelDelta;
float4 u_time;
float4 u_timeDelta;
float4 u_setting;
float u_video;
};
Expand All @@ -101,6 +102,7 @@ layout (std140, set = 0, binding = 0) uniform Data {
vec2 u_texelDelta;
vec2 u_pixelDelta;
vec4 u_time;
vec4 u_timeDelta;
vec4 u_setting;
float u_video;
};
Expand All @@ -111,8 +113,9 @@ float4 gl_HalfPixel : register(c0);
float2 u_texelDelta : register(c1);
float2 u_pixelDelta : register(c2);
float4 u_time : register(c3);
float4 u_setting : register(c4);
float u_video : register(c5);
float4 u_timeDelta : register(c4);
float4 u_setting : register(c5);
float u_video : register(c6);
)";

// SPIRV-Cross' HLSL output has some deficiencies we need to work around.
Expand All @@ -135,12 +138,9 @@ std::string Postprocess(std::string code, ShaderLanguage lang, ShaderStage stage
std::string line;
std::stringstream instream(code);
while (std::getline(instream, line)) {
if (line == "uniform sampler2D sampler0;" && lang == HLSL_D3D9) {
out << "sampler2D sampler0 : register(s0);\n";
continue;
}
if (line == "uniform sampler2D sampler1;" && lang == HLSL_D3D9) {
out << "sampler2D sampler1 : register(s1);\n";
int num;
if (lang == HLSL_D3D9 && sscanf(line.c_str(), "uniform sampler2D sampler%d;", &num) == 1) {
out << "sampler2D sampler" << num << " : register(s" << num << ");\n";
continue;
}
if (line.find("uniform float") != std::string::npos) {
Expand Down Expand Up @@ -184,7 +184,9 @@ bool ConvertToVulkanGLSL(std::string *dest, TranslatedShaderMetadata *destMetada
if (line.find("uniform bool") != std::string::npos) {
continue;
} else if (line.find("uniform sampler2D") == 0) {
if (line.find("sampler0") != line.npos)
if (sscanf(line.c_str(), "uniform sampler2D sampler%d", &num) == 1)
line = StringFromFormat("layout(set = 0, binding = %d) ", num + 1) + line;
else if (line.find("sampler0") != line.npos)
line = "layout(set = 0, binding = 1) " + line;
else
line = "layout(set = 0, binding = 2) " + line;
Expand Down Expand Up @@ -299,8 +301,11 @@ bool TranslateShader(std::string *dest, ShaderLanguage destLang, const ShaderLan

int i = 0;
for (auto &resource : resources.sampled_images) {
// int location = hlsl.get_decoration(resource.id, spv::DecorationLocation);
hlsl.set_decoration(resource.id, spv::DecorationLocation, i);
const std::string &name = hlsl.get_name(resource.id);
int num;
if (sscanf(name.c_str(), "sampler%d", &num) != 1)
num = i;
hlsl.set_decoration(resource.id, spv::DecorationBinding, num);
i++;
}
spirv_cross::CompilerHLSL::Options options{};
Expand Down
6 changes: 5 additions & 1 deletion Common/GPU/Vulkan/thin3d_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class VKBuffer;
class VKSamplerState;

enum {
MAX_BOUND_TEXTURES = 2
MAX_BOUND_TEXTURES = MAX_TEXTURE_SLOTS,
};

struct DescriptorSetKey {
Expand Down Expand Up @@ -416,6 +416,7 @@ class VKContext : public DrawContext {

// TODO: Make VKBuffers proper buffers, and do a proper binding model. This is just silly.
void BindVertexBuffers(int start, int count, Buffer **buffers, int *offsets) override {
_assert_(start + count <= ARRAY_SIZE(curVBuffers_));
for (int i = 0; i < count; i++) {
curVBuffers_[i + start] = (VKBuffer *)buffers[i];
curVBufferOffsets_[i + start] = offsets ? offsets[i] : 0;
Expand Down Expand Up @@ -689,6 +690,7 @@ RasterState *VKContext::CreateRasterState(const RasterStateDesc &desc) {
}

void VKContext::BindSamplerStates(int start, int count, SamplerState **state) {
_assert_(start + count <= MAX_BOUND_TEXTURES);
for (int i = start; i < start + count; i++) {
boundSamplers_[i] = (VKSamplerState *)state[i - start];
}
Expand Down Expand Up @@ -1271,6 +1273,7 @@ void VKContext::UpdateBuffer(Buffer *buffer, const uint8_t *data, size_t offset,
}

void VKContext::BindTextures(int start, int count, Texture **textures) {
_assert_(start + count <= MAX_BOUND_TEXTURES);
for (int i = start; i < start + count; i++) {
boundTextures_[i] = static_cast<VKTexture *>(textures[i - start]);
boundImageView_[i] = boundTextures_[i] ? boundTextures_[i]->GetImageView() : GetNullTexture()->GetImageView();
Expand Down Expand Up @@ -1544,6 +1547,7 @@ void VKContext::BindFramebufferAsRenderTarget(Framebuffer *fbo, const RenderPass

void VKContext::BindFramebufferAsTexture(Framebuffer *fbo, int binding, FBChannel channelBit, int attachment) {
VKFramebuffer *fb = (VKFramebuffer *)fbo;
_assert_(binding < MAX_BOUND_TEXTURES);

// TODO: There are cases where this is okay, actually. But requires layout transitions and stuff -
// we're not ready for this.
Expand Down
2 changes: 1 addition & 1 deletion Common/GPU/thin3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ enum class Event {
PRESENTED,
};

constexpr uint32_t MAX_TEXTURE_SLOTS = 2;
constexpr uint32_t MAX_TEXTURE_SLOTS = 3;

struct FramebufferDesc {
int width;
Expand Down
16 changes: 8 additions & 8 deletions Core/Font/PGF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,14 @@ bool PGF::ReadPtr(const u8 *ptr, size_t dataSize) {

const u8 *uptr = (const u8 *)wptr;

if (uptr >= startPtr + dataSize) {
return false;
}

int shadowCharMapSize = ((header.shadowMapLength * header.shadowMapBpe + 31) & ~31) / 8;
const u8 *shadowCharMap = uptr;
uptr += shadowCharMapSize;

if (uptr < startPtr || uptr >= startPtr + dataSize) {
return false;
}

const u16_le *sptr = (const u16_le *)uptr;
if (header.revision == 3) {
charmapCompressionTable1[0].resize(rev3extra.compCharMapLength1);
Expand All @@ -257,10 +257,6 @@ bool PGF::ReadPtr(const u8 *ptr, size_t dataSize) {

uptr = (const u8 *)sptr;

if (uptr >= startPtr + dataSize) {
return false;
}

int charMapSize = ((header.charMapLength * header.charMapBpe + 31) & ~31) / 8;
const u8 *charMap = uptr;
uptr += charMapSize;
Expand All @@ -269,6 +265,10 @@ bool PGF::ReadPtr(const u8 *ptr, size_t dataSize) {
const u8 *charPointerTable = uptr;
uptr += charPointerSize;

if (uptr < startPtr || uptr >= startPtr + dataSize) {
return false;
}

// PGF Fontdata.
u32 fontDataOffset = (u32)(uptr - startPtr);

Expand Down
1 change: 1 addition & 0 deletions GPU/Common/PostShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ void LoadPostShaderInfo(const std::vector<Path> &directories) {
section.Get("Upscaling", &info.isUpscalingFilter, false);
section.Get("SSAA", &info.SSAAFilterLevel, 0);
section.Get("60fps", &info.requires60fps, false);
section.Get("UsePreviousFrame", &info.usePreviousFrame, false);

if (info.parent == "Off")
info.parent = "";
Expand Down
2 changes: 2 additions & 0 deletions GPU/Common/PostShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ struct ShaderInfo {
int SSAAFilterLevel;
// Force constant/max refresh for animated filters
bool requires60fps;
// Takes previous frame as input (for blending effects.)
bool usePreviousFrame;

struct Setting {
std::string name;
Expand Down
Loading