Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Half-float support #354

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ taisei_c_args = [
'-fno-math-errno',
'-fno-signaling-nans',
'-fno-trapping-math',
'-mfp16-format=ieee',
]

deprecation_warnings = get_option('deprecation_warnings')
Expand Down Expand Up @@ -313,6 +314,128 @@ config.set('TAISEI_BUILDCONF_HAVE_ATTR_MALLOC_WITH_ARGS', cc.compiles(
args : ['-Wattributes', '-Werror']
))

config.set('TAISEI_BUILDCONF_F16_CVT_TYPE', false)
config.set('TAISEI_BUILDCONF_F16_STORAGE_TYPE', 'uint16_t')
config.set('TAISEI_BUILDCONF_F16_SIMD_TYPE', false)
config.set('TAISEI_BUILDCONF_F16_RT_ABI_TYPE', false)
config.set('TAISEI_BUILDCONF_F16_RT_FUNC_H2F', false)
config.set('TAISEI_BUILDCONF_F16_RT_FUNC_F2H', false)

float16_cvt_types = {
# cvt : storage
'_Float16' : '_Float16',
'__fp16' : 'uint16_t',
}
float16_simd_types = ['_Float16']
float16_rt_abi = 'none'
float16_rt_abi_typemap = {
'native' : '_Float16',
'integer' : 'uint16_t',
}
float16_rt_abi_choices = ['auto', 'none'] + float16_rt_abi_typemap.keys()
float16_rt_funcs = [
[ '__extendhfsf2', '__truncsfhf2' ],
[ '__gnu_h2f_ieee', '__gnu_f2h_ieee' ],
]

float16_rt_abi = meson.get_external_property('float16_rt_abi', 'auto')
if float16_rt_abi not in float16_rt_abi_choices
error('float16_rt_abi must be one of @0@'.format(', '.join(float16_rt_abi_choices)))
endif

float16_have_native_conversion = false
float16_have_rtlib_conversion = false

foreach cvt_type, storage_type : float16_cvt_types
if cc.sizeof(cvt_type) == 2
config.set('TAISEI_BUILDCONF_F16_CVT_TYPE', cvt_type)
config.set('TAISEI_BUILDCONF_F16_STORAGE_TYPE', storage_type)

if cvt_type in float16_simd_types
config.set('TAISEI_BUILDCONF_F16_SIMD_TYPE', cvt_type)
endif

float16_rt_abi = 'none'
float16_have_native_conversion = true
break
endif
endforeach

if float16_rt_abi != 'none'
foreach funcs : float16_rt_funcs
func_h2f = funcs[0]
func_f2h = funcs[1]

if not cc.has_function(func_h2f) or not cc.has_function(func_f2h)
continue
endif

if float16_rt_abi == 'auto'
float16_rt_abi = 'none'

foreach abiname, abitype : float16_rt_abi_typemap
r = cc.run(f'''
#include <assert.h>
#include <stdint.h>

#define NOINLINE __attribute__((noinline))

typedef @abitype@ f16_abi_t;
float @func_h2f@(f16_abi_t);
f16_abi_t @func_f2h@(float);

NOINLINE float f16_to_f32(uint16_t x) {
union {
f16_abi_t _f16abi;
uint16_t _uint;
} u = { ._uint = x };
return @func_h2f@(u._f16abi);
}

NOINLINE uint16_t f32_to_f16(float x) {
union {
f16_abi_t _f16abi;
uint16_t _uint;
} u = { ._f16abi = @func_f2h@(x) };
return u._uint;
}

int main(int argc, char **argv) {
volatile float src = 420.69f;
const float expected = 420.75f;
volatile uint16_t half = f32_to_f16(src);
volatile float roundtrip = f16_to_f32(half);
assert(roundtrip == expected);
return 0;
}
''', name : f'Test for @abiname@ float16 ABI')

if r.compiled() and r.returncode() == 0
float16_rt_abi = abiname
break
endif
endforeach
endif

if float16_rt_abi == 'none'
break
endif

