Skip to content

Commit

Permalink
Merge pull request #873 from sh-dave/g1-texture-filter-settings
Browse files Browse the repository at this point in the history
overridable g1 texture filters
  • Loading branch information
RobDangerous authored Apr 18, 2024
2 parents d0b13f4 + 8f35991 commit 89b4155
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Sources/kinc/graphics1/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <kinc/graphics4/texture.h>
#include <kinc/graphics4/vertexbuffer.h>
#include <kinc/io/filereader.h>
#include <kinc/log.h>

#ifdef KINC_KONG
#include <kong.h>
Expand All @@ -17,6 +18,9 @@ static kinc_g4_shader_t vertexShader;
static kinc_g4_shader_t fragmentShader;
static kinc_g4_pipeline_t pipeline;
static kinc_g4_texture_unit_t tex;
kinc_g1_texture_filter_t kinc_internal_g1_texture_filter_min = KINC_G1_TEXTURE_FILTER_LINEAR;
kinc_g1_texture_filter_t kinc_internal_g1_texture_filter_mag = KINC_G1_TEXTURE_FILTER_LINEAR;
kinc_g1_mipmap_filter_t kinc_internal_g1_mipmap_filter = KINC_G1_MIPMAP_FILTER_NONE;
#endif
static kinc_g4_vertex_buffer_t vb;
static kinc_g4_index_buffer_t ib;
Expand All @@ -30,6 +34,28 @@ void kinc_g1_begin(void) {
kinc_internal_g1_image = (uint32_t *)kinc_g4_texture_lock(&texture);
}

static inline kinc_g4_texture_filter_t map_texture_filter(kinc_g1_texture_filter_t filter) {
switch (filter) {
case KINC_G1_TEXTURE_FILTER_POINT: return KINC_G4_TEXTURE_FILTER_POINT;
case KINC_G1_TEXTURE_FILTER_LINEAR: return KINC_G4_TEXTURE_FILTER_LINEAR;
case KINC_G1_TEXTURE_FILTER_ANISOTROPIC: return KINC_G4_TEXTURE_FILTER_ANISOTROPIC;
}

kinc_log(KINC_LOG_LEVEL_WARNING, "unhandled kinc_g1_texture_filter_t (%i)", filter);
return KINC_G1_TEXTURE_FILTER_LINEAR;
}

static inline kinc_g4_texture_filter_t map_mipmap_filter(kinc_g1_texture_filter_t filter) {
switch (filter) {
case KINC_G1_MIPMAP_FILTER_NONE: return KINC_G4_MIPMAP_FILTER_NONE;
case KINC_G1_MIPMAP_FILTER_POINT: return KINC_G4_MIPMAP_FILTER_POINT;
case KINC_G1_MIPMAP_FILTER_LINEAR: return KINC_G4_MIPMAP_FILTER_LINEAR;
}

kinc_log(KINC_LOG_LEVEL_WARNING, "unhandled kinc_g1_mipmap_filter_t (%i)", filter);
return KINC_G4_MIPMAP_FILTER_NONE;
}

