Compare commits

..

No commits in common. "f0a508386f4ccd382df8831b7907c3f6685c2453" and "4c57f0b5f4a667005acf6008691d43cc21bf2790" have entirely different histories.

5 changed files with 95 additions and 37 deletions

View File

@ -33,6 +33,7 @@ pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_ctx: SharedConte
} }
pub fn deinit(self: *@This(), allocator: Allocator) void { pub fn deinit(self: *@This(), allocator: Allocator) void {
allocator.destroy(self.main);
allocator.destroy(self.wram); allocator.destroy(self.wram);
} }

View File

@ -40,6 +40,7 @@ pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_ctx: SharedConte
pub fn deinit(self: *@This(), allocator: Allocator) void { pub fn deinit(self: *@This(), allocator: Allocator) void {
self.ppu.deinit(allocator); self.ppu.deinit(allocator);
allocator.destroy(self.main);
allocator.destroy(self.vram1); allocator.destroy(self.vram1);
} }

View File

@ -23,7 +23,7 @@ const nds_height = ppu.screen_height;
pub const Dimensions = struct { width: u32, height: u32 }; pub const Dimensions = struct { width: u32, height: u32 };
const window_title = "Turbo"; const window_title = "Turbo (Name Pending)";
pub const Ui = struct { pub const Ui = struct {
const Self = @This(); const Self = @This();
@ -39,7 +39,7 @@ pub const Ui = struct {
if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO) < 0) panic(); if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO) < 0) panic();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GL_CONTEXT_PROFILE_CORE) < 0) panic(); if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GL_CONTEXT_PROFILE_CORE) < 0) panic();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0) panic(); if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0) panic();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_MINOR_VERSION, 3) < 0) panic(); if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0) panic();
const window = SDL.SDL_CreateWindow( const window = SDL.SDL_CreateWindow(
window_title, window_title,
@ -82,27 +82,28 @@ pub const Ui = struct {
SDL.SDL_Quit(); SDL.SDL_Quit();
} }
fn glGetProcAddress(_: SDL.SDL_GLContext, proc: [:0]const u8) ?*anyopaque { fn glGetProcAddress(ctx: SDL.SDL_GLContext, proc: [:0]const u8) ?*anyopaque {
_ = ctx;
return SDL.SDL_GL_GetProcAddress(proc.ptr); return SDL.SDL_GL_GetProcAddress(proc.ptr);
} }
pub fn run(self: *Self, nds7_group: nds7.Group, nds9_group: nds9.Group) !void { pub fn run(self: *Self, nds7_group: nds7.Group, nds9_group: nds9.Group) !void {
// TODO: Sort this out please // TODO: Sort this out please
const vao_id = opengl_impl.vao(); const objects = opengl_impl.createObjects();
defer gl.deleteVertexArrays(1, &[_]GLuint{vao_id}); defer gl.deleteBuffers(3, &[_]GLuint{ objects.vao, objects.vbo, objects.ebo });
const top_tex = opengl_impl.screenTex(nds9_group.bus.ppu.fb.top(.front)); const top_tex = opengl_impl.createScreenTexture(nds9_group.bus.ppu.fb.top(.front));
const btm_tex = opengl_impl.screenTex(nds9_group.bus.ppu.fb.btm(.front)); const btm_tex = opengl_impl.createScreenTexture(nds9_group.bus.ppu.fb.btm(.front));
const top_out_tex = opengl_impl.outTex(); const top_out_tex = opengl_impl.createOutputTexture();
const btm_out_tex = opengl_impl.outTex(); const btm_out_tex = opengl_impl.createOutputTexture();
defer gl.deleteTextures(4, &[_]GLuint{ top_tex, top_out_tex, btm_tex, btm_out_tex }); defer gl.deleteTextures(4, &[_]GLuint{ top_tex, top_out_tex, btm_tex, btm_out_tex });
const top_fbo = try opengl_impl.frameBuffer(top_out_tex); const top_fbo = try opengl_impl.createFrameBuffer(top_out_tex);
const btm_fbo = try opengl_impl.frameBuffer(btm_out_tex); const btm_fbo = try opengl_impl.createFrameBuffer(btm_out_tex);
defer gl.deleteFramebuffers(2, &[_]GLuint{ top_fbo, btm_fbo }); defer gl.deleteFramebuffers(2, &[_]GLuint{ top_fbo, btm_fbo });
const prog_id = try opengl_impl.program(); const prog_id = try opengl_impl.compileShaders();
defer gl.deleteProgram(prog_id); defer gl.deleteProgram(prog_id);
var event: SDL.SDL_Event = undefined; var event: SDL.SDL_Event = undefined;
@ -174,7 +175,7 @@ pub const Ui = struct {
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0); defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0, nds_width, nds_height); gl.viewport(0, 0, nds_width, nds_height);
opengl_impl.drawScreen(top_tex, prog_id, vao_id, nds9_group.bus.ppu.fb.top(.front)); opengl_impl.drawScreenTexture(top_tex, prog_id, objects, nds9_group.bus.ppu.fb.top(.front));
} }
{ {
@ -182,7 +183,7 @@ pub const Ui = struct {
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0); defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0, nds_width, nds_height); gl.viewport(0, 0, nds_width, nds_height);
opengl_impl.drawScreen(btm_tex, prog_id, vao_id, nds9_group.bus.ppu.fb.btm(.front)); opengl_impl.drawScreenTexture(btm_tex, prog_id, objects, nds9_group.bus.ppu.fb.btm(.front));
} }
const zgui_redraw = imgui.draw(&self.state, top_out_tex, btm_out_tex, nds9_group.cpu); const zgui_redraw = imgui.draw(&self.state, top_out_tex, btm_out_tex, nds9_group.cpu);
@ -212,24 +213,44 @@ fn panic() noreturn {
} }
const opengl_impl = struct { const opengl_impl = struct {
fn drawScreen(tex_id: GLuint, prog_id: GLuint, vao_id: GLuint, buf: []const u8) void { // zig fmt: off
const vertices: [32]f32 = [_]f32{
// Positions // Colours // Texture Coords
1.0, -1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, // Top Right
1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, // Bottom Right
-1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // Bottom Left
-1.0, -1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, // Top Left
};
const indices: [6]u32 = [_]u32{
0, 1, 3, // First Triangle
1, 2, 3, // Second Triangle
};
// zig fmt: on
const Objects = struct { vao: GLuint, vbo: GLuint, ebo: GLuint };
fn drawScreenTexture(tex_id: GLuint, prog_id: GLuint, ids: Objects, buf: []const u8) void {
gl.bindTexture(gl.TEXTURE_2D, tex_id); gl.bindTexture(gl.TEXTURE_2D, tex_id);
defer gl.bindTexture(gl.TEXTURE_2D, 0); defer gl.bindTexture(gl.TEXTURE_2D, 0);
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, nds_width, nds_height, gl.RGBA, gl.UNSIGNED_INT_8_8_8_8, buf.ptr); gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, nds_width, nds_height, gl.RGBA, gl.UNSIGNED_INT_8_8_8_8, buf.ptr);
// Bind VAO // Bind VAO, EBO. VBO not bound
gl.bindVertexArray(vao_id); gl.bindVertexArray(ids.vao); // VAO
defer gl.bindVertexArray(0); defer gl.bindVertexArray(0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ids.ebo); // EBO
defer gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0);
// Use compiled frag + vertex shader // Use compiled frag + vertex shader
gl.useProgram(prog_id); gl.useProgram(prog_id);
defer gl.useProgram(0); defer gl.useProgram(0);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 3); gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, null);
} }
fn program() !GLuint { fn compileShaders() !GLuint {
const vert_shader = @embedFile("shader/pixelbuf.vert"); const vert_shader = @embedFile("shader/pixelbuf.vert");
const frag_shader = @embedFile("shader/pixelbuf.frag"); const frag_shader = @embedFile("shader/pixelbuf.frag");
@ -249,22 +270,50 @@ const opengl_impl = struct {
if (!shader.didCompile(fs)) return error.FragmentCompileError; if (!shader.didCompile(fs)) return error.FragmentCompileError;
const prog = gl.createProgram(); const program = gl.createProgram();
gl.attachShader(prog, vs); gl.attachShader(program, vs);
gl.attachShader(prog, fs); gl.attachShader(program, fs);
gl.linkProgram(prog); gl.linkProgram(program);
return prog; return program;
} }
fn vao() GLuint { // Returns the VAO ID since it's used in run()
fn createObjects() Objects {
var vao_id: GLuint = undefined; var vao_id: GLuint = undefined;
gl.genVertexArrays(1, &vao_id); var vbo_id: GLuint = undefined;
var ebo_id: GLuint = undefined;
return vao_id; gl.genVertexArrays(1, &vao_id);
gl.genBuffers(1, &vbo_id);
gl.genBuffers(1, &ebo_id);
gl.bindVertexArray(vao_id);
defer gl.bindVertexArray(0);
gl.bindBuffer(gl.ARRAY_BUFFER, vbo_id);
defer gl.bindBuffer(gl.ARRAY_BUFFER, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo_id);
defer gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0);
gl.bufferData(gl.ARRAY_BUFFER, @sizeOf(@TypeOf(vertices)), &vertices, gl.STATIC_DRAW);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, @sizeOf(@TypeOf(indices)), &indices, gl.STATIC_DRAW);
// Position
gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 8 * @sizeOf(f32), null); // lmao
gl.enableVertexAttribArray(0);
// Colour
gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 8 * @sizeOf(f32), @ptrFromInt((3 * @sizeOf(f32))));
gl.enableVertexAttribArray(1);
// Texture Coord
gl.vertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, 8 * @sizeOf(f32), @ptrFromInt((6 * @sizeOf(f32))));
gl.enableVertexAttribArray(2);
return .{ .vao = vao_id, .vbo = vbo_id, .ebo = ebo_id };
} }
fn screenTex(buf: []const u8) GLuint { fn createScreenTexture(buf: []const u8) GLuint {
var tex_id: GLuint = undefined; var tex_id: GLuint = undefined;
gl.genTextures(1, &tex_id); gl.genTextures(1, &tex_id);
@ -279,7 +328,7 @@ const opengl_impl = struct {
return tex_id; return tex_id;
} }
fn outTex() GLuint { fn createOutputTexture() GLuint {
var tex_id: GLuint = undefined; var tex_id: GLuint = undefined;
gl.genTextures(1, &tex_id); gl.genTextures(1, &tex_id);
@ -294,7 +343,7 @@ const opengl_impl = struct {
return tex_id; return tex_id;
} }
fn frameBuffer(tex_id: GLuint) !GLuint { fn createFrameBuffer(tex_id: GLuint) !GLuint {
var fbo_id: GLuint = undefined; var fbo_id: GLuint = undefined;
gl.genFramebuffers(1, &fbo_id); gl.genFramebuffers(1, &fbo_id);
@ -302,7 +351,9 @@ const opengl_impl = struct {
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0); defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.framebufferTexture(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, tex_id, 0); gl.framebufferTexture(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, tex_id, 0);
gl.drawBuffers(1, &@as(GLuint, gl.COLOR_ATTACHMENT0));
const draw_buffers: [1]GLuint = .{gl.COLOR_ATTACHMENT0};
gl.drawBuffers(1, &draw_buffers);
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE)
return error.FrameBufferObejctInitFailed; return error.FrameBufferObejctInitFailed;
@ -311,6 +362,7 @@ const opengl_impl = struct {
} }
const shader = struct { const shader = struct {
const Kind = enum { vertex, fragment };
const log = std.log.scoped(.shader); const log = std.log.scoped(.shader);
fn didCompile(id: gl.GLuint) bool { fn didCompile(id: gl.GLuint) bool {

View File

@ -1,6 +1,7 @@
#version 330 core #version 330 core
out vec4 frag_color; out vec4 frag_color;
in vec3 color;
in vec2 uv; in vec2 uv;
uniform sampler2D screen; uniform sampler2D screen;

View File

@ -1,10 +1,13 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 in_color;
layout (location = 2) in vec2 in_uv;
out vec3 color;
out vec2 uv; out vec2 uv;
const vec2 pos[3] = vec2[3](vec2(-1.0f, -1.0f), vec2(-1.0f, 3.0f), vec2(3.0f, -1.0f));
const vec2 uvs[3] = vec2[3](vec2( 2.0f, 2.0f), vec2( 2.0f, 0.0f), vec2(0.0f, 2.0f));
void main() { void main() {
uv = uvs[gl_VertexID]; color = in_color;
gl_Position = vec4(pos[gl_VertexID], 0.0, 1.0); uv = in_uv;
} gl_Position = vec4(pos, 1.0);
}