added ENABLE_MICROPROFILE option

This commit is contained in:
Anthony Pesch 2017-06-15 20:59:18 -04:00
parent b4d7f72a11
commit bb67d95860
4 changed files with 170 additions and 100 deletions

View File

@ -17,13 +17,14 @@ set(CMAKE_CXX_STANDARD 11)
#--------------------------------------------------
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
"Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
"Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif()
option(BUILD_LIBRETRO "Build libretro core" OFF)
option(BUILD_TOOLS "Build tools" ON)
option(BUILD_TESTS "Build tests" ON)
option(BUILD_TOOLS "Build tools" OFF)
option(BUILD_TESTS "Build tests" OFF)
option(ENABLE_MICROPROFILE "Enable MicroProfile" ON)
if(WIN32 OR MINGW)
set(PLATFORM_WINDOWS TRUE)
@ -57,7 +58,7 @@ check_function_exists(strcasecmp HAVE_STRCASECMP)
check_function_exists(strnlen HAVE_STRNLEN)
check_function_exists(strnstr HAVE_STRNSTR)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/core/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/src/core/config.h)
list(APPEND REDREAM_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/src)
list(APPEND REDREAM_INCLUDES ${CMAKE_CURRENT_BINARY_DIR}/src)
#--------------------------------------------------
# dynamic libs
@ -65,13 +66,65 @@ list(APPEND REDREAM_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/src)
# opengl
find_package(OpenGL REQUIRED)
list(APPEND REDREAM_INCLUDE_DIRS ${OPENGL_INCLUDE_DIR})
list(APPEND REDREAM_INCLUDES ${OPENGL_INCLUDE_DIR})
list(APPEND REDREAM_LIBS ${OPENGL_LIBRARIES})
#--------------------------------------------------
# static libs
#--------------------------------------------------
# capstone
set(CAPSTONE_ARM_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_ARM64_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_MIPS_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_PPC_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_SPARC_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_SYSZ_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_XCORE_SUPPORT OFF CACHE BOOL "")
add_subdirectory(deps/capstone EXCLUDE_FROM_ALL)
list(APPEND REDREAM_INCLUDES deps/capstone/include)
list(APPEND REDREAM_LIBS capstone-static)
# dirent
list(APPEND REDREAM_INCLUDES deps/dirent-1.21)
# gdb_server
list(APPEND REDREAM_INCLUDES deps/gdb_server)
# glew
set(BUILD_UTILS OFF CACHE BOOL "")
add_subdirectory(deps/glew-1.13.0/build/cmake EXCLUDE_FROM_ALL)
list(APPEND REDREAM_INCLUDES deps/glew-1.13.0/include)
list(APPEND REDREAM_LIBS glew_s)
list(APPEND REDREAM_DEFS GLEW_STATIC)
# imgui
add_library(imgui STATIC
deps/cimgui/imgui/imgui.cpp
deps/cimgui/imgui/imgui_demo.cpp
deps/cimgui/imgui/imgui_draw.cpp
deps/cimgui/cimgui/cimgui.cpp
deps/cimgui/cimgui/drawList.cpp
deps/cimgui/cimgui/fontAtlas.cpp
deps/cimgui/cimgui/listClipper.cpp)
list(APPEND REDREAM_INCLUDES deps/cimgui)
list(APPEND REDREAM_LIBS imgui)
# inih
add_library(inih STATIC deps/inih/ini.c)
list(APPEND REDREAM_INCLUDES deps/inih)
list(APPEND REDREAM_LIBS inih)
# microprofile
list(APPEND MICROPROFILE_INCLUDES deps/microprofile)
# xbyak
list(APPEND REDREAM_INCLUDES deps/xbyak-4.901)
#--------------------------------------------------
# optional libs
#--------------------------------------------------
# sdl2
if(NOT BUILD_LIBRETRO)
set(DIRECTX OFF CACHE BOOL "")
@ -89,62 +142,13 @@ if(APPLE)
set(SDL_FRAMEWORK_CARBON 1)
endif()
add_subdirectory(deps/sdl2-2.0.5 EXCLUDE_FROM_ALL)
list(APPEND REDREAM_INCLUDE_DIRS deps/sdl2-2.0.5/include)
list(APPEND SDL_INCLUDE_DIRS deps/sdl2-2.0.5/include)
if(MINGW)
list(APPEND SDL_LIBS mingw32)
endif()
list(APPEND SDL_LIBS SDL2main SDL2-static)
endif()
# capstone
set(CAPSTONE_ARM_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_ARM64_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_MIPS_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_PPC_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_SPARC_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_SYSZ_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_XCORE_SUPPORT OFF CACHE BOOL "")
add_subdirectory(deps/capstone EXCLUDE_FROM_ALL)
list(APPEND REDREAM_INCLUDE_DIRS deps/capstone/include)
list(APPEND REDREAM_LIBS capstone-static)
# dirent
list(APPEND REDREAM_INCLUDE_DIRS deps/dirent-1.21)
# gdb_server
list(APPEND REDREAM_INCLUDE_DIRS deps/gdb_server)
# glew
set(BUILD_UTILS OFF CACHE BOOL "")
add_subdirectory(deps/glew-1.13.0/build/cmake EXCLUDE_FROM_ALL)
list(APPEND REDREAM_INCLUDE_DIRS deps/glew-1.13.0/include)
list(APPEND REDREAM_LIBS glew_s)
list(APPEND REDREAM_DEFS GLEW_STATIC)
# imgui
add_library(imgui STATIC
deps/cimgui/imgui/imgui.cpp
deps/cimgui/imgui/imgui_demo.cpp
deps/cimgui/imgui/imgui_draw.cpp
deps/cimgui/cimgui/cimgui.cpp
deps/cimgui/cimgui/drawList.cpp
deps/cimgui/cimgui/fontAtlas.cpp
deps/cimgui/cimgui/listClipper.cpp)
list(APPEND REDREAM_INCLUDE_DIRS deps/cimgui)
list(APPEND REDREAM_LIBS imgui)
# inih
add_library(inih STATIC deps/inih/ini.c)
list(APPEND REDREAM_INCLUDE_DIRS deps/inih)
list(APPEND REDREAM_LIBS inih)
# microprofile
list(APPEND REDREAM_INCLUDE_DIRS deps/microprofile)
# xbyak
list(APPEND REDREAM_INCLUDE_DIRS deps/xbyak-4.901)
#--------------------------------------------------
# format
#--------------------------------------------------
@ -158,8 +162,7 @@ endif()
#--------------------------------------------------
# relib
# intermediate "object library" to speed up recc / retest compiles
# redream sources, includes and libs, common to multiple projects
#--------------------------------------------------
set(REDREAM_SOURCES
@ -230,10 +233,7 @@ set(REDREAM_SOURCES
src/jit/pass_stats.c
src/render/gl_backend.c
src/render/imgui.cc
src/render/microprofile.cc
src/emulator.c
src/tracer.c
)
src/render/microprofile.cc)
if(PLATFORM_WINDOWS)
list(APPEND REDREAM_DEFS PLATFORM_WINDOWS=1)
@ -273,9 +273,6 @@ if(ARCH_X64)
src/jit/backend/x64/x64_dispatch.cc)
endif()
# assign source groups for visual studio projects
source_group_by_dir(REDREAM_SOURCES)
if(COMPILER_GCC OR COMPILER_CLANG)
list(APPEND REDREAM_FLAGS -pthread -fms-extensions -Wall -Wextra -Werror -Wno-unused-function -Wno-unused-parameter -Wno-unused-variable -Wno-strict-aliasing -D_GNU_SOURCE)
@ -300,22 +297,21 @@ elseif(PLATFORM_WINDOWS)
list(APPEND REDREAM_LIBS userenv ws2_32)
endif()
add_library(relib OBJECT ${REDREAM_SOURCES})
target_include_directories(relib SYSTEM PUBLIC ${REDREAM_INCLUDE_DIRS})
target_include_directories(relib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_compile_definitions(relib PRIVATE ${REDREAM_DEFS})
target_compile_options(relib PRIVATE ${REDREAM_FLAGS})
#--------------------------------------------------
# redream_libretro
#--------------------------------------------------
if(BUILD_LIBRETRO)
add_library(redream_libretro SHARED $<TARGET_OBJECTS:relib> src/host/retro_host.c)
set(REDREAM_LIBRETRO_SOURCES
${REDREAM_SOURCES}
src/host/retro_host.c
src/emulator.c)
source_group_by_dir(REDREAM_LIBRETRO_SOURCES)
add_library(redream_libretro SHARED ${REDREAM_LIBRETRO_SOURCES})
set_target_properties(redream_libretro PROPERTIES PREFIX "")
set_target_properties(redream_libretro PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
target_include_directories(redream_libretro SYSTEM PUBLIC ${REDREAM_INCLUDE_DIRS} deps/libretro/include)
target_include_directories(redream_libretro SYSTEM PUBLIC ${REDREAM_INCLUDES} deps/libretro/include)
target_include_directories(redream_libretro PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(redream_libretro ${REDREAM_LIBS})
target_compile_definitions(redream_libretro PRIVATE ${REDREAM_DEFS})
@ -337,13 +333,24 @@ endif()
# redream
#--------------------------------------------------
add_executable(redream $<TARGET_OBJECTS:relib> src/host/sdl_host.c)
target_include_directories(redream SYSTEM PUBLIC ${REDREAM_INCLUDE_DIRS})
set(REDREAM_SDL_SOURCES
${REDREAM_SOURCES}
src/host/sdl_host.c
src/emulator.c
src/tracer.c)
source_group_by_dir(REDREAM_SDL_SOURCES)
add_executable(redream ${REDREAM_SDL_SOURCES})
target_include_directories(redream SYSTEM PUBLIC ${REDREAM_INCLUDES} ${MICROPROFILE_INCLUDE_DIRS} ${SDL_INCLUDE_DIRS})
target_include_directories(redream PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(redream ${REDREAM_LIBS} ${SDL_LIBS})
target_compile_definitions(redream PRIVATE ${REDREAM_DEFS})
target_compile_options(redream PRIVATE ${REDREAM_FLAGS})
if(ENABLE_MICROPROFILE)
target_compile_definitions(redream PRIVATE ENABLE_MICROPROFILE=1)
endif()
#--------------------------------------------------
# recc
#--------------------------------------------------
@ -351,22 +358,34 @@ target_compile_options(redream PRIVATE ${REDREAM_FLAGS})
if(BUILD_TOOLS)
if(ARCH_X64)
add_executable(recc $<TARGET_OBJECTS:relib> src/host/null_host.c tools/recc/main.c)
target_include_directories(recc SYSTEM PUBLIC ${REDREAM_INCLUDE_DIRS})
set(RECC_SOURCES
${REDREAM_SOURCES}
src/host/null_host.c
tools/recc/main.c)
source_group_by_dir(RECC_SOURCES)
add_executable(recc ${RECC_SOURCES})
target_include_directories(recc SYSTEM PUBLIC ${REDREAM_INCLUDES})
target_include_directories(recc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(recc ${REDREAM_LIBS})
target_compile_definitions(recc PRIVATE ${REDREAM_DEFS} MICROPROFILE_ENABLED=0)
target_compile_definitions(recc PRIVATE ${REDREAM_DEFS})
target_compile_options(recc PRIVATE ${REDREAM_FLAGS})
endif()
add_executable(retrace $<TARGET_OBJECTS:relib>
set(RETRACE_SOURCES
${REDREAM_SOURCES}
src/host/null_host.c
tools/retrace/depth.c
tools/retrace/main.c)
target_include_directories(retrace SYSTEM PUBLIC ${REDREAM_INCLUDE_DIRS})
source_group_by_dir(RETRACE_SOURCES)
add_executable(retrace ${RETRACE_SOURCES})
target_include_directories(retrace SYSTEM PUBLIC ${REDREAM_INCLUDES})
target_include_directories(retrace PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(retrace ${REDREAM_LIBS})
target_compile_definitions(retrace PRIVATE ${REDREAM_DEFS} MICROPROFILE_ENABLED=0)
target_compile_definitions(retrace PRIVATE ${REDREAM_DEFS})
target_compile_options(retrace PRIVATE ${REDREAM_FLAGS})
endif()
@ -478,6 +497,7 @@ endif()
# build test binary
set(RETEST_SOURCES
${REDREAM_SOURCES}
src/host/null_host.c
test/test_dead_code_elimination.c
test/test_interval_tree.c
@ -486,9 +506,10 @@ set(RETEST_SOURCES
test/test_sh4.c
${asm_inc}
test/retest.c)
source_group_by_dir(RETEST_SOURCES)
add_executable(retest $<TARGET_OBJECTS:relib> ${RETEST_SOURCES})
target_include_directories(retest PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/test ${REDREAM_INCLUDE_DIRS})
add_executable(retest ${RETEST_SOURCES})
target_include_directories(retest PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/test ${REDREAM_INCLUDES})
target_link_libraries(retest ${REDREAM_LIBS})
target_compile_definitions(retest PRIVATE ${REDREAM_DEFS})
target_compile_options(retest PRIVATE ${REDREAM_FLAGS})

View File

@ -1,19 +1,8 @@
#include <microprofile.h>
#include <atomic>
/* clang-format off */
#if MICROPROFILE_ENABLED == 0
#define MICROPROFILE_MAX_COUNTERS 1
#define MicroProfileInit() do{}while(0)
#define MicroProfileGetToken(group, name, color, type) 0
#define MicroProfileGetCounterToken(name) 0
#define MicroProfileEnter(tok) 0
#define MicroProfileLeave(tok, tick) do{}while(0)
#define MicroProfileCounterAdd(tok, v) do{}while(0)
#define MicroProfileCounterSet(tok, v) do{}while(0)
#define MicroProfileCounterLoad(tok) 0
#if ENABLE_MICROPROFILE
#include <microprofile.h>
#endif
/* clang-format on */
extern "C" {
@ -21,9 +10,11 @@ extern "C" {
#include "core/time.h"
static struct {
#if ENABLE_MICROPROFILE
std::atomic<int64_t> counters[MICROPROFILE_MAX_COUNTERS];
int aggregate[MICROPROFILE_MAX_COUNTERS];
int64_t last_aggregation;
#endif
} prof;
static inline float hue_to_rgb(float p, float q, float t) {
@ -85,31 +76,48 @@ static uint32_t prof_scope_color(const char *name) {
prof_token_t prof_get_token(const char *group, const char *name) {
prof_init();
#if ENABLE_MICROPROFILE
uint32_t color = prof_scope_color(name);
return MicroProfileGetToken(group, name, color, MicroProfileTokenTypeCpu);
#else
return 0;
#endif
}
prof_token_t prof_get_counter_token(const char *name) {
prof_init();
#if ENABLE_MICROPROFILE
prof_token_t tok = MicroProfileGetCounterToken(name);
prof.aggregate[tok] = 0;
return tok;
#else
return 0;
#endif
}
prof_token_t prof_get_aggregate_token(const char *name) {
prof_init();
#if ENABLE_MICROPROFILE
prof_token_t tok = MicroProfileGetCounterToken(name);
prof.aggregate[tok] = 1;
return tok;
#else
return 0;
#endif
}
void prof_flip() {
/* flip frame-based profile zones at the end of every frame */
/* flip frame-based profile zones at the end of every frame */
#if ENABLE_MICROPROFILE
MicroProfileFlip();
#endif
}
void prof_update(int64_t now) {
/* update time-based aggregate counters every second */
/* update time-based aggregate counters every second */
#if ENABLE_MICROPROFILE
int64_t next_aggregation = prof.last_aggregation + NS_PER_SEC;
if (now > next_aggregation) {
@ -124,41 +132,60 @@ void prof_update(int64_t now) {
prof.last_aggregation = now;
}
#endif
}
void prof_counter_set(prof_token_t tok, int64_t count) {
#if ENABLE_MICROPROFILE
if (prof.aggregate[tok]) {
prof.counters[tok].store(count);
} else {
MicroProfileCounterSet(tok, count);
}
#endif
}
void prof_counter_add(prof_token_t tok, int64_t count) {
#if ENABLE_MICROPROFILE
if (prof.aggregate[tok]) {
prof.counters[tok].fetch_add(count);
} else {
MicroProfileCounterAdd(tok, count);
}
#endif
}
int64_t prof_counter_load(prof_token_t tok) {
#if ENABLE_MICROPROFILE
return MicroProfileCounterLoad(tok);
#else
return 0;
#endif
}
void prof_leave(prof_token_t tok, uint64_t tick) {
#if ENABLE_MICROPROFILE
MicroProfileLeave(tok, tick);
#endif
}
uint64_t prof_enter(prof_token_t tok) {
#if ENABLE_MICROPROFILE
return MicroProfileEnter(tok);
#else
return 0;
#endif
}
void prof_shutdown() {
#if ENABLE_MICROPROFILE
MicroProfileShutdown();
#endif
}
void prof_init() {
#if ENABLE_MICROPROFILE
MicroProfileInit();
#endif
}
}

View File

@ -40,7 +40,6 @@ typedef struct iso733 {
uint32_t be;
} iso733_t;
/* iso 9660 data structures */
#pragma pack(push, 1)

View File

@ -1,3 +1,4 @@
#if ENABLE_MICROPROFILE
#define MICROPROFILE_WEBSERVER 0
#define MICROPROFILE_GPU_TIMERS 0
#define MICROPROFILE_ENABLED 1
@ -8,6 +9,7 @@
#define MICROPROFILE_CONTEXT_SWITCH_TRACE 0
#include <microprofile.h>
#include <microprofileui.h>
#endif
extern "C" {
#include "core/assert.h"
@ -47,6 +49,7 @@ static struct microprofile *s_mp;
static struct ui_vertex *mp_alloc_verts(struct microprofile *mp,
const struct ui_surface &desc,
int count) {
#if ENABLE_MICROPROFILE
CHECK(mp->num_verts + count <= MAX_2D_VERTICES);
uint32_t first_vert = mp->num_verts;
mp->num_verts += count;
@ -76,10 +79,13 @@ static struct ui_vertex *mp_alloc_verts(struct microprofile *mp,
next_surf.num_verts = count;
mp->num_surfs++;
return &mp->verts[first_vert];
#endif
return NULL;
}
static void mp_draw_text(struct microprofile *mp, int x, int y, uint32_t color,
const char *text) {
#if ENABLE_MICROPROFILE
float fx = static_cast<float>(x);
float fy = static_cast<float>(y);
float fy2 = fy + (MICROPROFILE_TEXT_HEIGHT + 1);
@ -129,10 +135,12 @@ static void mp_draw_text(struct microprofile *mp, int x, int y, uint32_t color,
vertex += 6;
}
#endif
}
static void mp_draw_box(struct microprofile *mp, int x0, int y0, int x1, int y1,
uint32_t color, enum box_type type) {
#if ENABLE_MICROPROFILE
struct ui_vertex *vertex = mp_alloc_verts(mp, {PRIM_TRIANGLES,
0,
BLEND_SRC_ALPHA,
@ -186,10 +194,12 @@ static void mp_draw_box(struct microprofile *mp, int x0, int y0, int x1, int y1,
Q3(vertex, xy[1], (float)y1);
Q3(vertex, color, color1);
}
#endif
}
static void mp_draw_line(struct microprofile *mp, float *verts, int num_verts,
uint32_t color) {
#if ENABLE_MICROPROFILE
CHECK(num_verts);
struct ui_vertex *vertex = mp_alloc_verts(mp, {PRIM_LINES,
@ -211,9 +221,11 @@ static void mp_draw_line(struct microprofile *mp, float *verts, int num_verts,
vertex[1].color = color;
vertex += 2;
}
#endif
}
void mp_render(struct microprofile *mp) {
#if ENABLE_MICROPROFILE
s_mp = mp;
int width = r_viewport_width(mp->r);
@ -235,13 +247,17 @@ void mp_render(struct microprofile *mp) {
/* reset surfaces */
mp->num_surfs = 0;
mp->num_verts = 0;
#endif
}
void mp_mousemove(struct microprofile *mp, int x, int y) {
#if ENABLE_MICROPROFILE
MicroProfileMousePosition(x, y, 0);
#endif
}
void mp_keydown(struct microprofile *mp, enum keycode key, int16_t value) {
#if ENABLE_MICROPROFILE
if (key == K_F2) {
if (value > 0) {
MicroProfileToggleDisplayMode();
@ -253,10 +269,13 @@ void mp_keydown(struct microprofile *mp, enum keycode key, int16_t value) {
int down = value > 0;
MicroProfileMouseButton(0, down);
}
#endif
}
void mp_destroy(struct microprofile *mp) {
#if ENABLE_MICROPROFILE
r_destroy_texture(mp->r, mp->font_texture);
#endif
free(mp);
}
@ -265,6 +284,7 @@ struct microprofile *mp_create(struct render_backend *r) {
struct microprofile *mp = reinterpret_cast<struct microprofile *>(
calloc(1, sizeof(struct microprofile)));
#if ENABLE_MICROPROFILE
mp->r = r;
/* register and enable cpu and gpu groups by default */
@ -285,12 +305,14 @@ struct microprofile *mp_create(struct render_backend *r) {
r_create_texture(mp->r, PXL_RGBA, FILTER_NEAREST, WRAP_CLAMP_TO_EDGE,
WRAP_CLAMP_TO_EDGE, 0, FONT_WIDTH, FONT_HEIGHT,
reinterpret_cast<const uint8_t *>(s_font_data));
#endif
return mp;
}
/* microprofile expects the following three functions to be defined, they're
called during MicroProfileDraw */
#if ENABLE_MICROPROFILE
void MicroProfileDrawText(int x, int y, uint32_t color, const char *text,
uint32_t len) {
/* microprofile provides 24-bit rgb values for text color */
@ -323,3 +345,4 @@ void MicroProfileDrawLine2D(uint32_t num_vertices, float *vertices,
mp_draw_line(s_mp, vertices, num_vertices, color);
}
#endif