void kinc_g1_end(void) {
kinc_internal_g1_image = NULL;
kinc_g4_texture_unlock(&texture);
Expand All @@ -44,6 +70,9 @@ void kinc_g1_end(void) {

#ifndef KINC_KONG
kinc_g4_set_texture(tex, &texture);
kinc_g4_set_texture_minification_filter(tex, map_texture_filter(kinc_internal_g1_texture_filter_min));
kinc_g4_set_texture_magnification_filter(tex, map_texture_filter(kinc_internal_g1_texture_filter_mag));
kinc_g4_set_texture_mipmap_filter(tex, map_mipmap_filter(kinc_internal_g1_mipmap_filter));
#endif
kinc_g4_set_vertex_buffer(&vb);
kinc_g4_set_index_buffer(&ib);
Expand Down Expand Up @@ -172,4 +201,16 @@ int kinc_g1_height() {
return kinc_internal_g1_h;
}

void kinc_g1_set_texture_magnification_filter(kinc_g1_texture_filter_t filter) {
kinc_internal_g1_texture_filter_mag = filter;
}

void kinc_g1_set_texture_minification_filter(kinc_g1_texture_filter_t filter) {
kinc_internal_g1_texture_filter_min = filter;
}

void kinc_g1_set_texture_mipmap_filter(kinc_g1_mipmap_filter_t filter) {
kinc_internal_g1_mipmap_filter = filter;
}

#endif
48 changes: 48 additions & 0 deletions Sources/kinc/graphics1/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
extern "C" {
#endif

typedef enum {
KINC_G1_TEXTURE_FILTER_POINT,
KINC_G1_TEXTURE_FILTER_LINEAR,
KINC_G1_TEXTURE_FILTER_ANISOTROPIC,
} kinc_g1_texture_filter_t;

typedef enum {
KINC_G1_MIPMAP_FILTER_NONE,
KINC_G1_MIPMAP_FILTER_POINT,
KINC_G1_MIPMAP_FILTER_LINEAR,
} kinc_g1_mipmap_filter_t;

/// <summary>
/// Initializes the G1-API.
/// </summary>
Expand All @@ -34,6 +46,9 @@ KINC_FUNC void kinc_g1_end(void);

extern uint32_t *kinc_internal_g1_image;
extern int kinc_internal_g1_w, kinc_internal_g1_h, kinc_internal_g1_tex_width;
extern kinc_g1_texture_filter_t kinc_internal_g1_texture_filter_min;
extern kinc_g1_texture_filter_t kinc_internal_g1_texture_filter_mag;
extern kinc_g1_mipmap_filter_t kinc_internal_g1_mipmap_filter;

#if defined(KINC_DYNAMIC_COMPILE) || defined(KINC_DYNAMIC) || defined(KINC_DOCS)

Expand All @@ -59,6 +74,27 @@ KINC_FUNC int kinc_g1_width(void);
/// <returns>The height</returns>
KINC_FUNC int kinc_g1_height(void);

/// <summary>
/// Set the texture-sampling-mode for upscaled textures.
/// </summary>
/// <param name="unit">The texture-unit to set the texture-sampling-mode for</param>
/// <param name="filter">The mode to set</param>
KINC_FUNC void kinc_g1_set_texture_magnification_filter(kinc_g1_texture_filter_t filter);

/// <summary>
/// Set the texture-sampling-mode for downscaled textures.
/// </summary>
/// <param name="unit">The texture-unit to set the texture-sampling-mode for</param>
/// <param name="filter">The mode to set</param>
KINC_FUNC void kinc_g1_set_texture_minification_filter(kinc_g1_texture_filter_t filter);

/// <summary>
/// Sets the mipmap-sampling-mode which defines whether mipmaps are used at all and if so whether the two neighbouring mipmaps are linearly interpolated.
/// </summary>
/// <param name="unit">The texture-unit to set the mipmap-sampling-mode for</param>
/// <param name="filter">The mode to set</param>
KINC_FUNC void kinc_g1_set_texture_mipmap_filter(kinc_g1_mipmap_filter_t filter);

#else

// implementation moved to the header to allow easy inlining
Expand All @@ -79,6 +115,18 @@ static inline int kinc_g1_height(void) {
return kinc_internal_g1_h;
}

static inline void kinc_g1_set_texture_magnification_filter(kinc_g1_texture_filter_t filter) {
kinc_internal_g1_texture_filter_mag = filter;
}

static inline void kinc_g1_set_texture_minification_filter(kinc_g1_texture_filter_t filter) {
kinc_internal_g1_texture_filter_min = filter;
}

static inline void kinc_g1_set_texture_mipmap_filter(kinc_g1_mipmap_filter_t filter) {
kinc_internal_g1_mipmap_filter = filter;
}

#endif

#ifdef __cplusplus
Expand Down

0 comments on commit 89b4155

Please sign in to comment.