فهرست منبع

Some work on setting texture filters. Doesn't do anything yet. I have to think about how to best handle samplers in GL vs D3D

Karl Zylinski 4 ماه پیش
والد
کامیت
f0db2cf72b
5فایلهای تغییر یافته به همراه69 افزوده شده و 2 حذف شده
  1. 3 2
      examples/minimal/minimal.odin
  2. BIN
      examples/minimal/sixten.jpg
  3. 18 0
      karl2d.odin
  4. 41 0
      render_backend_d3d11.odin
  5. 7 0
      render_backend_interface.odin

+ 3 - 2
examples/minimal/minimal.odin

@@ -11,7 +11,6 @@ _ :: mem
 main :: proc() {
 	context.logger = log.create_console_logger()
 
-
 	when ODIN_DEBUG {
 		track: mem.Tracking_Allocator
 		mem.tracking_allocator_init(&track, context.allocator)
@@ -27,9 +26,9 @@ main :: proc() {
 		}
 	}
 
-
 	k2.init(1080, 1080, "Karl2D Minimal Program")
 	k2.set_window_position(300, 100)
+	tex := k2.load_texture_from_file("sixten.jpg")
 
 	for !k2.shutdown_wanted() {
 		k2.process_events()
@@ -40,6 +39,8 @@ main :: proc() {
 		k2.draw_circle({120, 40}, 30, k2.BLACK)
 		k2.draw_circle({120, 40}, 20, k2.GREEN)
 		k2.draw_text("Hellöpe!", {10, 100}, 64, k2.WHITE)
+		k2.draw_texture_ex(tex, {0, 0, f32(tex.width), f32(tex.height)}, {10, 200, 900, 500}, {}, 0)
+
 		k2.present()
 		free_all(context.temp_allocator)
 	}

BIN
examples/minimal/sixten.jpg


+ 18 - 0
karl2d.odin

@@ -813,6 +813,19 @@ destroy_texture :: proc(tex: Texture) {
 	rb.destroy_texture(tex.handle)
 }
 
+// Controls how a texture should be filtered. `scale_down_filter` and `scale_up_filter` controls how
+// the texture is filtered when we render the texture at a smaller or larger size.
+// `mip_filter` controls how the texture is filtered when it is sampled using _mipmapping_.
+//
+// TODO: Add mipmapping generation controls for texture and refer to it from here.
+set_texture_filter :: proc(
+	t: Texture,
+	scale_down_filter: Texture_Filter,
+	scale_up_filter: Texture_Filter,
+	mip_filter: Texture_Filter,
+) {
+	rb.set_texture_filter(t.handle, scale_down_filter, scale_up_filter, mip_filter)
+}
 
 //-------//
 // FONTS //
@@ -1199,6 +1212,11 @@ Texture :: struct {
 	height: int,
 }
 
+Texture_Filter :: enum {
+	Point,  // Similar to "nearest neighbor". Pixly up/down scaling
+	Linear, // Smoothed up/down scaling
+}
+
 Camera :: struct {
 	target: Vec2,
 	offset: Vec2,

+ 41 - 0
render_backend_d3d11.odin

@@ -20,6 +20,7 @@ RENDER_BACKEND_INTERFACE_D3D11 :: Render_Backend_Interface {
 	load_texture = d3d11_load_texture,
 	update_texture = d3d11_update_texture,
 	destroy_texture = d3d11_destroy_texture,
+	set_texture_filter = d3d11_set_texture_filter,
 	load_shader = d3d11_load_shader,
 	destroy_shader = d3d11_destroy_shader,
 	default_shader_vertex_source = d3d11_default_shader_vertex_source,
@@ -433,6 +434,45 @@ d3d11_destroy_texture :: proc(th: Texture_Handle) {
 	hm.remove(&s.textures, th)
 }
 
+d3d11_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
+	}
+
+	d := scale_down_filter
+	u := scale_up_filter
+	m := mip_filter
+	f: d3d11.FILTER
+
+	if d == .Point && u == .Point && m == .Point {
+		f = .MIN_MAG_MIP_POINT
+	} else if d == .Point && u == .Point && m == .Linear {
+		f = .MIN_MAG_POINT_MIP_LINEAR
+	} else if d == .Point && u == .Linear && m == .Linear {
+		f = .MIN_POINT_MAG_MIP_LINEAR
+	} else if d == .Linear && u == .Linear && m == .Linear {
+		f = .MIN_MAG_MIP_LINEAR
+	} else if d == .Linear && u == .Linear && m == .Point {
+		f = .MIN_MAG_LINEAR_MIP_POINT
+	} else if d == .Linear && u == .Point && m == .Point {
+		f = .MIN_LINEAR_MAG_MIP_POINT
+	} else if d == .Linear && u == .Point && m == .Linear {
+		f = .MIN_LINEAR_MAG_POINT_MIP_LINEAR
+	} else if d == .Point && u == .Linear && m == .Point {
+		f = .MIN_POINT_MAG_LINEAR_MIP_POINT
+	}
+
+	t.filter = f
+}
+
 d3d11_load_shader :: proc(
 	vs_source: string,
 	ps_source: string,
@@ -841,6 +881,7 @@ D3D11_Texture :: struct {
 	tex: ^d3d11.ITexture2D,
 	view: ^d3d11.IShaderResourceView,
 	format: Pixel_Format,
+	filter: d3d11.FILTER,
 }
 
 dxgi_format_from_pixel_format :: proc(f: Pixel_Format) -> dxgi.FORMAT {

+ 7 - 0
render_backend_interface.odin

@@ -28,6 +28,13 @@ Render_Backend_Interface :: struct {
 	load_texture: proc(data: []u8, width: int, height: int, format: Pixel_Format) -> Texture_Handle,
 	update_texture: proc(handle: Texture_Handle, data: []u8, rect: Rect) -> bool,
 	destroy_texture: proc(handle: Texture_Handle),
+	
+	set_texture_filter: proc(
+		handle: Texture_Handle,
+		scale_down_filter: Texture_Filter,
+		scale_up_filter: Texture_Filter,
+		mip_filter: Texture_Filter,
+	),
 
 	load_shader: proc(vertex_shader_source: string, pixel_shader_source: string, desc_allocator := context.temp_allocator, layout_formats: []Pixel_Format = {}) -> (handle: Shader_Handle, desc: Shader_Desc),
 	destroy_shader: proc(shader: Shader_Handle),