Skip to content

Commit

Permalink
Merge pull request #47 from UnsafePointer/gpu_transparency
Browse files Browse the repository at this point in the history
Implement transparency in OpenGL renderer
  • Loading branch information
UnsafePointer authored May 18, 2020
2 parents 0f7b426 + 20b7a3c commit 187b669
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 138 deletions.
12 changes: 11 additions & 1 deletion glsl/fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#version 450 core

uniform sampler2D frame_buffer_texture;
uniform uint draw_transparent_texture_blend;

in vec3 color;
flat in uint fragment_transparent;
in vec2 fragment_texture_point;
flat in uint fragment_texture_blend_mode;
flat in uvec2 fragment_texture_page;
Expand Down Expand Up @@ -34,7 +36,7 @@ vec4 get_pixel_from_vram(uint x, uint y) {

void main() {
if (fragment_texture_blend_mode == BLEND_MODE_NO_TEXTURE) {
fragment_color = vec4(color, 1.0);
fragment_color = vec4(color, .0);
} else {
uint pixel_per_hw = 1U << fragment_texture_depth_shift;

Expand Down Expand Up @@ -66,6 +68,14 @@ void main() {
discard;
}

// Bit 15
uint transparency_flag = uint(floor(texel.a + 0.5));
uint texel_semi_transparent = transparency_flag & fragment_transparent;

if (texel_semi_transparent != draw_transparent_texture_blend) {
discard;
}

vec4 out_color;

if (fragment_texture_blend_mode == BLEND_MODE_RAW_TEXTURE) {
Expand Down
10 changes: 7 additions & 3 deletions glsl/vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#version 450 core

in ivec2 vertex_point;
in ivec3 vertex_point;
in uvec3 vertex_color;
in uint transparent;
in ivec2 texture_point;
in uint texture_blend_mode;
in uvec2 texture_page;
in uint texture_depth_shift;
in uvec2 clut;

out vec3 color;
flat out uint fragment_transparent;
out vec2 fragment_texture_point;
flat out uint fragment_texture_blend_mode;
flat out uvec2 fragment_texture_page;
Expand All @@ -18,16 +20,18 @@ flat out uvec2 fragment_clut;
uniform ivec2 offset;

void main() {
ivec2 position = vertex_point + offset;
ivec2 position = vertex_point.xy + offset;

float x_pos = (float(position.x) / 512) - 1.0;
float y_pos = 1.0 - (float(position.y) / 256);
float z_pos = 1.0 - (float(vertex_point.z) / 32768);

gl_Position.xyzw = vec4(x_pos, y_pos, 0.0, 1.0);
gl_Position.xyzw = vec4(x_pos, y_pos, z_pos, 1.0);
color = vec3(float(vertex_color.r) / 255, float(vertex_color.g) / 255, float(vertex_color.b) / 255);
fragment_texture_point = vec2(texture_point);
fragment_texture_blend_mode = texture_blend_mode;
fragment_texture_page = texture_page;
fragment_texture_depth_shift = texture_depth_shift;
fragment_clut = clut;
fragment_transparent = transparent;
}
4 changes: 2 additions & 2 deletions include/GPU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ GP1(07h) - Vertical Display range (on Screen)
void executeGp0(uint32_t value);
void step(uint32_t cycles);
Dimensions getResolution();
Point getDisplayAreaStart();
Point2D getDisplayAreaStart();
Dimensions getDrawingAreaSize();
Point getDrawingAreaTopLeft();
Point2D getDrawingAreaTopLeft();
void toggleRenderPolygonOneByOne();
};
21 changes: 13 additions & 8 deletions include/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class GPU;
class Renderer {
Logger logger;
GLuint offsetUniform;
GLuint drawTransparentTextureBlendUniform;

std::vector<Vertex> opaqueVertices;
std::vector<Vertex> transparentVertices;

std::unique_ptr<Window> &mainWindow;

Expand All @@ -24,39 +28,40 @@ class Renderer {

std::unique_ptr<Texture> loadImageTexture;
std::unique_ptr<RendererProgram> textureRendererProgram;
std::unique_ptr<RendererBuffer<Point>> textureBuffer;
std::unique_ptr<RendererBuffer<Point2D>> textureBuffer;

std::unique_ptr<Texture> screenTexture;
std::unique_ptr<RendererProgram> screenRendererProgram;
std::unique_ptr<RendererBuffer<Pixel>> screenBuffer;

GLenum mode;
bool resizeToFitFramebuffer;
Point displayAreaStart;
Point2D displayAreaStart;
Dimensions screenResolution;
Point drawingAreaTopLeft;
Point2D drawingAreaTopLeft;
Dimensions drawingAreaSize;
bool renderPolygonOneByOne;
uint32_t orderingIndex;

void checkRenderPolygonOneByOne();
void checkForceDraw(unsigned int verticesToRender, GLenum newMode);
void forceDraw();
void applyScissor();
void insertVertices(std::vector<Vertex> vertices, bool opaque, TextureBlendMode textureBlendMode);
public:
Renderer(std::unique_ptr<Window> &mainWindow, GPU *gpu);
~Renderer();

void pushLine(std::vector<Vertex> vertices);
void pushPolygon(std::vector<Vertex> vertices);
void pushLine(std::vector<Vertex> vertices, bool opaque);
void pushPolygon(std::vector<Vertex> vertices, bool opaque, TextureBlendMode textureBlendMode);
void setDrawingOffset(int16_t x, int16_t y);
void prepareFrame();
void renderFrame();
void finalizeFrame();
void updateWindowTitle(std::string title);
void loadImage(std::unique_ptr<GPUImageBuffer> &imageBuffer);
void resetMainWindow();
void setDisplayAreaSart(Point point);
void setDisplayAreaSart(Point2D point);
void setScreenResolution(Dimensions dimensions);
void setDrawingArea(Point topLeft, Dimensions size);
void setDrawingArea(Point2D topLeft, Dimensions size);
void toggleRenderPolygonOneByOne();
};
35 changes: 22 additions & 13 deletions include/Vertex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ union Dimensions {
Dimensions(uint32_t width, uint32_t height) : width(width), height(height) {}
};

struct Point {
struct Point2D {
GLshort x, y;

Point();
Point(GLshort x, GLshort y);
Point(uint32_t position);
static Point forTexturePosition(uint16_t position);
static Point forTexturePage(uint32_t texturePage);
static Point forClut(uint16_t clutData);
Point2D();
Point2D(GLshort x, GLshort y);
Point2D(uint32_t position);
static Point2D forTexturePosition(uint16_t position);
static Point2D forTexturePage(uint32_t texturePage);
static Point2D forClut(uint16_t clutData);
};

struct Point3D {
GLshort x, y, z;

Point3D();
Point3D(GLshort x, GLshort y, GLshort z);
Point3D(uint32_t position);
};

struct Color {
Expand All @@ -36,16 +44,17 @@ enum TextureBlendMode {
};

struct Vertex {
Point point;
Point3D point;
Color color;
Point texturePosition;
GLuint transparent;
Point2D texturePosition;
GLuint textureBlendMode;
Point texturePage;
Point2D texturePage;
GLuint textureDepthShift;
Point clut;
Point2D clut;

Vertex(Point point, Color color);
Vertex(Point point, Color color, Point texturePosition, TextureBlendMode textureBlendMode, Point texturePage, GLuint textureDepthShift, Point clut);
Vertex(Point3D point, Color color, GLuint opaque);
Vertex(Point3D point, Color color, GLuint opaque, Point2D texturePosition, TextureBlendMode textureBlendMode, Point2D texturePage, GLuint textureDepthShift, Point2D clut);
~Vertex();
};

Expand Down
Loading

0 comments on commit 187b669

Please sign in to comment.