Skip to content

Commit

Permalink
add GPU_GX2, doesn't work yet ...
Browse files Browse the repository at this point in the history
  • Loading branch information
aliaspider committed May 11, 2018
1 parent 7ae8f28 commit 6227e4f
Show file tree
Hide file tree
Showing 42 changed files with 4,931 additions and 159 deletions.
57 changes: 42 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1375,21 +1375,39 @@ set(GPU_D3D11
GPU/D3D11/VertexShaderGeneratorD3D11.h
)

if(NOT WIIU)
set(GPU_GX2
GPU/GX2/DepalettizeShaderGX2.cpp
GPU/GX2/DepalettizeShaderGX2.h
GPU/GX2/DrawEngineGX2.cpp
GPU/GX2/DrawEngineGX2.h
GPU/GX2/FragmentShaderGeneratorGX2.cpp
GPU/GX2/FragmentShaderGeneratorGX2.h
GPU/GX2/FramebufferManagerGX2.cpp
GPU/GX2/FramebufferManagerGX2.h
GPU/GX2/GPU_GX2.cpp
GPU/GX2/GPU_GX2.h
GPU/GX2/GX2StaticShaders.c
GPU/GX2/GX2StaticShaders.h
GPU/GX2/GX2Util.cpp
GPU/GX2/GX2Util.h
GPU/GX2/ShaderManagerGX2.cpp
GPU/GX2/ShaderManagerGX2.h
GPU/GX2/StateMappingGX2.cpp
GPU/GX2/StateMappingGX2.h
GPU/GX2/StencilBufferGX2.cpp
GPU/GX2/TextureCacheGX2.cpp
GPU/GX2/TextureCacheGX2.h
GPU/GX2/TextureScalerGX2.cpp
GPU/GX2/TextureScalerGX2.h
GPU/GX2/VertexShaderGeneratorGX2.cpp
GPU/GX2/VertexShaderGeneratorGX2.h
)