abi_type = float16_rt_abi_typemap[float16_rt_abi]
config.set('TAISEI_BUILDCONF_F16_STORAGE_TYPE', abi_type)
config.set('TAISEI_BUILDCONF_F16_RT_ABI_TYPE', abi_type)
config.set('TAISEI_BUILDCONF_F16_RT_FUNC_H2F', func_h2f)
config.set('TAISEI_BUILDCONF_F16_RT_FUNC_F2H', func_f2h)

if abi_type in float16_simd_types
config.set('TAISEI_BUILDCONF_F16_SIMD_TYPE', abi_type)
endif

float16_have_rtlib_conversion = true
break
endforeach
endif

prefer_relpath_systems = [
'windows',
]
Expand Down
14 changes: 5 additions & 9 deletions src/renderer/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ typedef enum Primitive {

typedef enum VertexAttribType {
VA_FLOAT,
VA_HALF,
VA_BYTE,
VA_UBYTE,
VA_SHORT,
Expand Down Expand Up @@ -482,15 +483,10 @@ typedef struct SpriteParamsBuffer {
typedef struct SpriteInstanceAttribs {
mat4 mv_transform;
mat4 tex_transform;

union {
FloatRect texrect;
vec4 texrect_vec4;
};

Color rgba;
FloatExtent sprite_size;
ShaderCustomParams custom;
FloatRect texrect;
float16_storage_t sprite_size[2];
float16_storage_t rgba[4];
float16_storage_t custom[4];

// offsetof(end_of_fields) == size without padding.
char end_of_fields;
Expand Down
23 changes: 10 additions & 13 deletions src/renderer/common/sprite_batch.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void _r_sprite_batch_init(void) {
size_t sz_vert = sizeof(GenericModelVertex);
size_t sz_attr = SIZEOF_SPRITE_ATTRIBS;

#define VERTEX_OFS(attr) offsetof(GenericModelVertex, attr)
#define VERTEX_OFS(attr) offsetof(GenericModelVertex, attr)
#define INSTANCE_OFS(attr) offsetof(SpriteInstanceAttribs, attr)

VertexAttribFormat fmt[] = {
Expand All @@ -83,10 +83,10 @@ void _r_sprite_batch_init(void) {
{ { 4, VA_FLOAT, VA_CONVERT_FLOAT, 1 }, sz_attr, INSTANCE_OFS(tex_transform[1]), 1 },
{ { 4, VA_FLOAT, VA_CONVERT_FLOAT, 1 }, sz_attr, INSTANCE_OFS(tex_transform[2]), 1 },
{ { 4, VA_FLOAT, VA_CONVERT_FLOAT, 1 }, sz_attr, INSTANCE_OFS(tex_transform[3]), 1 },
{ { 4, VA_FLOAT, VA_CONVERT_FLOAT, 1 }, sz_attr, INSTANCE_OFS(rgba), 1 },
{ { 4, VA_HALF, VA_CONVERT_FLOAT, 1 }, sz_attr, INSTANCE_OFS(rgba), 1 },
{ { 4, VA_FLOAT, VA_CONVERT_FLOAT, 1 }, sz_attr, INSTANCE_OFS(texrect), 1 },
{ { 2, VA_FLOAT, VA_CONVERT_FLOAT, 1 }, sz_attr, INSTANCE_OFS(sprite_size), 1 },
{ { 4, VA_FLOAT, VA_CONVERT_FLOAT, 1 }, sz_attr, INSTANCE_OFS(custom), 1 },
{ { 2, VA_HALF, VA_CONVERT_FLOAT, 1 }, sz_attr, INSTANCE_OFS(sprite_size), 1 },
{ { 4, VA_HALF, VA_CONVERT_FLOAT, 1 }, sz_attr, INSTANCE_OFS(custom), 1 },
};

#undef VERTEX_OFS
Expand Down Expand Up @@ -213,12 +213,9 @@ static void _r_sprite_batch_compute_attribs(
glm_translate(attribs.mv_transform, (vec3) { ofs.x / imgdims.w, ofs.y / imgdims.h });
}

if(params->color == NULL) {
// XXX: should we use r_color_current here?
attribs.rgba = *RGBA(1, 1, 1, 1);
} else {
attribs.rgba = *params->color;
}
// XXX: should we default to r_color_current here?
const Color *color = params->color ?: RGBA(1, 1, 1, 1);
f32v4_to_f16v4(attribs.rgba, color->rgba);

attribs.texrect = spr->tex_area;

Expand All @@ -232,12 +229,12 @@ static void _r_sprite_batch_compute_attribs(
attribs.texrect.h *= -1;
}

attribs.sprite_size = spr->extent;
f32v2_to_f16v2(attribs.sprite_size, spr->extent.as_array);

if(params->shader_params == NULL) {
memset(&attribs.custom, 0, sizeof(attribs.custom));
memset(attribs.custom, 0, sizeof(attribs.custom));
} else {
attribs.custom = *params->shader_params;
f32v4_to_f16v4(attribs.custom, params->shader_params->vector);
}

*out_attribs = attribs;
Expand Down
1 change: 1 addition & 0 deletions src/renderer/gl33/vertex_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

static GLenum va_type_to_gl_type[] = {
[VA_FLOAT] = GL_FLOAT,
[VA_HALF] = GL_HALF_FLOAT,
[VA_BYTE] = GL_BYTE,
[VA_UBYTE] = GL_UNSIGNED_BYTE,
[VA_SHORT] = GL_SHORT,
Expand Down
24 changes: 7 additions & 17 deletions src/resource/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -1047,21 +1047,14 @@ static double _text_ucs4_draw(Font *font, const uint32_t *ucs4text, const TextPa

text_ucs4_bbox(font, ucs4text, 0, &bbox);

Color color;

if(params->color == NULL) {
// XXX: sprite batch code defaults this to RGB(1, 1, 1)
color = *r_color_current();
} else {
color = *params->color;
}

ShaderCustomParams shader_params;
SpriteInstanceAttribs init_attribs = {};
// XXX: sprite batch code defaults this to RGBA(1, 1, 1, 1)
f32v4_to_f16v4(init_attribs.rgba, (params->color ?: r_color_current())->rgba);

if(params->shader_params == NULL) {
memset(&shader_params, 0, sizeof(shader_params));
memset(init_attribs.custom, 0, sizeof(init_attribs.custom));
} else {
shader_params = *params->shader_params;
f32v4_to_f16v4(init_attribs.custom, params->shader_params->vector);
}

mat4 mat_texture;
Expand Down Expand Up @@ -1131,9 +1124,7 @@ static double _text_ucs4_draw(Font *font, const uint32_t *ucs4text, const TextPa
Sprite *spr = &glyph->sprite;
set_batch_texture(&batch_state_params, spr->tex);

SpriteInstanceAttribs attribs;
attribs.rgba = color;
attribs.custom = shader_params;
SpriteInstanceAttribs attribs = init_attribs;

float g_x = x + glyph->metrics.bearing_x + spr->w * 0.5;
float g_y = y - glyph->metrics.bearing_y + spr->h * 0.5 - font->metrics.descent;
Expand All @@ -1147,8 +1138,7 @@ static double _text_ucs4_draw(Font *font, const uint32_t *ucs4text, const TextPa
attribs.texrect = spr->tex_area;

// NOTE: Glyphs have their sprite w/h unadjusted for scale.
attribs.sprite_size.w = spr->w * iscale;
attribs.sprite_size.h = spr->h * iscale;
f32v2_to_f16v2(attribs.sprite_size, (float[2]) { spr->w * iscale, spr->h * iscale });

if(params->glyph_callback.func != NULL) {
params->glyph_callback.func(font, uchar, &attribs, params->glyph_callback.userdata);
Expand Down
2 changes: 1 addition & 1 deletion src/stagedraw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ static int draw_numeric_callback(Font *font, charcode_t charcode, SpriteInstance
st->color1 = st->color2;
}

spr_attribs->rgba = *st->color1;
f32v4_to_f16v4(spr_attribs->rgba, st->color1->rgba);
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "util/miscmath.h"
// #include "util/pngcruft.h"
#include "util/stringops.h"
#include "util/float16.h"

// FIXME: might not be the best place for these
#include "log.h"
Expand Down
Loading