Karl Zylinski пре 4 месеци
родитељ
комит
446c2ab0ee
3 измењених фајлова са 47 додато и 33 уклоњено
  1. 14 11
      TODO.md
  2. 1 0
      examples/minimal/minimal.odin
  3. 32 22
      render_backend_gl.odin

+ 14 - 11
TODO.md

@@ -1,15 +1,5 @@
 ## TODO
-* GL backend:
-	textures --- try make the d3d11 backend support multiple textures first and
-	             then generalize to gl
-
-	             for d3d11: we need to reflect bound resources for both vs and ps... if they are
-	             shared then perhaps we only need one buffer -- also, should you create less buffers
-	             and reuse them between shaders? So it doesn't become lots of buffers for each shader
-	             permutation
-	X set uniforms -- needs more type info?
-	X the constant types are hardcoded to just a few types right now
-
+* Can we reuse memory for const buffers and union blocks between shaders? Just create reasonably sized ones and fetch based on size or something.
 * should gamepad come from separate interface than window?
 	* keyboard input could also come from some input interface, but
 	  it is tightly bound to window in windows, so we'll see.
@@ -19,6 +9,8 @@
 	* what happens when you pull one out?
 	* playstation
 * Textures: Make the sampler state configurable
+	* filtering (still needs to fix GL)
+	* wrapping
 * Textures D3D11: Do we need the SRV in the texture?
 * Shaders: Reflect and expose samplers
 	* generalised sampler handling for both gl and d3d
@@ -32,6 +24,17 @@
 * think about sound
 
 ## DONE
+* GL backend:
+	textures --- try make the d3d11 backend support multiple textures first and
+	             then generalize to gl
+
+	             for d3d11: we need to reflect bound resources for both vs and ps... if they are
+	             shared then perhaps we only need one buffer -- also, should you create less buffers
+	             and reuse them between shaders? So it doesn't become lots of buffers for each shader
+	             permutation
+	X set uniforms -- needs more type info?
+	X the constant types are hardcoded to just a few types right now
+
 * Should we sort by depth? Maybe we should use Vec3 because some 2D games rely on it?
 	* I think we should.
 * Do proper checks of vertex count and dispatch rendering when full

+ 1 - 0
examples/minimal/minimal.odin

@@ -29,6 +29,7 @@ main :: proc() {
 	k2.init(1080, 1080, "Karl2D Minimal Program")
 	k2.set_window_position(300, 100)
 	tex := k2.load_texture_from_file("sixten.jpg")
+	k2.set_texture_filter(tex, .Point)
 
 	for !k2.shutdown_wanted() {
 		k2.process_events()

+ 32 - 22
render_backend_gl.odin

@@ -20,6 +20,7 @@ RENDER_BACKEND_INTERFACE_GL :: Render_Backend_Interface {
 	load_texture = gl_load_texture,
 	update_texture = gl_update_texture,
 	destroy_texture = gl_destroy_texture,
+	set_texture_filter = gl_set_texture_filter,
 	load_shader = gl_load_shader,
 	destroy_shader = gl_destroy_shader,
 
@@ -325,18 +326,18 @@ gl_set_internal_state :: proc(state: rawptr) {
 	s = (^GL_State)(state)
 }
 
-gl_create_texture :: proc(width: int, height: int, format: Pixel_Format) -> Texture_Handle {
+create_texture :: proc(width: int, height: int, format: Pixel_Format, data: rawptr) -> Texture_Handle {
 	id: u32
 	gl.GenTextures(1, &id)
 	gl.BindTexture(gl.TEXTURE_2D, id)
 
 	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
 	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
-	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
-	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
+	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
+	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
 
 	pf := gl_translate_pixel_format(format)
-	gl.TexImage2D(gl.TEXTURE_2D, 0, pf, i32(width), i32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
+	gl.TexImage2D(gl.TEXTURE_2D, 0, pf, i32(width), i32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, data)
 
 	tex := GL_Texture {
 		id = id,
@@ -346,25 +347,12 @@ gl_create_texture :: proc(width: int, height: int, format: Pixel_Format) -> Text
 	return hm.add(&s.textures, tex)
 }
 
-gl_load_texture :: proc(data: []u8, width: int, height: int, format: Pixel_Format) -> Texture_Handle {
-	id: u32
-	gl.GenTextures(1, &id)
-	gl.BindTexture(gl.TEXTURE_2D, id)
-
-	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
-	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
-	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
-	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
-
-	pf := gl_translate_pixel_format(format)
-	gl.TexImage2D(gl.TEXTURE_2D, 0, pf, i32(width), i32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, raw_data(data))
+gl_create_texture :: proc(width: int, height: int, format: Pixel_Format) -> Texture_Handle {
+	return create_texture(width, height, format, nil)
+}
 
-	tex := GL_Texture {
-		id = id,
-		format = format,
-	}
-	
-	return hm.add(&s.textures, tex)
+gl_load_texture :: proc(data: []u8, width: int, height: int, format: Pixel_Format) -> Texture_Handle {
+	return create_texture(width, height, format, raw_data(data))
 }
 
 gl_update_texture :: proc(th: Texture_Handle, data: []u8, rect: Rect) -> bool {
@@ -390,6 +378,28 @@ gl_destroy_texture :: proc(th: Texture_Handle) {
 	hm.remove(&s.textures, th)
 }
 
+gl_set_texture_filter :: proc(
+	th: Texture_Handle,
+	scale_down_filter: Texture_Filter,
+	scale_up_filter: Texture_Filter,
+	mip_filter: Texture_Filter,
+) {
+	t := hm.get(&s.textures, th)
+
+	if t == nil {
+		log.error("Trying to set texture filter for invalid texture %v", th)
+		return
+	}
+
+	gl.BindTexture(gl.TEXTURE_2D, t.id)
+
+	min_filter: i32 = scale_down_filter == .Point ? gl.NEAREST : gl.LINEAR
+	mag_filter: i32 = scale_up_filter == .Point ? gl.NEAREST : gl.LINEAR
+
+	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, min_filter)
+	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, mag_filter)
+}
+
 Shader_Compile_Result_OK :: struct {}
 
 Shader_Compile_Result_Error :: string