A bit closer to working. Shadow visible

This commit is contained in:
Henrik Rydgard 2014-03-30 00:11:01 +01:00
parent 8fba7fa98e
commit d0e65054a4
4 changed files with 82 additions and 21 deletions

View File

@ -1,4 +1,4 @@
// Copyright (c) 2014- PPSSPP Project.
// 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
@ -17,6 +17,9 @@
#include <map>
#define SHADERLOG
#include "base/logging.h"
#include "Common/Log.h"
#include "DepalettizeShader.h"
#include "GPU/GPUState.h"
@ -24,6 +27,7 @@
static const char *depalVShader =
"#version 100\n"
"precision highp float;\n"
"// Depal shader\n"
"attribute vec4 a_position;\n"
"attribute vec2 a_texcoord0;\n"
@ -82,11 +86,12 @@ void GenerateDepalShader(char *buffer, GEBufferFormat pixelFormat) {
#define WRITE p+=sprintf
WRITE(p, "#version 100\n");
WRITE(p, "varying vec2 texcoord0;\n");
WRITE(p, "precision mediump float;\n");
WRITE(p, "varying vec2 v_texcoord0;\n");
WRITE(p, "uniform sampler2D tex;\n");
WRITE(p, "uniform sampler2D pal;\n");
WRITE(p, "void main() {\n");
WRITE(p, " vec4 index = texture2D(tex);\n");
WRITE(p, " vec4 index = texture2D(tex, v_texcoord0);\n");
char lookupMethod[128] = "index.r";
char offset[128] = "";
@ -97,6 +102,7 @@ void GenerateDepalShader(char *buffer, GEBufferFormat pixelFormat) {
int shift = gstate.getClutIndexShift();
int mask = gstate.getClutIndexMask();
float multiplier = 1.0f;
// pixelformat is the format of the texture we are sampling.
switch (pixelFormat) {
case GE_FORMAT_8888:
@ -121,6 +127,7 @@ void GenerateDepalShader(char *buffer, GEBufferFormat pixelFormat) {
default:
case 12: strcpy(lookupMethod, "index.a"); break;
}
multiplier = 1.0f / 15.0f;
} else {
// Ugh
}
@ -133,6 +140,7 @@ void GenerateDepalShader(char *buffer, GEBufferFormat pixelFormat) {
default:
case 11: strcpy(lookupMethod, "index.b"); break;
}
multiplier = 1.0f / 31.0f;
} else {
// Ugh
}
@ -156,8 +164,13 @@ void GenerateDepalShader(char *buffer, GEBufferFormat pixelFormat) {
sprintf(offset, " + %.0f", (float)clutBase / 255.0f); // 256?
}
WRITE(p, " vec4 color = texture2D(pal, vec2(%s%s, 0.0));\n", lookupMethod, offset);
WRITE(p, " gl_Color = color;\n");
if (true) {
WRITE(p, " gl_FragColor = vec4(index.r);\n", lookupMethod, offset);
//WRITE(p, " gl_FragColor = vec4(index) + texture2D(pal, vec2(v_texcoord0.x, 0));\n", lookupMethod, offset);
} else {
WRITE(p, " gl_FragColor = texture2D(pal, vec2((%s * %f)%s, 0.0));\n", lookupMethod, multiplier, offset);
}
WRITE(p, "}\n");
}
@ -175,7 +188,6 @@ GLuint DepalShaderCache::GetClutTexture(const u32 clutID, u32 *rawClut) {
DepalTexture *tex = new DepalTexture();
glGenTextures(1, &tex->texture);
glActiveTexture(1);
glBindTexture(GL_TEXTURE_2D, tex->texture);
GLuint components = dstFmt == GL_UNSIGNED_SHORT_5_6_5 ? GL_RGB : GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, components, 256, 1, 0, components, dstFmt, (void *)rawClut);
@ -183,7 +195,6 @@ GLuint DepalShaderCache::GetClutTexture(const u32 clutID, u32 *rawClut) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glActiveTexture(0);
texCache_[clutID] = tex;
return tex->texture;
@ -213,13 +224,19 @@ GLuint DepalShaderCache::GetDepalettizeShader(GEBufferFormat pixelFormat) {
return shader->second->program;
}
char buffer[2048];
char *buffer = new char[2048];
GenerateDepalShader(buffer, pixelFormat);
GLuint fragShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader_, 1, &depalVShader, 0);
glCompileShader(vertexShader_);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
const char *buf = buffer;
glShaderSource(fragShader, 1, &buf, 0);
glCompileShader(fragShader);
CheckShaderCompileSuccess(fragShader, buffer);
delete[] buffer;
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader_);

