Skip to content

Commit

Permalink
Try fix rendering on high-dpi
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoam committed Feb 11, 2019
1 parent d81b417 commit 99ff016
Show file tree
Hide file tree
Showing 17 changed files with 70 additions and 81 deletions.
22 changes: 11 additions & 11 deletions src/bgfx/BgfxSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ module mud.bgfx;

namespace mud
{
BgfxContext::BgfxContext(BgfxSystem& gfx_system, const string& name, int width, int height, bool fullScreen, bool init)
BgfxContext::BgfxContext(BgfxSystem& gfx_system, const string& name, uvec2 size, bool fullScreen, bool init)
#if defined MUD_CONTEXT_GLFW
: GlfwContext(gfx_system, name, width, height, fullScreen, false)
: GlfwContext(gfx_system, name, size, fullScreen, false)
#elif defined MUD_CONTEXT_WASM
: EmContext(gfx_system, name, width, height, fullScreen)
: EmContext(gfx_system, name, size, fullScreen)
#elif defined MUD_CONTEXT_WINDOWS
: WinContext(gfx_system, name, width, height, fullScreen)
: WinContext(gfx_system, name, size, fullScreen)
#endif
{
if(init)
gfx_system.init(*this);
}

void BgfxContext::reset(uint16_t width, uint16_t height)
void BgfxContext::reset_fb(const uvec2& size)
{
bgfx::reset(width, height, BGFX_RESET_NONE);
bgfx::reset(uint16_t(size.x), uint16_t(size.y), BGFX_RESET_NONE);
}

BgfxSystem::BgfxSystem(const string& resource_path)
Expand All @@ -57,9 +57,9 @@ namespace mud
return alloc;
}

object<Context> BgfxSystem::create_context(const string& name, int width, int height, bool fullScreen)
object<Context> BgfxSystem::create_context(const string& name, uvec2 size, bool fullScreen)
{
return oconstruct<BgfxContext>(*this, name, width, height, fullScreen, !m_initialized);
return oconstruct<BgfxContext>(*this, name, size, fullScreen, !m_initialized);
}

void BgfxSystem::init(BgfxContext& context)
Expand All @@ -74,8 +74,8 @@ namespace mud
bgfx::Init params = {};
params.type = bgfx::RendererType::OpenGL;
//params.type = bgfx::RendererType::Direct3D11;
params.resolution.width = uint32_t(context.m_width);
params.resolution.height = uint32_t(context.m_height);
params.resolution.width = uint32_t(context.m_size.x);
params.resolution.height = uint32_t(context.m_size.y);
params.resolution.reset = BGFX_RESET_NONE;
bgfx::init(params);

Expand All @@ -85,7 +85,7 @@ namespace mud
bgfx::setDebug(BGFX_DEBUG_TEXT | BGFX_DEBUG_PROFILER);
#endif

bgfx::setViewRect(0, 0, 0, uint16_t(context.m_width), uint16_t(context.m_height));
bgfx::setViewRect(0, 0, 0, uint16_t(context.m_fb_size.x), uint16_t(context.m_fb_size.y));
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x000000ff, 1.0f, 0);

m_start_counter = double(bx::getHPCounter());
Expand Down
6 changes: 3 additions & 3 deletions src/bgfx/BgfxSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ namespace mud
#endif
{
public:
BgfxContext(BgfxSystem& gfx_system, const string& name, int width, int height, bool fullScreen, bool init);
BgfxContext(BgfxSystem& gfx_system, const string& name, uvec2 size, bool fullScreen, bool init);

virtual void reset(uint16_t width, uint16_t height) override;
virtual void reset_fb(const uvec2& size) override;
};

export_ struct MUD_BGFX_EXPORT TimerBx
Expand All @@ -62,7 +62,7 @@ namespace mud
virtual void begin_frame() override;
virtual bool next_frame() override;

virtual object<Context> create_context(const string& name, int width, int height, bool fullScreen) override;
virtual object<Context> create_context(const string& name, uvec2 size, bool fullScreen) override;