if(WIIU)
set(GPU_IMPLS ${GPU_GX2})
else()
# We build Vulkan even on Apple to avoid annoying build differences.
set(GPU_IMPLS ${GPU_GLES} ${GPU_VULKAN})
set(GPU_SHADERS
GPU/Common/DepalettizeShaderCommon.cpp
GPU/Common/DepalettizeShaderCommon.h
GPU/Common/ShaderId.cpp
GPU/Common/ShaderId.h
GPU/Common/ShaderUniforms.cpp
GPU/Common/ShaderUniforms.h
GPU/Common/ShaderCommon.cpp
GPU/Common/ShaderCommon.h
GPU/Common/ShaderTranslation.cpp
GPU/Common/ShaderTranslation.h
)
set(GPU_IMPLS ${GPU_GLES} ${GPU_VULKAN} )
endif()
if(WIN32)
list(APPEND GPU_IMPLS ${GPU_D3D9} ${GPU_D3D11})
Expand All @@ -1401,7 +1419,6 @@ endif()
set(GPU_SOURCES
${GPU_IMPLS}
${GPU_NEON}
${GPU_SHADERS}
GPU/Common/FramebufferCommon.cpp
GPU/Common/FramebufferCommon.h
GPU/Common/GPUDebugInterface.cpp
Expand All @@ -1418,6 +1435,16 @@ set(GPU_SOURCES
GPU/Common/SoftwareTransformCommon.h
GPU/Common/VertexDecoderCommon.cpp
GPU/Common/VertexDecoderCommon.h
GPU/Common/DepalettizeShaderCommon.cpp
GPU/Common/DepalettizeShaderCommon.h
GPU/Common/ShaderId.cpp
GPU/Common/ShaderId.h
GPU/Common/ShaderCommon.cpp
GPU/Common/ShaderCommon.h
GPU/Common/ShaderUniforms.cpp
GPU/Common/ShaderUniforms.h
GPU/Common/ShaderTranslation.cpp
GPU/Common/ShaderTranslation.h
GPU/Common/TransformCommon.cpp
GPU/Common/TransformCommon.h
GPU/Common/IndexGenerator.cpp
Expand Down
6 changes: 3 additions & 3 deletions GPU/Common/ShaderTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ static EShLanguage GetLanguage(const Draw::ShaderStage stage) {

void ShaderTranslationInit() {
// TODO: We have TLS issues on UWP
#if !PPSSPP_PLATFORM(UWP)
#if !PPSSPP_PLATFORM(UWP) && !PPSSPP_PLATFORM(WIIU)
glslang::InitializeProcess();
#endif
}
void ShaderTranslationShutdown() {
#if !PPSSPP_PLATFORM(UWP)
#if !PPSSPP_PLATFORM(UWP) && !PPSSPP_PLATFORM(WIIU)
glslang::FinalizeProcess();
#endif
}
Expand Down Expand Up @@ -197,7 +197,7 @@ bool TranslateShader(std::string *dest, ShaderLanguage destLang, TranslatedShade
*errorMessage = "";
}

#if PPSSPP_PLATFORM(UWP)
#if PPSSPP_PLATFORM(UWP) || PPSSPP_PLATFORM(WIIU)
return false;
#endif

Expand Down
18 changes: 13 additions & 5 deletions GPU/GPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@
#include "GPU/GPU.h"
#include "GPU/GPUInterface.h"

#include "GPU/Null/NullGpu.h"
#include "GPU/Software/SoftGpu.h"

#if PPSSPP_PLATFORM(UWP)
#include "GPU/D3D11/GPU_D3D11.h"
#elif PPSSPP_PLATFORM(WIIU)
#include "GPU/GX2/GPU_GX2.h"
#else
#ifndef __wiiu__
#include "GPU/GLES/GPU_GLES.h"
#endif

#ifndef NO_VULKAN
#include "GPU/Vulkan/GPU_Vulkan.h"
#endif
#include "GPU/Null/NullGpu.h"
#include "GPU/Software/SoftGpu.h"

#if defined(_WIN32)
#include "GPU/Directx9/GPU_DX9.h"
Expand Down Expand Up @@ -73,7 +74,7 @@ bool GPU_Init(GraphicsContext *ctx, Draw::DrawContext *draw) {
SetGPU(new NullGPU());
break;
case GPUCORE_GLES:
#ifndef __wiiu__
#if !PPSSPP_PLATFORM(WIIU)
SetGPU(new GPU_GLES(ctx, draw));
break;
#else
Expand Down Expand Up @@ -105,6 +106,13 @@ bool GPU_Init(GraphicsContext *ctx, Draw::DrawContext *draw) {
SetGPU(new GPU_Vulkan(ctx, draw));
#endif
break;
case GPUCORE_GX2:
#if PPSSPP_PLATFORM(WIIU)
SetGPU(new GPU_GX2(ctx, draw));
break;
#else
return false;
#endif
}

return gpu != NULL;
Expand Down
194 changes: 194 additions & 0 deletions GPU/GX2/DepalettizeShaderGX2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// Copyright (c) 2014- PPSSPP Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.

#include <map>
#include <wiiu/gx2/shaders.h>

#include "base/basictypes.h"
#include "base/logging.h"
#include "Common/Log.h"
#include "Common/ColorConv.h"
#include "Common/StringUtils.h"
#include "Core/Reporting.h"
#include "GPU/GX2/TextureCacheGX2.h"
#include "GPU/GX2/DepalettizeShaderGX2.h"
#include "GPU/GX2/GX2Util.h"
#include "GPU/Common/DepalettizeShaderCommon.h"

DepalShaderCacheGX2::DepalShaderCacheGX2(Draw::DrawContext *draw) {
static const GX2AttribStream depalAttribStream[] = {
{ 0, 0, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32_32, GX2_ATTRIB_INDEX_PER_VERTEX, 0, GX2_COMP_SEL(_x, _y, _z, _1), GX2_ENDIAN_SWAP_DEFAULT },
{ 1, 0, 12, GX2_ATTRIB_FORMAT_FLOAT_32_32, GX2_ATTRIB_INDEX_PER_VERTEX, 0, GX2_COMP_SEL(_x, _y, _0, _0), GX2_ENDIAN_SWAP_DEFAULT },
};
fetchShader_.size = GX2CalcFetchShaderSizeEx(countof(depalAttribStream), GX2_FETCH_SHADER_TESSELLATION_NONE, GX2_TESSELLATION_MODE_DISCRETE);
fetchShader_.program = (u8 *)MEM2_alloc(fetchShader_.size, GX2_SHADER_ALIGNMENT);
GX2InitFetchShaderEx(&fetchShader_, fetchShader_.program, countof(depalAttribStream), depalAttribStream, GX2_FETCH_SHADER_TESSELLATION_NONE, GX2_TESSELLATION_MODE_DISCRETE);
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_SHADER, fetchShader_.program, fetchShader_.size);

context_ = (GX2ContextState *)draw->GetNativeObject(Draw::NativeObject::CONTEXT);
}

DepalShaderCacheGX2::~DepalShaderCacheGX2() {
Clear();
MEM2_free(fetchShader_.program);
}

GX2Texture *DepalShaderCacheGX2::GetClutTexture(GEPaletteFormat clutFormat, const u32 clutHash, u32 *rawClut, bool expandTo32bit) {
const u32 clutId = GetClutID(clutFormat, clutHash);

auto oldtex = texCache_.find(clutId);
if (oldtex != texCache_.end()) {
oldtex->second->lastFrame = gpuStats.numFlips;
return oldtex->second;
}

int texturePixels = clutFormat == GE_CMODE_32BIT_ABGR8888 ? 256 : 512;
int bpp = clutFormat == GE_CMODE_32BIT_ABGR8888 ? 4 : 2;
GX2SurfaceFormat dstFmt;
u32 *expanded = nullptr;
if (expandTo32bit && clutFormat != GE_CMODE_32BIT_ABGR8888) {
expanded = new u32[texturePixels];
switch (clutFormat) {
case GE_CMODE_16BIT_ABGR4444:
ConvertRGBA4444ToRGBA8888(expanded, (const u16 *)rawClut, texturePixels);
break;
case GE_CMODE_16BIT_ABGR5551:
ConvertRGBA5551ToRGBA8888(expanded, (const u16 *)rawClut, texturePixels);
break;
case GE_CMODE_16BIT_BGR5650:
ConvertRGBA565ToRGBA8888(expanded, (const u16 *)rawClut, texturePixels);
break;
}
rawClut = expanded;
dstFmt = GX2_SURFACE_FORMAT_UNORM_R8_G8_B8_A8;
bpp = 4;
} else {
dstFmt = GetClutDestFormatGX2(clutFormat);
}

DepalTextureGX2 *tex = new DepalTextureGX2();

tex->surface.width = texturePixels;
tex->surface.height = 1;
tex->surface.depth = 1;
tex->surface.dim = GX2_SURFACE_DIM_TEXTURE_1D;
tex->surface.tileMode = GX2_TILE_MODE_LINEAR_ALIGNED;
tex->surface.use = GX2_SURFACE_USE_TEXTURE;
tex->viewNumSlices = 1;

tex->surface.format = dstFmt;
tex->compMap = GX2_COMP_SEL(_a, _r, _g, _b);

GX2CalcSurfaceSizeAndAlignment(&tex->surface);
GX2InitTextureRegs(tex);

tex->surface.image = MEM2_alloc(tex->surface.imageSize, tex->surface.alignment);
_assert_(tex->surface.image);

if (bpp == 2) {
const u16 *src = (const u16 *)rawClut;
u16 *dst = (u16 *)tex->surface.image;
while (src < (u16 *)rawClut + texturePixels) {
*dst++ = __builtin_bswap16(*src++);
}
} else {
const u32 *src = (const u32 *)rawClut;
u32 *dst = (u32 *)tex->surface.image;
while (src < (u32 *)rawClut + texturePixels) {
*dst++ = __builtin_bswap32(*src++);
}
}
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_TEXTURE, tex->surface.image, tex->surface.imageSize);

tex->lastFrame = gpuStats.numFlips;
texCache_[clutId] = tex;

if (expandTo32bit) {
delete[] expanded;
}
return tex;
}

void DepalShaderCacheGX2::Clear() {
for (auto shader = cache_.begin(); shader != cache_.end(); ++shader) {
delete shader->second;
}
cache_.clear();

for (auto tex = texCache_.begin(); tex != texCache_.end(); ++tex) {
delete tex->second;
}
texCache_.clear();
}

void DepalShaderCacheGX2::Decimate() {
for (auto tex = texCache_.begin(); tex != texCache_.end();) {
if (tex->second->lastFrame + DEPAL_TEXTURE_OLD_AGE < gpuStats.numFlips) {
delete tex->second;
texCache_.erase(tex++);
} else {
++tex;
}
}
}

extern "C" GX2PixelShader GX2_fsCol;
DepalShaderCacheGX2::DepalShaderGX2::DepalShaderGX2(GEBufferFormat pixelFormat) : GX2PixelShader(GX2_fsCol) {
// TODO;
program = (u8*)MEM2_alloc(size, GX2_SHADER_ALIGNMENT);
memcpy(program, GX2_fsCol.program, size);
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_SHADER, program, size);
}

GX2PixelShader *DepalShaderCacheGX2::GetDepalettizePixelShader(u32 clutMode, GEBufferFormat pixelFormat) {
u32 id = GenerateShaderID(clutMode, pixelFormat);

auto shader = cache_.find(id);
if (shader != cache_.end()) {
return shader->second;
}

DepalShaderGX2 *depal = new DepalShaderGX2(pixelFormat);
cache_[id] = depal;

return depal;
}

std::vector<std::string> DepalShaderCacheGX2::DebugGetShaderIDs(DebugShaderType type) {
std::vector<std::string> ids;
for (auto &iter : cache_) {
ids.push_back(StringFromFormat("%08x", iter.first));
}
return ids;
}

std::string DepalShaderCacheGX2::DebugGetShaderString(std::string idstr, DebugShaderType type, DebugShaderStringType stringType) {
u32 id;
sscanf(idstr.c_str(), "%08x", &id);
auto iter = cache_.find(id);
if (iter == cache_.end())
return "";
switch (stringType) {
case SHADER_STRING_SHORT_DESC:
return idstr;
case SHADER_STRING_SOURCE_CODE:
// TODO: disassemble shader
return "N/A";
default:
return "";
}
}
Loading

0 comments on commit 6227e4f

Please sign in to comment.