View File

@ -610,6 +610,7 @@ void FramebufferManager::DrawActiveTexture(GLuint texture, float x, float y, flo
shaderManager_->DirtyLastShader(); // dirty lastShader_
}
VirtualFramebuffer *FramebufferManager::GetVFBAt(u32 addr) {
VirtualFramebuffer *match = NULL;
for (size_t i = 0; i < vfbs_.size(); ++i) {
@ -684,6 +685,10 @@ void FramebufferManager::DestroyFramebuf(VirtualFramebuffer *v) {
delete v;
}
void FramebufferManager::RebindFramebuffer() {
fbo_bind_as_render_target(currentRenderVfb_->fbo);
}
void FramebufferManager::DoSetRenderFrameBuffer() {
/*
if (useBufferedRendering_ && currentRenderVfb_) {

View File

@ -220,6 +220,8 @@ public:
bool GetCurrentDepthbuffer(GPUDebugBuffer &buffer);
bool GetCurrentStencilbuffer(GPUDebugBuffer &buffer);
void RebindFramebuffer();
private:
void CompileDraw2DProgram();
void DestroyDraw2DProgram();

View File

@ -915,30 +915,67 @@ void TextureCache::SetTextureFramebuffer(TexCacheEntry *entry) {
entry->framebuffer->usageFlags |= FB_USAGE_TEXTURE;
bool useBufferedRendering = g_Config.iRenderingMode != FB_NON_BUFFERED_MODE;
if (useBufferedRendering) {
framebufferManager_->BindFramebufferColor(entry->framebuffer);
if (entry->status & TexCacheEntry::STATUS_DEPALETTIZE) {
GLuint program = depalShaderCache_->GetDepalettizeShader(entry->framebuffer->format);
glUseProgram(program);
// Check if we can handle the current setup
GLuint clutTexture = depalShaderCache_->GetClutTexture(clutHash_, clutBufConverted_);
glActiveTexture(1);
glBindTexture(GL_TEXTURE_2D, clutTexture);
glActiveTexture(0);
if (!entry->depalFBO) {
entry->depalFBO = fbo_create(entry->framebuffer->bufferWidth, entry->framebuffer->bufferHeight, 1, false, FBO_8888);
}
fbo_bind_as_render_target(entry->depalFBO);
glViewport(0, 0, entry->framebuffer->bufferWidth, entry->framebuffer->bufferHeight);
// ...
static const float pos[12] = {
-1, -1, -1,
1, -1, -1,
1, 1, -1,
-1, 1, -1
};
static const float uv[8] = {
0, 0,
1, 0,
1, 1,
0, 1,
};
static const GLubyte indices[4] = { 0, 1, 3, 2 };
GLuint program = depalShaderCache_->GetDepalettizeShader(entry->framebuffer->format);
glUseProgram(program);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, clutTexture);
glActiveTexture(GL_TEXTURE0);
framebufferManager_->BindFramebufferColor(entry->framebuffer);
glstate.blend.disable();
glstate.colorMask.set(true, true, true, true);
glstate.scissorTest.disable();
glstate.cullFace.disable();
glstate.depthTest.disable();
glstate.viewport.set(0, 0, entry->framebuffer->bufferWidth, entry->framebuffer->bufferHeight);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 12, pos);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8, uv);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indices);
/*
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
*/
fbo_bind_color_as_texture(entry->depalFBO, 0);
glstate.Restore();
framebufferManager_->RebindFramebuffer();
} else {
framebufferManager_->BindFramebufferColor(entry->framebuffer);
}
// Keep the framebuffer alive.
entry->framebuffer->last_frame_used = gpuStats.numFlips;