void init(BgfxContext& context);
void advance();
Expand Down
3 changes: 0 additions & 3 deletions src/clrefl/Generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,6 @@ namespace mud
//explicit operator const CLType&() const { return *m_type; }
};

template<typename T, size_t N>
constexpr size_t array_size(T(&array)[N]) { return N; }

class CLBaseType : public CLType
{
public:
Expand Down
24 changes: 9 additions & 15 deletions src/ctx-glfw/GlfwContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ namespace mud
else return translate(convert_glfw_key(key));
}

GlfwContext::GlfwContext(RenderSystem& render_system, const string& name, int width, int height, bool full_screen, bool auto_swap)
: Context(render_system, name, width, height, full_screen)
GlfwContext::GlfwContext(RenderSystem& render_system, const string& name, uvec2 size, bool full_screen, bool auto_swap)
: Context(render_system, name, size, full_screen)
, m_gl_window(nullptr)
, m_auto_swap(auto_swap)
{
Expand Down Expand Up @@ -222,7 +222,7 @@ namespace mud
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
//glfwWindowHint(GLFW_DECORATED, 0);

m_gl_window = glfwCreateWindow(m_width, m_height, m_title.c_str(), NULL, NULL);
m_gl_window = glfwCreateWindow(int(m_size.x), int(m_size.y), m_title.c_str(), NULL, NULL);

if(!m_gl_window)
{
Expand All @@ -246,11 +246,6 @@ namespace mud
#endif
}

void GlfwContext::reset(uint16_t width, uint16_t height)
{
UNUSED(width); UNUSED(height);
}

void GlfwContext::lock_mouse(bool locked)
{
m_mouse_lock = locked;
Expand All @@ -275,15 +270,15 @@ namespace mud
void GlfwContext::update_size()
{
int width, height;
int fbWidth, fbHeight;
int fb_width, fb_height;
glfwGetWindowSize(m_gl_window, &width, &height);
glfwGetFramebufferSize(m_gl_window, &fbWidth, &fbHeight);
glfwGetFramebufferSize(m_gl_window, &fb_width, &fb_height);

// Calculate pixel ration for hi-dpi devices.
// float pxRatio = (float)fbWidth / (float)winWidth;
m_pixel_ratio = (float)fb_width / (float)width;

m_width = width;
m_height = height;
m_size = { uint(width), uint(height) };
m_fb_size = { uint(fb_width), uint(fb_height) };
}

void GlfwContext::init_input(Mouse& mouse, Keyboard& keyboard)
Expand All @@ -303,11 +298,10 @@ namespace mud
void GlfwContext::inject_mouse_move(double x, double y)
{
//printf("glfw: mouse move %f, %f\n", float(x), float(y));
vec2 size = { float(m_width), float(m_height) };
if(m_mouse_lock)
m_cursor = { float(x), float(y) };
else
m_cursor = max(vec2(0.f), min(size, vec2{ float(x), float(y) }));
m_cursor = max(vec2(0.f), min(vec2(m_fb_size), vec2{ float(x), float(y) }));
m_mouse->moved(m_cursor);
}

Expand Down
3 changes: 1 addition & 2 deletions src/ctx-glfw/GlfwContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ namespace mud
export_ class refl_ MUD_CTX_GLFW_EXPORT GlfwContext : public Context
{
public:
GlfwContext(RenderSystem& render_system, const string& name, int width, int height, bool full_screen, bool auto_swap = true);
GlfwContext(RenderSystem& render_system, const string& name, uvec2 size, bool full_screen, bool auto_swap = true);
~GlfwContext();

void init_context();
virtual void init_input(Mouse& mouse, Keyboard& keyboard) final;
virtual void reset(uint16_t width, uint16_t height) override;

virtual bool next_frame() final;

Expand Down
6 changes: 3 additions & 3 deletions src/ctx/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ namespace mud
, m_manual_render(manual_render)
{}

Context::Context(RenderSystem& render_system, const string& title, int width, int height, bool full_screen)
Context::Context(RenderSystem& render_system, const string& title, uvec2 size, bool full_screen)
: m_render_system(render_system)
, m_resource_path(render_system.m_resource_path)
, m_title(title)
, m_width(width)
, m_height(height)
, m_size(size)
, m_fb_size(size)
, m_full_screen(full_screen)
{}

Expand Down
12 changes: 7 additions & 5 deletions src/ctx/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace mud
virtual void begin_frame() = 0;
virtual bool next_frame() = 0;

virtual object<Context> create_context(const string& name, int width, int height, bool fullScreen) = 0;
virtual object<Context> create_context(const string& name, uvec2 size, bool fullScreen) = 0;

const string m_resource_path;
const bool m_manual_render;
Expand All @@ -33,17 +33,19 @@ namespace mud
export_ class refl_ MUD_CTX_EXPORT Context
{
public:
Context(RenderSystem& render_system, const string& title, int width, int height, bool full_screen = false);
Context(RenderSystem& render_system, const string& title, uvec2 size, bool full_screen = false);
virtual ~Context();

RenderSystem& m_render_system;
attr_ const string m_resource_path;

attr_ string m_title;
attr_ unsigned int m_width;
attr_ unsigned int m_height;
attr_ uvec2 m_size;
attr_ uvec2 m_fb_size;
attr_ bool m_full_screen;

attr_ float m_pixel_ratio;

size_t m_handle = 0;
void* m_native_handle = nullptr;
void* m_native_target = nullptr;
Expand All @@ -54,7 +56,7 @@ namespace mud
attr_ vec2 m_cursor;
attr_ bool m_mouse_lock = false;

meth_ virtual void reset(uint16_t width, uint16_t height) = 0;
meth_ virtual void reset_fb(const uvec2& size) = 0;
meth_ virtual void init_input(Mouse& mouse, Keyboard& keyboard) = 0;

meth_ virtual bool next_frame() = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/frame/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace mud

void Shell::init()
{
m_context = m_gfx_system.create_context("mud EditorCore", 1600, 900, false);
m_context = m_gfx_system.create_context("mud EditorCore", { 1600U, 900U }, false);
GfxContext& context = as<GfxContext>(*m_context);
#if defined MUD_VG_VG
m_vg = oconstruct<VgVg>(m_resource_path.c_str(), &m_gfx_system.allocator());
Expand Down
20 changes: 10 additions & 10 deletions src/gfx/GfxSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,28 @@ module mud.gfx;

namespace mud
{
GfxContext::GfxContext(GfxSystem& gfx_system, const string& name, int width, int height, bool fullScreen, bool init)
: BgfxContext(gfx_system, name, width, height, fullScreen, false)
GfxContext::GfxContext(GfxSystem& gfx_system, const string& name, uvec2 size, bool fullScreen, bool init)
: BgfxContext(gfx_system, name, size, fullScreen, false)
, m_gfx_system(gfx_system)
, m_target()
{
if(init)
gfx_system.init(*this);
m_target = oconstruct<RenderTarget>(uvec2(width, height));
m_target = oconstruct<RenderTarget>(size);
}

GfxContext::~GfxContext()
{}

void GfxContext::reset(uint16_t width, uint16_t height)
void GfxContext::reset_fb(const uvec2& size)
{
bgfx::reset(width, height, BGFX_RESET_NONE);
if(width == 0 || height == 0)
bgfx::reset(uint16_t(size.x), uint16_t(size.y), BGFX_RESET_NONE);
if(size.x == 0 || size.y == 0)
m_target = nullptr;
else
{
if(!m_target || width != m_target->m_size.x || height != m_target->m_size.y)
m_target = oconstruct<RenderTarget>(uvec2(width, height));
if(!m_target || size != m_target->m_size)
m_target = oconstruct<RenderTarget>(size);
}
m_vg_handle = m_reset_vg(*this, *m_gfx_system.m_vg);
}
Expand Down Expand Up @@ -145,9 +145,9 @@ namespace mud
return m_impl->m_importers[size_t(format)];
}

object<Context> GfxSystem::create_context(const string& name, int width, int height, bool fullScreen)
object<Context> GfxSystem::create_context(const string& name, uvec2 size, bool fullScreen)
{
object<GfxContext> context = oconstruct<GfxContext>(*this, name, width, height, fullScreen, !m_initialized);
object<GfxContext> context = oconstruct<GfxContext>(*this, name, size, fullScreen, !m_initialized);
m_impl->m_contexts.push_back(context.get());
return move(context);
}
Expand Down
6 changes: 3 additions & 3 deletions src/gfx/GfxSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ namespace mud
export_ class refl_ MUD_GFX_EXPORT GfxContext : public BgfxContext
{
public:
GfxContext(GfxSystem& gfx_system, const string& name, int width, int height, bool fullScreen, bool init);
GfxContext(GfxSystem& gfx_system, const string& name, uvec2 size, bool fullScreen, bool init);
~GfxContext();

virtual void reset(uint16_t width, uint16_t height) override;
virtual void reset_fb(const uvec2& size) override;

GfxSystem& m_gfx_system;

Expand Down Expand Up @@ -85,7 +85,7 @@ namespace mud
virtual void begin_frame() final;
virtual bool next_frame() final;

virtual object<Context> create_context(const string& name, int width, int height, bool full_screen) final;
virtual object<Context> create_context(const string& name, uvec2 size, bool full_screen) final;

void init(GfxContext& context);

Expand Down
6 changes: 3 additions & 3 deletions src/meta/ctx.meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void mud_Key__to_string(void* val, string& str) { str = g_enu[type<mud::Key>().m
void mud_Key__to_value(const string& str, void* val) { (*static_cast<mud::Key*>(val)) = mud::Key(g_enu[type<mud::Key>().m_id]->value(str.c_str())); }
void mud_MouseButtonCode__to_string(void* val, string& str) { str = g_enu[type<mud::MouseButtonCode>().m_id]->name(uint32_t((*static_cast<mud::MouseButtonCode*>(val)))); }
void mud_MouseButtonCode__to_value(const string& str, void* val) { (*static_cast<mud::MouseButtonCode*>(val)) = mud::MouseButtonCode(g_enu[type<mud::MouseButtonCode>().m_id]->value(str.c_str())); }
void mud_Context_reset(void* object, span<void*> args, void*& result) { UNUSED(result); (*static_cast<mud::Context*>(object)).reset(*static_cast<uint16_t*>(args[0]), *static_cast<uint16_t*>(args[1])); }
void mud_Context_reset(void* object, span<void*> args, void*& result) { UNUSED(result); (*static_cast<mud::Context*>(object)).reset_fb(*static_cast<uvec2*>(args[0])); }
void mud_Context_init_input(void* object, span<void*> args, void*& result) { UNUSED(result); (*static_cast<mud::Context*>(object)).init_input(*static_cast<mud::Mouse*>(args[0]), *static_cast<mud::Keyboard*>(args[1])); }
void mud_Context_next_frame(void* object, span<void*> args, void*& result) { UNUSED(args); (*static_cast<bool*>(result)) = (*static_cast<mud::Context*>(object)).next_frame(); }
void mud_Context_lock_mouse(void* object, span<void*> args, void*& result) { UNUSED(result); (*static_cast<mud::Context*>(object)).lock_mouse(*static_cast<bool*>(args[0])); }
Expand Down Expand Up @@ -131,8 +131,8 @@ namespace mud
static Member members[] = {
{ t, offsetof(mud::Context, m_resource_path), type<stl::string>(), "resource_path", nullptr, Member::Flags(Member::Value|Member::NonMutable), nullptr },
{ t, offsetof(mud::Context, m_title), type<stl::string>(), "title", nullptr, Member::Value, nullptr },
{ t, offsetof(mud::Context, m_width), type<uint>(), "width", nullptr, Member::Value, nullptr },
{ t, offsetof(mud::Context, m_height), type<uint>(), "height", nullptr, Member::Value, nullptr },
{ t, offsetof(mud::Context, m_size), type<uvec2>(), "size", nullptr, Member::Value, nullptr },
{ t, offsetof(mud::Context, m_fb_size), type<uvec2>(), "fb_size", nullptr, Member::Value, nullptr },
{ t, offsetof(mud::Context, m_full_screen), type<bool>(), "full_screen", nullptr, Member::Value, nullptr },
{ t, offsetof(mud::Context, m_active), type<bool>(), "active", &active_default, Member::Value, nullptr },
{ t, offsetof(mud::Context, m_shutdown), type<bool>(), "shutdown", &shutdown_default, Member::Value, nullptr },
Expand Down
2 changes: 1 addition & 1 deletion src/meta/gfx.meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void mud_Particles__construct_0(void* ref, span<void*> args) { UNUSED(args); new
void mud_Particles__copy_construct(void* ref, void* other) { new(stl::placeholder(), ref) mud::Particles((*static_cast<mud::Particles*>(other))); }
void mud_gfx_update_item_lights_0(span<void*> args, void*& result) { UNUSED(result); mud::gfx::update_item_lights(*static_cast<mud::Item*>(args[0])); }
void mud_gfx_update_item_aabb_1(span<void*> args, void*& result) { UNUSED(result); mud::gfx::update_item_aabb(*static_cast<mud::Item*>(args[0])); }
void mud_gfx_node_2(span<void*> args, void*& result) { result = &mud::gfx::node(*static_cast<mud::Gnode*>(args[0]), *static_cast<mud::Ref*>(args[1]), *static_cast<mud::vec3*>(args[2]), *static_cast<mud::quat*>(args[3]), *static_cast<mud::vec3*>(args[4])); }
void mud_gfx_node_2(span<void*> args, void*& result) { result = &mud::gfx::node(*static_cast<mud::Gnode*>(args[0]), Ref(), *static_cast<mud::vec3*>(args[2]), *static_cast<mud::quat*>(args[3]), *static_cast<mud::vec3*>(args[4])); }
void mud_gfx_shape_3(span<void*> args, void*& result) { result = &mud::gfx::shape(*static_cast<mud::Gnode*>(args[0]), *static_cast<mud::Shape*>(args[1]), *static_cast<mud::Symbol*>(args[2]), *static_cast<uint32_t*>(args[3]), static_cast<mud::Material*>(args[4]), *static_cast<size_t*>(args[5])); }
void mud_gfx_draw_4(span<void*> args, void*& result) { UNUSED(result); mud::gfx::draw(*static_cast<mud::Gnode*>(args[0]), *static_cast<mud::Shape*>(args[1]), *static_cast<mud::Symbol*>(args[2]), *static_cast<uint32_t*>(args[3])); }
void mud_gfx_sprite_5(span<void*> args, void*& result) { result = &mud::gfx::sprite(*static_cast<mud::Gnode*>(args[0]), *static_cast<mud::Image256*>(args[1]), *static_cast<mud::vec2*>(args[2]), *static_cast<uint32_t*>(args[3]), static_cast<mud::Material*>(args[4]), *static_cast<size_t*>(args[5])); }
Expand Down
3 changes: 3 additions & 0 deletions src/stl/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace stl
{
template<typename T, size_t N>
constexpr size_t array_size(T(&array)[N]) { return N; }

export_ template <class T, size_t Size>
struct array
{
Expand Down
4 changes: 2 additions & 2 deletions src/ui/UiRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ namespace mud
UiRenderer::~UiRenderer()
{}

void UiRenderer::render(Layer& target)
void UiRenderer::render(Layer& target, float pixel_ratio)
{
this->log_FPS();

m_debug_batch = 0;
static size_t prevBatch = 0;

m_vg.begin_frame({ vec2(0.f), target.m_frame.m_size });
m_vg.begin_frame({ vec2(0.f), target.m_frame.m_size }, pixel_ratio);

#ifdef MUD_UI_DRAW_CACHE
target.visit([&](Layer& layer) {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/UiRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace mud
UiRenderer(Vg& vg);
virtual ~UiRenderer();

void render(Layer& layer);
void render(Layer& layer, float pixel_ratio);// = 1.f);

// drawing implementation
void render_layer(Layer& layer);
Expand Down
Loading

0 comments on commit 99ff016

Please sign in to comment.