diff --git a/.gitignore b/.gitignore index 0edfb01a5b7..a3357981d32 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ !/samples/ !/scripts/ !/src/ +!/shaders/ !/tests/ !/doxygen/ !/web/ diff --git a/3rdparty/bgfx/tools/bin/windows/.gitignore b/3rdparty/bgfx/tools/bin/windows/.gitignore deleted file mode 100644 index 72e8ffc0db8..00000000000 --- a/3rdparty/bgfx/tools/bin/windows/.gitignore +++ /dev/null @@ -1 +0,0 @@ -* diff --git a/3rdparty/bgfx/tools/bin/windows/geometryc.exe b/3rdparty/bgfx/tools/bin/windows/geometryc.exe new file mode 100644 index 00000000000..f1e001cf2ea Binary files /dev/null and b/3rdparty/bgfx/tools/bin/windows/geometryc.exe differ diff --git a/3rdparty/bgfx/tools/bin/windows/shaderc.exe b/3rdparty/bgfx/tools/bin/windows/shaderc.exe new file mode 100644 index 00000000000..0e1b0346140 Binary files /dev/null and b/3rdparty/bgfx/tools/bin/windows/shaderc.exe differ diff --git a/3rdparty/bgfx/tools/bin/windows/texturec.exe b/3rdparty/bgfx/tools/bin/windows/texturec.exe new file mode 100644 index 00000000000..652b3b729de Binary files /dev/null and b/3rdparty/bgfx/tools/bin/windows/texturec.exe differ diff --git a/makefile b/makefile index eae7d59071c..2595a9bba6b 100644 --- a/makefile +++ b/makefile @@ -1367,3 +1367,7 @@ cppcheck: @echo Generate CppCheck analysis report cppcheck --enable=all src/ $(CPPCHECK_PARAMS) -j9 +.PHONY: shaders + +shaders: + $(SILENT) $(MAKE) -C $(SRC)/osd/modules/render/bgfx rebuild diff --git a/shaders/dx11/fs_line.bin b/shaders/dx11/fs_line.bin new file mode 100644 index 00000000000..5bd281a0889 Binary files /dev/null and b/shaders/dx11/fs_line.bin differ diff --git a/shaders/dx11/fs_quad.bin b/shaders/dx11/fs_quad.bin new file mode 100644 index 00000000000..5bd281a0889 Binary files /dev/null and b/shaders/dx11/fs_quad.bin differ diff --git a/shaders/dx11/fs_quad_texture.bin b/shaders/dx11/fs_quad_texture.bin new file mode 100644 index 00000000000..80d85fb1107 Binary files /dev/null and b/shaders/dx11/fs_quad_texture.bin differ diff --git a/shaders/dx11/vs_line.bin b/shaders/dx11/vs_line.bin new file mode 100644 index 00000000000..24d50eb25ac Binary files /dev/null and b/shaders/dx11/vs_line.bin differ diff --git a/shaders/dx11/vs_quad.bin b/shaders/dx11/vs_quad.bin new file mode 100644 index 00000000000..24d50eb25ac Binary files /dev/null and b/shaders/dx11/vs_quad.bin differ diff --git a/shaders/dx11/vs_quad_texture.bin b/shaders/dx11/vs_quad_texture.bin new file mode 100644 index 00000000000..b0bebf9da4c Binary files /dev/null and b/shaders/dx11/vs_quad_texture.bin differ diff --git a/shaders/dx9/fs_line.bin b/shaders/dx9/fs_line.bin new file mode 100644 index 00000000000..5bfd5497f0b Binary files /dev/null and b/shaders/dx9/fs_line.bin differ diff --git a/shaders/dx9/fs_quad.bin b/shaders/dx9/fs_quad.bin new file mode 100644 index 00000000000..5bfd5497f0b Binary files /dev/null and b/shaders/dx9/fs_quad.bin differ diff --git a/shaders/dx9/fs_quad_texture.bin b/shaders/dx9/fs_quad_texture.bin new file mode 100644 index 00000000000..ef8fa0730c3 Binary files /dev/null and b/shaders/dx9/fs_quad_texture.bin differ diff --git a/shaders/dx9/vs_line.bin b/shaders/dx9/vs_line.bin new file mode 100644 index 00000000000..9bd4a9797e0 Binary files /dev/null and b/shaders/dx9/vs_line.bin differ diff --git a/shaders/dx9/vs_quad.bin b/shaders/dx9/vs_quad.bin new file mode 100644 index 00000000000..9bd4a9797e0 Binary files /dev/null and b/shaders/dx9/vs_quad.bin differ diff --git a/shaders/dx9/vs_quad_texture.bin b/shaders/dx9/vs_quad_texture.bin new file mode 100644 index 00000000000..ea94c83e62d Binary files /dev/null and b/shaders/dx9/vs_quad_texture.bin differ diff --git a/shaders/gles/fs_line.bin b/shaders/gles/fs_line.bin new file mode 100644 index 00000000000..de509a3eeb5 Binary files /dev/null and b/shaders/gles/fs_line.bin differ diff --git a/shaders/gles/fs_quad.bin b/shaders/gles/fs_quad.bin new file mode 100644 index 00000000000..de509a3eeb5 Binary files /dev/null and b/shaders/gles/fs_quad.bin differ diff --git a/shaders/gles/fs_quad_texture.bin b/shaders/gles/fs_quad_texture.bin new file mode 100644 index 00000000000..22e2f0c23d0 Binary files /dev/null and b/shaders/gles/fs_quad_texture.bin differ diff --git a/shaders/gles/vs_line.bin b/shaders/gles/vs_line.bin new file mode 100644 index 00000000000..db0ff91e152 Binary files /dev/null and b/shaders/gles/vs_line.bin differ diff --git a/shaders/gles/vs_quad.bin b/shaders/gles/vs_quad.bin new file mode 100644 index 00000000000..db0ff91e152 Binary files /dev/null and b/shaders/gles/vs_quad.bin differ diff --git a/shaders/gles/vs_quad_texture.bin b/shaders/gles/vs_quad_texture.bin new file mode 100644 index 00000000000..9dd618de8a0 Binary files /dev/null and b/shaders/gles/vs_quad_texture.bin differ diff --git a/shaders/glsl/fs_line.bin b/shaders/glsl/fs_line.bin new file mode 100644 index 00000000000..6f8e3df8290 Binary files /dev/null and b/shaders/glsl/fs_line.bin differ diff --git a/shaders/glsl/fs_quad.bin b/shaders/glsl/fs_quad.bin new file mode 100644 index 00000000000..6f8e3df8290 Binary files /dev/null and b/shaders/glsl/fs_quad.bin differ diff --git a/shaders/glsl/fs_quad_texture.bin b/shaders/glsl/fs_quad_texture.bin new file mode 100644 index 00000000000..db0fe2b487b Binary files /dev/null and b/shaders/glsl/fs_quad_texture.bin differ diff --git a/shaders/glsl/vs_line.bin b/shaders/glsl/vs_line.bin new file mode 100644 index 00000000000..7c8490550c7 Binary files /dev/null and b/shaders/glsl/vs_line.bin differ diff --git a/shaders/glsl/vs_quad.bin b/shaders/glsl/vs_quad.bin new file mode 100644 index 00000000000..7c8490550c7 Binary files /dev/null and b/shaders/glsl/vs_quad.bin differ diff --git a/shaders/glsl/vs_quad_texture.bin b/shaders/glsl/vs_quad_texture.bin new file mode 100644 index 00000000000..af6c9349f41 Binary files /dev/null and b/shaders/glsl/vs_quad_texture.bin differ diff --git a/shaders/metal/fs_line.bin b/shaders/metal/fs_line.bin new file mode 100644 index 00000000000..fb726f353c0 Binary files /dev/null and b/shaders/metal/fs_line.bin differ diff --git a/shaders/metal/fs_quad.bin b/shaders/metal/fs_quad.bin new file mode 100644 index 00000000000..fb726f353c0 Binary files /dev/null and b/shaders/metal/fs_quad.bin differ diff --git a/shaders/metal/fs_quad_texture.bin b/shaders/metal/fs_quad_texture.bin new file mode 100644 index 00000000000..6a3da392480 Binary files /dev/null and b/shaders/metal/fs_quad_texture.bin differ diff --git a/shaders/metal/vs_line.bin b/shaders/metal/vs_line.bin new file mode 100644 index 00000000000..d2eeed80dfd Binary files /dev/null and b/shaders/metal/vs_line.bin differ diff --git a/shaders/metal/vs_quad.bin b/shaders/metal/vs_quad.bin new file mode 100644 index 00000000000..d2eeed80dfd Binary files /dev/null and b/shaders/metal/vs_quad.bin differ diff --git a/shaders/metal/vs_quad_texture.bin b/shaders/metal/vs_quad_texture.bin new file mode 100644 index 00000000000..122b7dee5c3 Binary files /dev/null and b/shaders/metal/vs_quad_texture.bin differ diff --git a/src/osd/modules/render/bgfx/fs_line.sc b/src/osd/modules/render/bgfx/fs_line.sc new file mode 100644 index 00000000000..1fcd3b27284 --- /dev/null +++ b/src/osd/modules/render/bgfx/fs_line.sc @@ -0,0 +1,11 @@ +$input v_color0 + +// license:BSD-3-Clause +// copyright-holders:Dario Manesku + +#include "../../../../../3rdparty/bgfx/examples/common/common.sh" + +void main() +{ + gl_FragColor = v_color0; +} diff --git a/src/osd/modules/render/bgfx/fs_quad.sc b/src/osd/modules/render/bgfx/fs_quad.sc new file mode 100644 index 00000000000..1fcd3b27284 --- /dev/null +++ b/src/osd/modules/render/bgfx/fs_quad.sc @@ -0,0 +1,11 @@ +$input v_color0 + +// license:BSD-3-Clause +// copyright-holders:Dario Manesku + +#include "../../../../../3rdparty/bgfx/examples/common/common.sh" + +void main() +{ + gl_FragColor = v_color0; +} diff --git a/src/osd/modules/render/bgfx/fs_quad_texture.sc b/src/osd/modules/render/bgfx/fs_quad_texture.sc new file mode 100644 index 00000000000..5a45a056171 --- /dev/null +++ b/src/osd/modules/render/bgfx/fs_quad_texture.sc @@ -0,0 +1,14 @@ +$input v_color0, v_texcoord0 + +// license:BSD-3-Clause +// copyright-holders:Dario Manesku + +#include "../../../../../3rdparty/bgfx/examples/common/common.sh" + +SAMPLER2D(s_tex, 0); + +void main() +{ + vec4 texel = texture2D(s_tex, v_texcoord0); + gl_FragColor = texel * v_color0; +} diff --git a/src/osd/modules/render/bgfx/makefile b/src/osd/modules/render/bgfx/makefile new file mode 100644 index 00000000000..86735a61c38 --- /dev/null +++ b/src/osd/modules/render/bgfx/makefile @@ -0,0 +1,13 @@ +BGFX_DIR=../../../../../3rdparty/bgfx +RUNTIME_DIR=../../../../.. +BUILD_DIR=../../../../../build + +include $(BGFX_DIR)/scripts/shader.mk + +rebuild: + @make -s --no-print-directory TARGET=0 clean all + @make -s --no-print-directory TARGET=1 clean all + @make -s --no-print-directory TARGET=2 clean all + @make -s --no-print-directory TARGET=3 clean all + @make -s --no-print-directory TARGET=4 clean all + @make -s --no-print-directory TARGET=5 clean all diff --git a/src/osd/modules/render/bgfx/varying.def.sc b/src/osd/modules/render/bgfx/varying.def.sc new file mode 100644 index 00000000000..4745725e015 --- /dev/null +++ b/src/osd/modules/render/bgfx/varying.def.sc @@ -0,0 +1,7 @@ +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); +vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0); +vec3 v_pos : TEXCOORD1 = vec3(0.0, 0.0, 0.0); + +vec3 a_position : POSITION; +vec4 a_color0 : COLOR0; +vec2 a_texcoord0 : TEXCOORD0; diff --git a/src/osd/modules/render/bgfx/vs_line.sc b/src/osd/modules/render/bgfx/vs_line.sc new file mode 100644 index 00000000000..874ffcf46ac --- /dev/null +++ b/src/osd/modules/render/bgfx/vs_line.sc @@ -0,0 +1,13 @@ +$input a_position, a_color0 +$output v_color0 + +// license:BSD-3-Clause +// copyright-holders:Dario Manesku + +#include "../../../../../3rdparty/bgfx/examples/common/common.sh" + +void main() +{ + gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0)); + v_color0 = a_color0; +} diff --git a/src/osd/modules/render/bgfx/vs_quad.sc b/src/osd/modules/render/bgfx/vs_quad.sc new file mode 100644 index 00000000000..84e23c5a09b --- /dev/null +++ b/src/osd/modules/render/bgfx/vs_quad.sc @@ -0,0 +1,13 @@ +$input a_position, a_color0 +$output v_color0 + +// license:BSD-3-Clause +// copyright-holders:Dario Manesku + +#include "../../../../../3rdparty/bgfx/examples/common/common.sh" + +void main() +{ + gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0)); + v_color0 = a_color0; +} diff --git a/src/osd/modules/render/bgfx/vs_quad_texture.sc b/src/osd/modules/render/bgfx/vs_quad_texture.sc new file mode 100644 index 00000000000..c3699f6fbd9 --- /dev/null +++ b/src/osd/modules/render/bgfx/vs_quad_texture.sc @@ -0,0 +1,14 @@ +$input a_position, a_texcoord0, a_color0 +$output v_texcoord0, v_color0 + +// license:BSD-3-Clause +// copyright-holders:Dario Manesku + +#include "../../../../../3rdparty/bgfx/examples/common/common.sh" + +void main() +{ + gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0)); + v_texcoord0 = a_texcoord0; + v_color0 = a_color0; +} diff --git a/src/osd/modules/render/drawbgfx.cpp b/src/osd/modules/render/drawbgfx.cpp index ee808bfc197..20b92aeff71 100644 --- a/src/osd/modules/render/drawbgfx.cpp +++ b/src/osd/modules/render/drawbgfx.cpp @@ -1,5 +1,5 @@ // license:BSD-3-Clause -// copyright-holders:Miodrag Milanovic +// copyright-holders:Miodrag Milanovic, Dario Manesku, Branimir Karadzic //============================================================ // // drawbgfx.c - BGFX drawer @@ -30,6 +30,8 @@ #include #include +#include +#include //============================================================ // DEBUGGING @@ -110,6 +112,11 @@ public: INT64 m_last_blit_time; INT64 m_last_blit_pixels; + bgfx::ProgramHandle m_progQuad; + bgfx::ProgramHandle m_progQuadTexture; + bgfx::ProgramHandle m_progLine; + bgfx::UniformHandle m_s_texColor; + // Original display_mode }; @@ -155,7 +162,7 @@ static void drawbgfx_exit(void) //============================================================ // renderer_bgfx::create //============================================================ - +bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName); int renderer_bgfx::create() { // create renderer @@ -177,7 +184,12 @@ int renderer_bgfx::create() #endif // Enable debug text. - bgfx::setDebug(BGFX_DEBUG_STATS);// BGFX_DEBUG_TEXT); + bgfx::setDebug(BGFX_DEBUG_TEXT); //BGFX_DEBUG_STATS + // Create program from shaders. + m_progQuad = loadProgram("vs_quad", "fs_quad"); + m_progQuadTexture = loadProgram("vs_quad_texture", "fs_quad_texture"); + m_progLine = loadProgram("vs_line", "fs_line"); + m_s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1); osd_printf_verbose("Leave drawsdl2_window_create\n"); return 0; @@ -192,6 +204,12 @@ void renderer_bgfx::destroy() // free the memory in the window // destroy_all_textures(); + // + bgfx::destroyUniform(m_s_texColor); + // Cleanup. + bgfx::destroyProgram(m_progQuad); + bgfx::destroyProgram(m_progQuadTexture); + bgfx::destroyProgram(m_progLine); // Shutdown bgfx. bgfx::shutdown(); @@ -215,39 +233,421 @@ int renderer_bgfx::xy_to_render_target(int x, int y, int *xt, int *yt) } #endif +static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const char* _filePath) +{ + if (bx::open(_reader, _filePath)) + { + uint32_t size = (uint32_t)bx::getSize(_reader); + const bgfx::Memory* mem = bgfx::alloc(size + 1); + bx::read(_reader, mem->data, size); + bx::close(_reader); + mem->data[mem->size - 1] = '\0'; + return mem; + } + + return NULL; +} +static bgfx::ShaderHandle loadShader(bx::FileReaderI* _reader, const char* _name) +{ + char filePath[512]; + + const char* shaderPath = "shaders/dx9/"; + + switch (bgfx::getRendererType()) + { + case bgfx::RendererType::Direct3D11: + case bgfx::RendererType::Direct3D12: + shaderPath = "shaders/dx11/"; + break; + + case bgfx::RendererType::OpenGL: + shaderPath = "shaders/glsl/"; + break; + + case bgfx::RendererType::Metal: + shaderPath = "shaders/metal/"; + break; + + case bgfx::RendererType::OpenGLES: + shaderPath = "shaders/gles/"; + break; + + default: + break; + } + + strcpy(filePath, shaderPath); + strcat(filePath, _name); + strcat(filePath, ".bin"); + + return bgfx::createShader(loadMem(_reader, filePath)); +} + +bgfx::ProgramHandle loadProgram(bx::FileReaderI* _reader, const char* _vsName, const char* _fsName) +{ + bgfx::ShaderHandle vsh = loadShader(_reader, _vsName); + bgfx::ShaderHandle fsh = BGFX_INVALID_HANDLE; + if (NULL != _fsName) + { + fsh = loadShader(_reader, _fsName); + } + + return bgfx::createProgram(vsh, fsh, true /* destroy shaders when program is destroyed */); +} +static auto s_fileReader = new bx::CrtFileReader; + +bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName) +{ + + return loadProgram(s_fileReader, _vsName, _fsName); +} //============================================================ // drawbgfx_window_draw //============================================================ +struct PosColorTexCoord0Vertex +{ + float m_x; + float m_y; + float m_z; + uint32_t m_rgba; + float m_u; + float m_v; + + static void init() + { + ms_decl.begin() + .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) + .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) + .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float) + .end(); + } + + static bgfx::VertexDecl ms_decl; +}; +bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl; + +void screenQuad(float _x1 + , float _y1 + , float _x2 + , float _y2 + , uint32_t _abgr + , render_quad_texuv uv + ) +{ + if (bgfx::checkAvailTransientVertexBuffer(6, PosColorTexCoord0Vertex::ms_decl)) + { + bgfx::TransientVertexBuffer vb; + bgfx::allocTransientVertexBuffer(&vb, 6, PosColorTexCoord0Vertex::ms_decl); + PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)vb.data; + + const float minx = _x1; + const float miny = _y1; + const float maxx = _x2; + const float maxy = _y2; + const float zz = 0.0f; + + vertex[0].m_x = minx; + vertex[0].m_y = miny; + vertex[0].m_z = zz; + vertex[0].m_rgba = _abgr; + vertex[0].m_u = uv.tl.u; + vertex[0].m_v = uv.tl.v; + + vertex[1].m_x = maxx; + vertex[1].m_y = miny; + vertex[1].m_z = zz; + vertex[1].m_rgba = _abgr; + vertex[1].m_u = uv.tr.u; + vertex[1].m_v = uv.tr.v; + + vertex[2].m_x = maxx; + vertex[2].m_y = maxy; + vertex[2].m_z = zz; + vertex[2].m_rgba = _abgr; + vertex[2].m_u = uv.br.u; + vertex[2].m_v = uv.br.v; + + vertex[3].m_x = maxx; + vertex[3].m_y = maxy; + vertex[3].m_z = zz; + vertex[3].m_rgba = _abgr; + vertex[3].m_u = uv.br.u; + vertex[3].m_v = uv.br.v; + + vertex[4].m_x = minx; + vertex[4].m_y = maxy; + vertex[4].m_z = zz; + vertex[4].m_rgba = _abgr; + vertex[4].m_u = uv.bl.u; + vertex[4].m_v = uv.bl.v; + + vertex[5].m_x = minx; + vertex[5].m_y = miny; + vertex[5].m_z = zz; + vertex[5].m_rgba = _abgr; + vertex[5].m_u = uv.tl.u; + vertex[5].m_v = uv.tl.v; + bgfx::setVertexBuffer(&vb); + } +} + + +struct PosColorVertex +{ + float m_x; + float m_y; + uint32_t m_abgr; + + static void init() + { + ms_decl + .begin() + .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float) + .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) + .end(); + } + + static bgfx::VertexDecl ms_decl; +}; +bgfx::VertexDecl PosColorVertex::ms_decl; + +#define MAX_TEMP_COORDS 100 + +void drawPolygon(const float* _coords, uint32_t _numCoords, float _r, uint32_t _abgr) +{ + float tempCoords[MAX_TEMP_COORDS * 2]; + float tempNormals[MAX_TEMP_COORDS * 2]; + + _numCoords = _numCoords < MAX_TEMP_COORDS ? _numCoords : MAX_TEMP_COORDS; + + for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++) + { + const float* v0 = &_coords[jj * 2]; + const float* v1 = &_coords[ii * 2]; + float dx = v1[0] - v0[0]; + float dy = v1[1] - v0[1]; + float d = sqrtf(dx * dx + dy * dy); + if (d > 0) + { + d = 1.0f / d; + dx *= d; + dy *= d; + } + + tempNormals[jj * 2 + 0] = dy; + tempNormals[jj * 2 + 1] = -dx; + } + + for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++) + { + float dlx0 = tempNormals[jj * 2 + 0]; + float dly0 = tempNormals[jj * 2 + 1]; + float dlx1 = tempNormals[ii * 2 + 0]; + float dly1 = tempNormals[ii * 2 + 1]; + float dmx = (dlx0 + dlx1) * 0.5f; + float dmy = (dly0 + dly1) * 0.5f; + float dmr2 = dmx * dmx + dmy * dmy; + if (dmr2 > 0.000001f) + { + float scale = 1.0f / dmr2; + if (scale > 10.0f) + { + scale = 10.0f; + } + + dmx *= scale; + dmy *= scale; + } + + tempCoords[ii * 2 + 0] = _coords[ii * 2 + 0] + dmx * _r; + tempCoords[ii * 2 + 1] = _coords[ii * 2 + 1] + dmy * _r; + } + + uint32_t numVertices = _numCoords * 6 + (_numCoords - 2) * 3; + if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorVertex::ms_decl)) + { + bgfx::TransientVertexBuffer tvb; + bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorVertex::ms_decl); + uint32_t trans = _abgr & 0xffffff; + + PosColorVertex* vertex = (PosColorVertex*)tvb.data; + for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++) + { + vertex->m_x = _coords[ii * 2 + 0]; + vertex->m_y = _coords[ii * 2 + 1]; + vertex->m_abgr = _abgr; + ++vertex; + + vertex->m_x = _coords[jj * 2 + 0]; + vertex->m_y = _coords[jj * 2 + 1]; + vertex->m_abgr = _abgr; + ++vertex; + + vertex->m_x = tempCoords[jj * 2 + 0]; + vertex->m_y = tempCoords[jj * 2 + 1]; + vertex->m_abgr = trans; + ++vertex; + + vertex->m_x = tempCoords[jj * 2 + 0]; + vertex->m_y = tempCoords[jj * 2 + 1]; + vertex->m_abgr = trans; + ++vertex; + + vertex->m_x = tempCoords[ii * 2 + 0]; + vertex->m_y = tempCoords[ii * 2 + 1]; + vertex->m_abgr = trans; + ++vertex; + + vertex->m_x = _coords[ii * 2 + 0]; + vertex->m_y = _coords[ii * 2 + 1]; + vertex->m_abgr = _abgr; + ++vertex; + } + + for (uint32_t ii = 2; ii < _numCoords; ++ii) + { + vertex->m_x = _coords[0]; + vertex->m_y = _coords[1]; + vertex->m_abgr = _abgr; + ++vertex; + + vertex->m_x = _coords[(ii - 1) * 2 + 0]; + vertex->m_y = _coords[(ii - 1) * 2 + 1]; + vertex->m_abgr = _abgr; + ++vertex; + + vertex->m_x = _coords[ii * 2 + 0]; + vertex->m_y = _coords[ii * 2 + 1]; + vertex->m_abgr = _abgr; + ++vertex; + } + + bgfx::setVertexBuffer(&tvb); + } +} + +void drawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _abgr, float _fth = 1.0f) +{ + float dx = _x1 - _x0; + float dy = _y1 - _y0; + float d = sqrtf(dx * dx + dy * dy); + if (d > 0.0001f) + { + d = 1.0f / d; + dx *= d; + dy *= d; + } + + float nx = dy; + float ny = -dx; + float verts[4 * 2]; + _r -= _fth; + _r *= 0.5f; + if (_r < 0.01f) + { + _r = 0.01f; + } + + dx *= _r; + dy *= _r; + nx *= _r; + ny *= _r; + + verts[0] = _x0 - dx - nx; + verts[1] = _y0 - dy - ny; + + verts[2] = _x0 - dx + nx; + verts[3] = _y0 - dy + ny; + + verts[4] = _x1 + dx + nx; + verts[5] = _y1 + dy + ny; + + verts[6] = _x1 + dx - nx; + verts[7] = _y1 + dy - ny; + + drawPolygon(verts, 4, _fth, _abgr); +} + +void initVertexDecls() +{ + PosColorTexCoord0Vertex::init(); + PosColorVertex::init(); +} + +static inline +uint32_t u32Color(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a = 255) +{ + return 0 + | (uint32_t(_r) << 0) + | (uint32_t(_g) << 8) + | (uint32_t(_b) << 16) + | (uint32_t(_a) << 24) + ; +} + int renderer_bgfx::draw(int update) { - //if (has_flags(FI_CHANGED) || (window().width() != m_last_width) || (window().height() != m_last_height)) - // do something - //clear_flags(FI_CHANGED); + initVertexDecls(); + // Set view 0 default viewport. + int width, height; +#ifdef OSD_WINDOWS + RECT client; + GetClientRect(window().m_hwnd, &client); + width = rect_width(&client); + height = rect_height(&client); +#else + width = m_blit_dim.width(); + height = m_blit_dim.height(); +#endif + bgfx::setViewRect(0, 0, 0, width, height); + bgfx::reset(width, height, BGFX_RESET_VSYNC); + // Setup view transform. + { + float view[16]; + bx::mtxIdentity(view); + + float left = 0.0f; + float top = 0.0f; + float right = width; + float bottom = height; + float proj[16]; + bx::mtxOrtho(proj, left, right, bottom, top, 0.0f, 100.0f); + bgfx::setViewTransform(0, view, proj); + } bgfx::setViewClear(0 - , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH + , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH , 0x000000ff , 1.0f , 0 ); - // Set view 0 default viewport. -#ifdef OSD_WINDOWS - RECT client; - GetClientRect(window().m_hwnd, &client); - bgfx::setViewRect(0, 0, 0, rect_width(&client), rect_height(&client)); -#else - bgfx::setViewRect(0, 0, 0, m_blit_dim.width(), m_blit_dim.height()); -#endif + // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. bgfx::touch(0); window().m_primlist->acquire_lock(); + // Draw quad. // now draw for (render_primitive *prim = window().m_primlist->first(); prim != NULL; prim = prim->next()) { + uint64_t flags = BGFX_STATE_RGB_WRITE; + switch (prim->flags & PRIMFLAG_BLENDMODE_MASK) + { + case PRIMFLAG_BLENDMODE(BLENDMODE_NONE): + break; + case PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA): + flags |= BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA); + break; + case PRIMFLAG_BLENDMODE(BLENDMODE_RGB_MULTIPLY): + flags |= BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_DST_COLOR, BGFX_STATE_BLEND_ZERO); + break; + case PRIMFLAG_BLENDMODE(BLENDMODE_ADD): + flags |= BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_ONE); + } + switch (prim->type) { /** @@ -255,72 +655,98 @@ int renderer_bgfx::draw(int update) * since entering and leaving one is most expensive.. */ case render_primitive::LINE: - // check if it's really a point -/* - if (((prim->bounds.x1 - prim->bounds.x0) == 0) && ((prim->bounds.y1 - prim->bounds.y0) == 0)) - { - curPrimitive=GL_POINTS; - } else { - curPrimitive=GL_LINES; - } - - if(pendingPrimitive!=GL_NO_PRIMITIVE && pendingPrimitive!=curPrimitive) - { - glEnd(); - pendingPrimitive=GL_NO_PRIMITIVE; - } - - if ( pendingPrimitive==GL_NO_PRIMITIVE ) - { - set_blendmode(sdl, PRIMFLAG_GET_BLENDMODE(prim->flags)); - } - - glColor4f(prim->color.r, prim->color.g, prim->color.b, prim->color.a); - - if(pendingPrimitive!=curPrimitive) - { - glBegin(curPrimitive); - pendingPrimitive=curPrimitive; - } - - // check if it's really a point - if (curPrimitive==GL_POINTS) - { - glVertex2f(prim->bounds.x0+hofs, prim->bounds.y0+vofs); - } - else - { - glVertex2f(prim->bounds.x0+hofs, prim->bounds.y0+vofs); - glVertex2f(prim->bounds.x1+hofs, prim->bounds.y1+vofs); - }*/ + + drawLine(prim->bounds.x0, prim->bounds.y0, prim->bounds.x1, prim->bounds.y1, + 1.0f, + u32Color(prim->color.r * 255, prim->color.g * 255, prim->color.b * 255, prim->color.a * 255), + 1.0f); + bgfx::setState(flags); + bgfx::submit(0, m_progLine); break; case render_primitive::QUAD: -/* - if(pendingPrimitive!=GL_NO_PRIMITIVE) - { - glEnd(); - pendingPrimitive=GL_NO_PRIMITIVE; - } + if (prim->texture.base == nullptr) { + render_quad_texuv uv; + uv.tl.u = uv.tl.v = uv.tr.u = uv.tr.v = 0; + uv.bl.u = uv.bl.v = uv.br.u = uv.br.v = 0; + screenQuad(prim->bounds.x0, prim->bounds.y0,prim->bounds.x1, prim->bounds.y1, + u32Color(prim->color.r * 255, prim->color.g * 255, prim->color.b * 255, prim->color.a * 255),uv); + bgfx::setState(flags); + bgfx::submit(0, m_progQuad); + } else { + screenQuad(prim->bounds.x0, prim->bounds.y0, prim->bounds.x1, prim->bounds.y1, + 0xFFFFFFFF,prim->texcoords); + bgfx::TextureHandle m_texture; + // render based on the texture coordinates + switch (prim->flags & PRIMFLAG_TEXFORMAT_MASK) + { + case PRIMFLAG_TEXFORMAT(TEXFORMAT_PALETTEA16): + case PRIMFLAG_TEXFORMAT(TEXFORMAT_PALETTE16): + { + auto mem = bgfx::alloc(prim->texture.width*prim->texture.height * 4); + + int y, x; - glColor4f(prim->color.r, prim->color.g, prim->color.b, prim->color.a); + for (y = 0; y < prim->texture.height; y++) + { + unsigned char *pARGB32 = (unsigned char *)prim->texture.base + y*prim->texture.rowpixels*2; + unsigned char *pRGBA8 = (unsigned char *)mem->data + y*prim->texture.width * 4; + for (x = 0; x < prim->texture.width*2; x+=2) + { + pRGBA8[x*2 + 0] = prim->texture.palette[pARGB32[x + 0] + pARGB32[x + 1] * 256].r(); + pRGBA8[x*2 + 1] = prim->texture.palette[pARGB32[x + 0] + pARGB32[x + 1] * 256].g(); + pRGBA8[x*2 + 2] = prim->texture.palette[pARGB32[x + 0] + pARGB32[x + 1] * 256].b(); + pRGBA8[x*2 + 3] = prim->texture.palette[pARGB32[x + 0] + pARGB32[x + 1] * 256].a(); + } + } + m_texture = bgfx::createTexture2D((uint16_t)prim->texture.width + , (uint16_t)prim->texture.height + , 1 + , bgfx::TextureFormat::RGBA8 + , 0 + , mem + ); + } + break; + case PRIMFLAG_TEXFORMAT(TEXFORMAT_YUY16): + break; + case PRIMFLAG_TEXFORMAT(TEXFORMAT_RGB32): + case PRIMFLAG_TEXFORMAT(TEXFORMAT_ARGB32): + { + + auto mem = bgfx::alloc(prim->texture.width*prim->texture.height * 4); + int y, x; - set_blendmode(sdl, PRIMFLAG_GET_BLENDMODE(prim->flags)); + for (y = 0; y < prim->texture.height; y++) + { + unsigned char *pARGB32 = (unsigned char *)prim->texture.base + y*prim->texture.rowpixels*4; + unsigned char *pRGBA8 = (unsigned char *)mem->data + y*prim->texture.width *4; + for (x = 0; x < prim->texture.width *4; x+=4 ) + { + pRGBA8[x] = pARGB32[x+2]; + pRGBA8[x+1] = pARGB32[x+1]; + pRGBA8[x+2] = pARGB32[x+0]; + pRGBA8[x + 3] = pARGB32[x + 3]; + } + } + m_texture = bgfx::createTexture2D((uint16_t)prim->texture.width + , (uint16_t)prim->texture.height + , 1 + , bgfx::TextureFormat::RGBA8 + , 0 + , mem + ); + } + break; - texture = texture_update(window, prim, 0); - - - sdl->texVerticex[0]=prim->bounds.x0 + hofs; - sdl->texVerticex[1]=prim->bounds.y0 + vofs; - sdl->texVerticex[2]=prim->bounds.x1 + hofs; - sdl->texVerticex[3]=prim->bounds.y0 + vofs; - sdl->texVerticex[4]=prim->bounds.x1 + hofs; - sdl->texVerticex[5]=prim->bounds.y1 + vofs; - sdl->texVerticex[6]=prim->bounds.x0 + hofs; - sdl->texVerticex[7]=prim->bounds.y1 + vofs; - - glDrawArrays(GL_QUADS, 0, 4); -*/ + default: + break; + } + bgfx::setTexture(0, m_s_texColor, m_texture); + bgfx::setState(flags); + bgfx::submit(0, m_progQuadTexture); + bgfx::destroyTexture(m_texture); + } break; default: