diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..8753ad99c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,17 @@ +[submodule "extern/rendering"] + path = extern/rendering + url = https://github.com/greentwip/bgfx.cmake + +[submodule "extern/math/glm"] + path = extern/math/glm + url = https://github.com/g-truc/glm + +[submodule "extern/rendering/bgfx"] + path = extern/rendering/bgfx + url = https://github.com/greentwip/bgfx +[submodule "extern/rendering/bimg"] + path = extern/rendering/bimg + url = https://github.com/bkaradzic/bimg +[submodule "extern/rendering/bx"] + path = extern/rendering/bx + url = https://github.com/bkaradzic/bx diff --git a/CMakeLists.txt b/CMakeLists.txt index 3cd048b1e..0403ea319 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,10 +25,64 @@ include(AutoSetup) option(STORM_ENABLE_STEAM "Enable Steam integration" OFF) +set(BGFX_BUILD_EXAMPLES OFF) +set(BGFX_INSTALL_EXAMPLES OFF) + +set(BGFX_BUILD_TOOLS OFF) + set(STORM_RESOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/res") set(STORM_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src") set(STORM_EXTERN_DIR "${CMAKE_CURRENT_SOURCE_DIR}/extern") +set(STORM_EXTERN_RENDERING_DIR "${CMAKE_CURRENT_SOURCE_DIR}/extern/rendering") +set(STORM_EXTERN_MATH_DIR "${CMAKE_CURRENT_SOURCE_DIR}/extern/math") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +include_directories(${STORM_EXTERN_RENDERING_DIR}/bgfx/examples/common + ${STORM_EXTERN_RENDERING_DIR}/bgfx/3rdparty + ${STORM_EXTERN_RENDERING_DIR}/bgfx/3rdparty/dxsdk/include + ${STORM_EXTERN_RENDERING_DIR}/bgfx/3rdparty/khronos + $ + $ + $ + $ + $ + $ + ${STORM_EXTERN_MATH_DIR}/glm/glm + ${GLSL-OPTIMIZER_INCLUDES} + ${GLSLLANG_INCLUDES} + ${SPIRV_CROSS_INCLUDES} + ${SPIRV_TOOLS_INCLUDES} + ${WEBGPU_INCLUDES}) + +# Build system specific configurations +if( MSVC ) + include_directories($ + $) +elseif( MINGW ) + include_directories($ + $ ) +elseif( APPLE ) + include_directories($ + $ ) +endif() + +include_directories() + + + +add_definitions("-D_CRT_SECURE_NO_WARNINGS" "-D__STDC_FORMAT_MACROS") + + add_subdirectory(src) + +target_link_libraries(engine PRIVATE bgfx dear-imgui meshoptimizer brtshaderc fcpp bx bimg bgfx-vertexlayout bgfx-shader-spirv fcpp glsl-optimizer glslang spirv-cross spirv-tools webgpu example-common) + + +add_subdirectory(${STORM_EXTERN_RENDERING_DIR}) + +add_subdirectory(${STORM_EXTERN_MATH_DIR}/glm) + + + + diff --git a/CMakeSettings.json b/CMakeSettings.json index ae1b2ca02..cadcbd7a8 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -8,8 +8,9 @@ "buildRoot": "${projectDir}\\build\\${name}", "cmakeCommandArgs": "-DCMAKE_C_COMPILER=cl -DCMAKE_CXX_COMPILER=cl", "buildCommandArgs": "", - "enableClangTidyCodeAnalysis": true, - "variables": [] + "addressSanitizerEnabled": true, + "addressSanitizerRuntimeFlags": "windows_hook_legacy_allocators=true:halt_on_error=0", + "enableClangTidyCodeAnalysis": true }, { "name": "msvc-release", @@ -19,8 +20,7 @@ "buildRoot": "${projectDir}\\build\\${name}", "cmakeCommandArgs": "-DCMAKE_C_COMPILER=cl -DCMAKE_CXX_COMPILER=cl", "buildCommandArgs": "", - "enableClangTidyCodeAnalysis": true, - "variables": [] + "enableClangTidyCodeAnalysis": true } ] -} +} \ No newline at end of file diff --git a/extern/math/glm b/extern/math/glm new file mode 160000 index 000000000..761a842a5 --- /dev/null +++ b/extern/math/glm @@ -0,0 +1 @@ +Subproject commit 761a842a595f58acfc01cc5c7760ff351feaaece diff --git a/extern/rendering/.gitignore b/extern/rendering/.gitignore new file mode 100644 index 000000000..91890a846 --- /dev/null +++ b/extern/rendering/.gitignore @@ -0,0 +1,15 @@ +[Bb]uild/ +*.swp +CMakeCache.txt +CMakeFiles/ +Debug/ +Release/ +*.vcxproj +*.vcxproj.filters +*.dir/ +*.sln +install/ +install_manifest.txt +generated/* +!generated/*.in +cmake_install.cmake diff --git a/extern/rendering/.travis.yml b/extern/rendering/.travis.yml new file mode 100644 index 000000000..22306ad5b --- /dev/null +++ b/extern/rendering/.travis.yml @@ -0,0 +1,164 @@ +language: cpp + +addons: + apt_packages: + - libgl1-mesa-dev + homebrew: + packages: + - cmake + update: true + +matrix: + include: + + - name: "Linux gcc" + os: linux + dist: xenial + compiler: gcc + env: + - AMALGAMATED=OFF + + - name: "Linux gcc Amalgamated" + os: linux + dist: xenial + compiler: gcc + env: + - AMALGAMATED=ON + + - name: "Linux clang" + os: linux + dist: xenial + compiler: clang + env: + - AMALGAMATED=OFF + + - name: "Linux clang Amalgamated" + os: linux + dist: xenial + compiler: clang + env: + - AMALGAMATED=ON + + - name: "macOS Xcode 10" + os: osx + osx_image: xcode10 + env: + - CMAKE_FLAGS="-GXcode" + - AMALGAMATED=OFF + + - name: "macOS Xcode 10 Amalgamated" + os: osx + osx_image: xcode10 + env: + - CMAKE_FLAGS="-GXcode" + - AMALGAMATED=ON + + - name: "macOS Xcode 10 (Makefiles)" + os: osx + osx_image: xcode10 + env: + - AMALGAMATED=OFF + + - name: "macOS Xcode 10 Amalgamated (Makefiles)" + os: osx + osx_image: xcode10 + env: + - AMALGAMATED=ON + + #- name: "iOS Xcode 10" + # os: osx + # osx_image: xcode10 + # env: + # - CMAKE_FLAGS="-GXcode -DCMAKE_SYSTEM_NAME=iOS -DBGFX_BUILD_TOOLS=OFF -DCMAKE_OSX_SYSROOT=iphonesimulator" + # - AMALGAMATED=OFF + + #- name: "iOS Xcode 10 Amalgamated" + # os: osx + # osx_image: xcode10 + # env: + # - CMAKE_FLAGS="-GXcode -DCMAKE_SYSTEM_NAME=iOS -DBGFX_BUILD_TOOLS=OFF -DCMAKE_OSX_SYSROOT=iphonesimulator" + # - AMALGAMATED=ON + + - name: "Visual studio 15 2017" + os: windows + env: + - AMALGAMATED=OFF + + + - name: "Visual studio 15 2017 Amalgamated" + os: windows + env: + - AMALGAMATED=ON + + - name: "Emscripten" + os: linux + dist: xenial + language: node_js + services: + - docker + before_script: + - docker run -dit --name emscripten -v $(pwd):/src trzeci/emscripten:sdk-incoming-64bit bash + script: + - docker exec -it emscripten emconfigure cmake . -DBGFX_BUILD_TOOLS=OFF -DBGFX_AMALGAMATED=$AMALGAMATED -DBX_AMALGAMATED=$AMALGAMATED + - docker exec -it emscripten emmake make + env: + - AMALGAMATED=OFF + + - name: "Emscripten Amalgamated" + os: linux + dist: xenial + language: node_js + services: + - docker + before_script: + - docker run -dit --name emscripten -v $(pwd):/src trzeci/emscripten:sdk-incoming-64bit bash + script: + - docker exec -it emscripten emconfigure cmake . -DBGFX_BUILD_TOOLS=OFF -DBGFX_AMALGAMATED=$AMALGAMATED -DBX_AMALGAMATED=$AMALGAMATED + - docker exec -it emscripten emmake make + env: + - AMALGAMATED=OFF + + - name: "Android armeabi-v7a" + language: android + android: &androidComponents + components: + - tools + - platform-tools + - build-tools-26.0.1 + env: + - CMAKE_FLAGS="-DCMAKE_SYSTEM_NAME=Android -DBGFX_BUILD_TOOLS=OFF -DBGFX_BUILD_EXAMPLES=OFF -DCMAKE_ANDROID_NDK=$TRAVIS_BUILD_DIR/android-ndk-r18b -DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a -DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=clang -DCMAKE_ANDROID_STL_TYPE=c++_shared -DCMAKE_ANDROID_API=26" + - AMALGAMATED=OFF + install: &androidInstall + - echo y | sdkmanager "cmake;3.10.2.4988404" + - sudo ln -sf /usr/local/android-sdk/cmake/3.10.2.4988404/bin/cmake /usr/bin/cmake + - wget https://dl.google.com/android/repository/android-ndk-r18b-linux-x86_64.zip + - unzip -qq android-ndk-r18b-linux-x86_64.zip + + - name: "Android armeabi-v7a Amalgamated" + language: android + android: *androidComponents + env: + - CMAKE_FLAGS="-DCMAKE_SYSTEM_NAME=Android -DBGFX_BUILD_TOOLS=OFF -DBGFX_BUILD_EXAMPLES=OFF -DCMAKE_ANDROID_NDK=$TRAVIS_BUILD_DIR/android-ndk-r18b -DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a -DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=clang -DCMAKE_ANDROID_STL_TYPE=c++_shared -DCMAKE_ANDROID_API=26" + - AMALGAMATED=ON + install: *androidInstall + + - name: "Android x86" + language: android + android: *androidComponents + env: + - CMAKE_FLAGS="-DCMAKE_SYSTEM_NAME=Android -DBGFX_BUILD_TOOLS=OFF -DBGFX_BUILD_EXAMPLES=OFF -DCMAKE_ANDROID_NDK=$TRAVIS_BUILD_DIR/android-ndk-r18b -DCMAKE_ANDROID_ARCH_ABI=x86 -DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=clang -DCMAKE_ANDROID_STL_TYPE=c++_shared -DCMAKE_ANDROID_API=26" + - AMALGAMATED=OFF + install: *androidInstall + + - name: "Android x86 Amalgamated" + language: android + android: *androidComponents + env: + - CMAKE_FLAGS="-DCMAKE_SYSTEM_NAME=Android -DBGFX_BUILD_TOOLS=OFF -DBGFX_BUILD_EXAMPLES=OFF -DCMAKE_ANDROID_NDK=$TRAVIS_BUILD_DIR/android-ndk-r18b -DCMAKE_ANDROID_ARCH_ABI=x86 -DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=clang -DCMAKE_ANDROID_STL_TYPE=c++_shared -DCMAKE_ANDROID_API=26" + - AMALGAMATED=ON + install: *androidInstall + +script: + - mkdir build && cd build + - cmake $CMAKE_FLAGS -DBGFX_INSTALL_EXAMPLES=ON -DBGFX_AMALGAMATED=$AMALGAMATED -DBX_AMALGAMATED=$AMALGAMATED .. + - cmake --build . diff --git a/extern/rendering/CMakeLists.txt b/extern/rendering/CMakeLists.txt new file mode 100644 index 000000000..0c80ff6d8 --- /dev/null +++ b/extern/rendering/CMakeLists.txt @@ -0,0 +1,184 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +cmake_minimum_required( VERSION 3.0 ) +project(bgfx) + +# sets project version from api ver / git rev +include( ${STORM_EXTERN_RENDERING_DIR}/cmake/version.cmake ) + +set_property( GLOBAL PROPERTY USE_FOLDERS ON ) +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +# Makefile generators on apple need this flag to compile mixed objective/c++ +if( APPLE AND NOT XCODE ) + set( CMAKE_CXX_FLAGS "-ObjC++" ) +endif() + +option( BGFX_BUILD_TOOLS "Build bgfx tools." OFF ) +option( BGFX_BUILD_ADDONS "Build bgfx addons." ON ) +option( BGFX_BUILD_EXAMPLES "Build bgfx examples." OFF ) +option( BGFX_INSTALL "Create installation target." ON ) +option( BGFX_INSTALL_EXAMPLES "Install examples and their runtimes." OFF ) +option( BGFX_CUSTOM_TARGETS "Include convenience custom targets." ON ) +option( BGFX_USE_OVR "Build with OVR support." OFF ) +option( BGFX_AMALGAMATED "Amalgamated bgfx build for faster compilation" OFF ) +option( BX_AMALGAMATED "Amalgamated bx build for faster compilation" OFF ) +option( BGFX_CONFIG_DEBUG "Enables debug configuration on all builds" OFF ) +option( BGFX_CONFIG_RENDERER_WEBGPU "Enables the webgpu renderer" OFF ) + +set( BGFX_OPENGL_VERSION "" CACHE STRING "Specify minimum opengl version" ) +set( BGFX_OPENGLES_VERSION "" CACHE STRING "Specify minimum OpenGL ES version" ) +set( BGFX_LIBRARY_TYPE "STATIC" CACHE STRING "Linking type for library" ) + +set_property( CACHE BGFX_LIBRARY_TYPE PROPERTY STRINGS STATIC SHARED ) + +set( BX_DIR "${STORM_EXTERN_RENDERING_DIR}/bx" CACHE STRING "Location of bx." FORCE ) +set( BIMG_DIR "${STORM_EXTERN_RENDERING_DIR}/bimg" CACHE STRING "Location of bimg." FORCE ) +set( BGFX_DIR "${STORM_EXTERN_RENDERING_DIR}/bgfx" CACHE STRING "Location of bgfx." FORCE ) + +if( BGFX_USE_OVR ) + include( ${STORM_EXTERN_RENDERING_DIR}/cmake/ovr.cmake ) +endif() + +include( ${STORM_EXTERN_RENDERING_DIR}/cmake/shared.cmake ) +include( ${STORM_EXTERN_RENDERING_DIR}/cmake/bx.cmake ) +include( ${STORM_EXTERN_RENDERING_DIR}/cmake/bimg.cmake ) +include( ${STORM_EXTERN_RENDERING_DIR}/cmake/bgfx.cmake ) + +if( BGFX_BUILD_TOOLS ) + include( ${STORM_EXTERN_RENDERING_DIR}/cmake/tools.cmake ) +endif() + +if( BGFX_BUILD_ADDONS ) + include( ${STORM_EXTERN_RENDERING_DIR}/cmake/addons.cmake ) +endif() + + +include( ${STORM_EXTERN_RENDERING_DIR}/cmake/examples.cmake ) + +if( BGFX_INSTALL ) + include(GNUInstallDirs) + + # Layout. This works for all platforms: + # * /lib*/cmake/ + # * /lib*/ + # * /include/ + set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") + set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated") + + # Configuration + set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake") + set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake") + set(TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets") + set(namespace "${PROJECT_NAME}::") + + # Include module with fuction 'write_basic_package_version_file' + include(CMakePackageConfigHelpers) + + # Configure 'ConfigVersion.cmake' + # Use: + # * PROJECT_VERSION + if(${CMAKE_VERSION} VERSION_LESS "3.11.0") + set(BGFX_COMPATIBILITY SameMajorVersion) + else() + set(BGFX_COMPATIBILITY SameMinorVersion) + endif() + write_basic_package_version_file( + "${version_config}" + VERSION ${PROJECT_VERSION} + COMPATIBILITY ${BGFX_COMPATIBILITY} + ) + + # Configure 'Config.cmake' + # Use variables: + # * TARGETS_EXPORT_NAME + # * PROJECT_NAME + configure_package_config_file( + "${STORM_EXTERN_RENDERING_DIR}/cmake/Config.cmake.in" + "${project_config}" + INSTALL_DESTINATION "${config_install_dir}" + ) + + install(FILES ${BGFX_DIR}/LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR}) + install(FILES README.md DESTINATION ${CMAKE_INSTALL_DOCDIR}) + + install( TARGETS bgfx bimg bx astc-codec astc edtaa3 etc1 etc2 iqa squish nvtt pvrtc + EXPORT "${TARGETS_EXPORT_NAME}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + ) + if (BGFX_CONFIG_RENDERER_WEBGPU) + install( TARGETS webgpu + EXPORT "${TARGETS_EXPORT_NAME}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + ) + endif() + + # install headers (this should be done as a target probably... ^) + install( DIRECTORY ${BX_DIR}/include/bx ${BX_DIR}/include/compat ${BX_DIR}/include/tinystl DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") + install( DIRECTORY ${BIMG_DIR}/include/bimg DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") + install( DIRECTORY ${BGFX_DIR}/include/bgfx DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") + + # header required for shader compilation + install( FILES ${BGFX_DIR}/src/bgfx_shader.sh ${BGFX_DIR}/src/bgfx_compute.sh + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/bgfx" + ) + + # install tools + if( BGFX_BUILD_TOOLS ) + install( TARGETS shaderc + EXPORT "${TARGETS_EXPORT_NAME}" + DESTINATION "${CMAKE_INSTALL_BINDIR}" ) + install( TARGETS geometryc + EXPORT "${TARGETS_EXPORT_NAME}" + DESTINATION "${CMAKE_INSTALL_BINDIR}" ) + install( TARGETS geometryv + EXPORT "${TARGETS_EXPORT_NAME}" + DESTINATION "${CMAKE_INSTALL_BINDIR}" ) + install( TARGETS texturec + EXPORT "${TARGETS_EXPORT_NAME}" + DESTINATION "${CMAKE_INSTALL_BINDIR}" ) + install( TARGETS texturev + EXPORT "${TARGETS_EXPORT_NAME}" + DESTINATION "${CMAKE_INSTALL_BINDIR}" ) + endif() + + # install examples + if( BGFX_BUILD_EXAMPLES AND BGFX_INSTALL_EXAMPLES ) + install( DIRECTORY ${BGFX_DIR}/examples/runtime/ DESTINATION examples ) + foreach( EXAMPLE ${BGFX_EXAMPLES} ) + install( TARGETS example-${EXAMPLE} DESTINATION examples ) + endforeach() + endif() + + # Config + # * /lib/cmake/bgfx/bgfxConfig.cmake + # * /lib/cmake/bgfx/bgfxConfigVersion.cmake + install( + FILES "${project_config}" "${version_config}" + DESTINATION "${config_install_dir}" + ) + + # Config + # * /lib/cmake/bgfx/bgfxTargets.cmake + install( + EXPORT "${TARGETS_EXPORT_NAME}" + NAMESPACE "${namespace}" + DESTINATION "${config_install_dir}" + ) +endif() diff --git a/extern/rendering/COPYING b/extern/rendering/COPYING new file mode 100644 index 000000000..0e259d42c --- /dev/null +++ b/extern/rendering/COPYING @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/extern/rendering/README.md b/extern/rendering/README.md new file mode 100644 index 000000000..9cc91412f --- /dev/null +++ b/extern/rendering/README.md @@ -0,0 +1,44 @@ +bgfx.cmake +=================== +[![Build Status](https://travis-ci.org/widberg/bgfx.cmake.svg?branch=master)](https://travis-ci.org/widberg/bgfx.cmake) + +This repo contains a bunch of cmake files that can be used to build bgfx with CMake. + +Building +------------- + +``` +git clone https://github.com/widberg/bgfx.cmake.git +cd bgfx.cmake +git submodule init +git submodule update +mkdir build +cd build +cmake .. +``` + +If downloading via zip (instead of using git submodules) manually download bx, bimg and bgfx and copy them into the root directory, or locate them via BX_DIR, BIMG_DIR and BGFX_DIR CMake variables. + +How To Use +------------- +This project is setup to be included a few different ways. To include bgfx source code in your project simply use add_subdirectory to include this project. To build bgfx binaries build the INSTALL target (or "make install"). The installed files will be in the directory specified by CMAKE_INSTALL_PREFIX which I recommend you set to "./install" so it will export to your build directory. Note you may want to build install on both Release and Debug configurations. + +Features +------------- +* No outside dependencies besides bx, bimg, bgfx, and CMake. +* Tested on Visual Studio 2015, Xcode, gcc 5.4, clang 3.8. +* Compiles bgfx, tools & examples. +* Detects shader modifications and automatically rebuilds them for all examples. + +Does this work with latest bx/bgfx/bimg? +------------- +Probably! This project needs to be updated if a dependency is added or the bgfx build system changes. The bgfx repository is very active but these types of changes are rare. New examples have to be added manually as well, but not doing so will merely result in that example not showing up and won't break anything else. If pulling latest causes issues, be sure to manually reconfigure CMake as the glob patterns may need to be refreshed (the use of glob patterns in CMake is generally discouraged but in this project it helps to ensure upwards compatibilty with newer bgfx updates). + +Todo +------------- +* Support Native Client. +* Support Windows Phone. +* Fix iOS builds. [#85](https://github.com/widberg/bgfx.cmake/issues/85) +* More configuration. [#12](https://github.com/widberg/bgfx.cmake/issues/12) +* Add varying.def.sc files as shader dependencies. +* Combined examples. diff --git a/extern/rendering/bgfx b/extern/rendering/bgfx new file mode 160000 index 000000000..beda3ba82 --- /dev/null +++ b/extern/rendering/bgfx @@ -0,0 +1 @@ +Subproject commit beda3ba82b55c7ae7ac95ce420090ff51484ed7c diff --git a/extern/rendering/bimg b/extern/rendering/bimg new file mode 160000 index 000000000..8355d36be --- /dev/null +++ b/extern/rendering/bimg @@ -0,0 +1 @@ +Subproject commit 8355d36befc90c1db82fca8e54f38bfb7eeb3530 diff --git a/extern/rendering/bx b/extern/rendering/bx new file mode 160000 index 000000000..369c826bb --- /dev/null +++ b/extern/rendering/bx @@ -0,0 +1 @@ +Subproject commit 369c826bb3ef5227c9323260a9e2494b9841602f diff --git a/extern/rendering/cmake/3rdparty/astc-codec.cmake b/extern/rendering/cmake/3rdparty/astc-codec.cmake new file mode 100644 index 000000000..ef7db5547 --- /dev/null +++ b/extern/rendering/cmake/3rdparty/astc-codec.cmake @@ -0,0 +1,28 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET astc-codec ) + return() +endif() + +file( + GLOB + ASTC_CODEC_SOURCES + ${BIMG_DIR}/3rdparty/astc-codec/src/decoder/*.cc + ${BIMG_DIR}/3rdparty/astc-codec/src/decoder/*.h +) + +add_library( astc-codec STATIC ${ASTC_CODEC_SOURCES} ) +target_include_directories( astc-codec + PUBLIC + $ + $ + $ ) +set_target_properties( astc-codec PROPERTIES FOLDER "bgfx/3rdparty" ) \ No newline at end of file diff --git a/extern/rendering/cmake/3rdparty/astc.cmake b/extern/rendering/cmake/3rdparty/astc.cmake new file mode 100644 index 000000000..371fea633 --- /dev/null +++ b/extern/rendering/cmake/3rdparty/astc.cmake @@ -0,0 +1,19 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET astc ) + return() +endif() + +file( GLOB ASTC_SOURCES ${BIMG_DIR}/3rdparty/astc/*.cpp ${BIMG_DIR}/3rdparty/astc/*.h ) + +add_library( astc STATIC ${ASTC_SOURCES} ) +target_include_directories( astc PUBLIC $ ) +set_target_properties( astc PROPERTIES FOLDER "bgfx/3rdparty" ) \ No newline at end of file diff --git a/extern/rendering/cmake/3rdparty/dear-imgui.cmake b/extern/rendering/cmake/3rdparty/dear-imgui.cmake new file mode 100644 index 000000000..9b5604310 --- /dev/null +++ b/extern/rendering/cmake/3rdparty/dear-imgui.cmake @@ -0,0 +1,21 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET dear-imgui ) + return() +endif() + +file( GLOB dear_IMGUI_SOURCES ${BGFX_DIR}/3rdparty/dear-imgui/*.cpp ${BGFX_DIR}/3rdparty/dear-imgui/*.h ${BGFX_DIR}/3rdparty/dear-imgui/*.inl ) + +add_library( dear-imgui STATIC EXCLUDE_FROM_ALL ${dear_IMGUI_SOURCES} ) +target_compile_definitions( dear-imgui PRIVATE "-D_CRT_SECURE_NO_WARNINGS" "-D__STDC_FORMAT_MACROS" ) +target_include_directories( dear-imgui PUBLIC ${BGFX_DIR}/3rdparty ) +target_link_libraries( dear-imgui PUBLIC bx ) +set_target_properties( dear-imgui PROPERTIES FOLDER "bgfx/3rdparty" ) diff --git a/extern/rendering/cmake/3rdparty/edtaa3.cmake b/extern/rendering/cmake/3rdparty/edtaa3.cmake new file mode 100644 index 000000000..253ea2787 --- /dev/null +++ b/extern/rendering/cmake/3rdparty/edtaa3.cmake @@ -0,0 +1,19 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET edtaa3 ) + return() +endif() + +file( GLOB EDTAA3_SOURCES ${BIMG_DIR}/3rdparty/edtaa3/*.cpp ${BIMG_DIR}/3rdparty/edtaa3/*.h ) + +add_library( edtaa3 STATIC ${EDTAA3_SOURCES} ) +target_include_directories( edtaa3 PUBLIC $ ) +set_target_properties( edtaa3 PROPERTIES FOLDER "bgfx/3rdparty" ) \ No newline at end of file diff --git a/extern/rendering/cmake/3rdparty/etc1.cmake b/extern/rendering/cmake/3rdparty/etc1.cmake new file mode 100644 index 000000000..690afc9e7 --- /dev/null +++ b/extern/rendering/cmake/3rdparty/etc1.cmake @@ -0,0 +1,19 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET etc1 ) + return() +endif() + +file( GLOB ETC1_SOURCES ${BIMG_DIR}/3rdparty/etc1/*.cpp ${BIMG_DIR}/3rdparty/etc1/*.h ) + +add_library( etc1 STATIC ${ETC1_SOURCES} ) +target_include_directories( etc1 PUBLIC $ ) +set_target_properties( etc1 PROPERTIES FOLDER "bgfx/3rdparty" ) \ No newline at end of file diff --git a/extern/rendering/cmake/3rdparty/etc2.cmake b/extern/rendering/cmake/3rdparty/etc2.cmake new file mode 100644 index 000000000..dc0aeb92a --- /dev/null +++ b/extern/rendering/cmake/3rdparty/etc2.cmake @@ -0,0 +1,20 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET etc2 ) + return() +endif() + +file( GLOB ETC2_SOURCES ${BIMG_DIR}/3rdparty/etc2/*.cpp ${BIMG_DIR}/3rdparty/etc2/*.h ) + +add_library( etc2 STATIC ${ETC2_SOURCES} ) +target_include_directories( etc2 PUBLIC $ ) +set_target_properties( etc2 PROPERTIES FOLDER "bgfx/3rdparty" ) +target_link_libraries( etc2 PUBLIC bx ) \ No newline at end of file diff --git a/extern/rendering/cmake/3rdparty/fcpp.cmake b/extern/rendering/cmake/3rdparty/fcpp.cmake new file mode 100644 index 000000000..54054b2a2 --- /dev/null +++ b/extern/rendering/cmake/3rdparty/fcpp.cmake @@ -0,0 +1,32 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET fcpp ) + return() +endif() + +file( GLOB FCPP_SOURCES ${BGFX_DIR}/3rdparty/fcpp/*.c ${BGFX_DIR}/3rdparty/fcpp/*.h ) + +add_library( fcpp STATIC ${FCPP_SOURCES} ) +target_include_directories( fcpp PUBLIC ${BGFX_DIR}/3rdparty/fcpp ) +target_compile_definitions( fcpp + PRIVATE + NINCLUDE=64 + NWORK=65536 + NBUFF=65536 + OLD_PREPROCESSOR=0 +) + +if( MSVC ) + set_target_properties( fcpp PROPERTIES COMPILE_FLAGS "/W0" ) +endif() + +set_target_properties( fcpp PROPERTIES FOLDER "bgfx/3rdparty" ) +set_source_files_properties( ${BGFX_DIR}/3rdparty/fcpp/usecpp.c PROPERTIES HEADER_FILE_ONLY ON ) diff --git a/extern/rendering/cmake/3rdparty/glsl-optimizer.cmake b/extern/rendering/cmake/3rdparty/glsl-optimizer.cmake new file mode 100644 index 000000000..870414eb9 --- /dev/null +++ b/extern/rendering/cmake/3rdparty/glsl-optimizer.cmake @@ -0,0 +1,85 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET glsl-optimizer ) + return() +endif() + +set( GLSL-OPTIMIZER_INCLUDES + ${BGFX_DIR}/3rdparty/glsl-optimizer/include + ${BGFX_DIR}/3rdparty/glsl-optimizer/src/mesa + ${BGFX_DIR}/3rdparty/glsl-optimizer/src/mapi + ${BGFX_DIR}/3rdparty/glsl-optimizer/src/glsl + ${BGFX_DIR}/3rdparty/glsl-optimizer/src +) + +# glsl-optimizer makes UBSan segfault for whatever reason +# We already know that it's full of UB anyway... so just don't build it with UBSan +string(REPLACE "-fsanitize=undefined" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" ) +string(REPLACE "-fsanitize=undefined" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" ) + +# glcpp +file( GLOB GLCPP_SOURCES ${BGFX_DIR}/3rdparty/glsl-optimizer/src/glsl/glcpp/*.c ${BGFX_DIR}/3rdparty/glsl-optimizer/src/util/*.c ) +add_library( glcpp ${GLCPP_SOURCES} ) +target_include_directories( glcpp PUBLIC ${GLSL-OPTIMIZER_INCLUDES} ) +if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang") + target_compile_options( glcpp PRIVATE "-fno-strict-aliasing") +endif() +if( MSVC ) + set_target_properties( glcpp PROPERTIES COMPILE_FLAGS "/W0" ) +endif() +set_target_properties( glcpp PROPERTIES FOLDER "bgfx/3rdparty" ) + +# mesa +file( GLOB MESA_SOURCES ${BGFX_DIR}/3rdparty/glsl-optimizer/src/mesa/program/*.c ${BGFX_DIR}/3rdparty/glsl-optimizer/src/mesa/main/*.c ) +add_library( mesa ${MESA_SOURCES} ) +target_include_directories( mesa PUBLIC ${GLSL-OPTIMIZER_INCLUDES} ) +if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang") + target_compile_options( mesa PRIVATE "-fno-strict-aliasing") +endif() +if( MSVC ) + set_target_properties( mesa PROPERTIES COMPILE_FLAGS "/W0" ) +endif() +set_target_properties( mesa PROPERTIES FOLDER "bgfx/3rdparty" ) + +# glsl-optimizer +file( GLOB GLSL-OPTIMIZER_SOURCES ${BGFX_DIR}/3rdparty/glsl-optimizer/src/glsl/*.cpp ${BGFX_DIR}/3rdparty/glsl-optimizer/src/glsl/*.c ) +file( GLOB GLSL-OPTIMIZER_SOURCES_REMOVE ${BGFX_DIR}/3rdparty/glsl-optimizer/src/glsl/main.cpp ${BGFX_DIR}/3rdparty/glsl-optimizer/src/glsl/builtin_stubs.cpp ) +list( REMOVE_ITEM GLSL-OPTIMIZER_SOURCES ${GLSL-OPTIMIZER_SOURCES_REMOVE} ) +add_library( glsl-optimizer ${GLSL-OPTIMIZER_SOURCES} ) +target_link_libraries( glsl-optimizer glcpp mesa ) +if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + target_compile_options( glsl-optimizer + PRIVATE + -fno-strict-aliasing + -Wno-implicit-fallthrough + -Wno-parentheses + -Wno-sign-compare + -Wno-unused-function + -Wno-unused-parameter + ) +endif() +if( MSVC ) + set_target_properties( glsl-optimizer PROPERTIES COMPILE_FLAGS "/W0" ) + target_compile_definitions( glsl-optimizer + PRIVATE + __STDC__ + __STDC_VERSION__=199901L + strdup=_strdup + alloca=_alloca + isascii=__isascii + ) +elseif(APPLE) + target_compile_options( glsl-optimizer + PRIVATE + -Wno-deprecated-register + ) +endif() +set_target_properties( glsl-optimizer PROPERTIES FOLDER "bgfx/3rdparty" ) diff --git a/extern/rendering/cmake/3rdparty/glslang.cmake b/extern/rendering/cmake/3rdparty/glslang.cmake new file mode 100644 index 000000000..d3332ed70 --- /dev/null +++ b/extern/rendering/cmake/3rdparty/glslang.cmake @@ -0,0 +1,100 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET glslang ) + return() +endif() + +file( GLOB GLSLANG_SOURCES + ${BGFX_DIR}/3rdparty/glslang/glslang/GenericCodeGen/*.cpp + ${BGFX_DIR}/3rdparty/glslang/glslang/MachineIndependent/*.cpp + ${BGFX_DIR}/3rdparty/glslang/glslang/MachineIndependent/preprocessor/*.cpp + ${BGFX_DIR}/3rdparty/glslang/glslang/HLSL/*.cpp + ${BGFX_DIR}/3rdparty/glslang/hlsl/*.cpp + ${BGFX_DIR}/3rdparty/glslang/SPIRV/*.cpp + ${BGFX_DIR}/3rdparty/glslang/OGLCompilersDLL/*.cpp +) + +if( WIN32 ) + list( APPEND GLSLANG_SOURCES ${BGFX_DIR}/3rdparty/glslang/glslang/OSDependent/Windows/ossource.cpp ) +else() + list( APPEND GLSLANG_SOURCES ${BGFX_DIR}/3rdparty/glslang/glslang/OSDependent/Unix/ossource.cpp ) +endif() + +add_library( glslang STATIC EXCLUDE_FROM_ALL ${GLSLANG_SOURCES} ) + +set( GLSLLANG_INCLUDES + ${BGFX_DIR}/3rdparty/spirv-tools/include + ${BGFX_DIR}/3rdparty/spirv-tools/source + ${BGFX_DIR}/3rdparty/glslang + ${BGFX_DIR}/3rdparty/glslang/glslang/Include + ${BGFX_DIR}/3rdparty/glslang/glslang/Public +) + +target_include_directories( glslang PUBLIC + ${GLSLLANG_INCLUDES} +) + +set_target_properties( glslang PROPERTIES FOLDER "bgfx/3rdparty" ) + +if( MSVC ) + target_compile_options( glslang PRIVATE + "/wd4005" + "/wd4065" + "/wd4100" + "/wd4127" + "/wd4189" + "/wd4244" + "/wd4310" + "/wd4389" + "/wd4456" + "/wd4457" + "/wd4458" + "/wd4702" + "/wd4715" + "/wd4838" + ) +else() + target_compile_options( glslang PRIVATE + "-Wno-ignored-qualifiers" + "-Wno-implicit-fallthrough" + "-Wno-missing-field-initializers" + "-Wno-reorder" + "-Wno-return-type" + "-Wno-shadow" + "-Wno-sign-compare" + "-Wno-switch" + "-Wno-undef" + "-Wno-unknown-pragmas" + "-Wno-unused-function" + "-Wno-unused-parameter" + "-Wno-unused-variable" + "-fno-strict-aliasing" + ) +endif() + +if( APPLE ) + target_compile_options( glslang PRIVATE + "-Wno-c++11-extensions" + "-Wno-unused-const-variable" + "-Wno-deprecated-register" + ) +endif() + +if( UNIX AND NOT APPLE ) + target_compile_options( glslang PRIVATE + "-Wno-unused-but-set-variable" + ) +endif() + +target_compile_definitions( glslang PRIVATE + ENABLE_OPT=1 + ENABLE_HLSL=1 +) diff --git a/extern/rendering/cmake/3rdparty/iqa.cmake b/extern/rendering/cmake/3rdparty/iqa.cmake new file mode 100644 index 000000000..27127ce47 --- /dev/null +++ b/extern/rendering/cmake/3rdparty/iqa.cmake @@ -0,0 +1,19 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET iqa ) + return() +endif() + +file( GLOB IQA_SOURCES ${BIMG_DIR}/3rdparty/iqa/source/*.c ${BIMG_DIR}/3rdparty/iqa/include/*.h ) + +add_library( iqa STATIC ${IQA_SOURCES} ) +target_include_directories( iqa PUBLIC $ ) +set_target_properties( iqa PROPERTIES FOLDER "bgfx/3rdparty" ) \ No newline at end of file diff --git a/extern/rendering/cmake/3rdparty/libsquish.cmake b/extern/rendering/cmake/3rdparty/libsquish.cmake new file mode 100644 index 000000000..16854da0e --- /dev/null +++ b/extern/rendering/cmake/3rdparty/libsquish.cmake @@ -0,0 +1,19 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET squish ) + return() +endif() + +file( GLOB SQUISH_SOURCES ${BIMG_DIR}/3rdparty/libsquish/*.cpp ${BIMG_DIR}/3rdparty/libsquish/*.h ${BIMG_DIR}/3rdparty/libsquish/*.inl ) + +add_library( squish STATIC ${SQUISH_SOURCES} ) +target_include_directories( squish PUBLIC $ ) +set_target_properties( squish PROPERTIES FOLDER "bgfx/3rdparty" ) \ No newline at end of file diff --git a/extern/rendering/cmake/3rdparty/meshoptimizer.cmake b/extern/rendering/cmake/3rdparty/meshoptimizer.cmake new file mode 100644 index 000000000..4a3c27dcf --- /dev/null +++ b/extern/rendering/cmake/3rdparty/meshoptimizer.cmake @@ -0,0 +1,19 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET meshoptimizer ) + return() +endif() + +file( GLOB MESHOPTIMIZER_SOURCES ${BGFX_DIR}/3rdparty/meshoptimizer/src/*.cpp ${BGFX_DIR}/3rdparty/meshoptimizer/src/*.h ) + +add_library( meshoptimizer STATIC ${MESHOPTIMIZER_SOURCES} ) +target_include_directories( meshoptimizer PUBLIC ${BGFX_DIR}/3rdparty ) +set_target_properties( meshoptimizer PROPERTIES FOLDER "bgfx/3rdparty" ) diff --git a/extern/rendering/cmake/3rdparty/nvtt.cmake b/extern/rendering/cmake/3rdparty/nvtt.cmake new file mode 100644 index 000000000..9fd7dda3d --- /dev/null +++ b/extern/rendering/cmake/3rdparty/nvtt.cmake @@ -0,0 +1,36 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET nvtt ) + return() +endif() + +file( + GLOB + NVTT_SOURCES + ${BIMG_DIR}/3rdparty/nvtt/bc6h/*.cpp + ${BIMG_DIR}/3rdparty/nvtt/bc6h/*.h + ${BIMG_DIR}/3rdparty/nvtt/bc7/*.cpp + ${BIMG_DIR}/3rdparty/nvtt/bc7/*.h + ${BIMG_DIR}/3rdparty/nvtt/nvcore/*.h + ${BIMG_DIR}/3rdparty/nvtt/nvcore/*.inl + ${BIMG_DIR}/3rdparty/nvtt/nvmath/*.cpp + ${BIMG_DIR}/3rdparty/nvtt/nvmath/*.h + ${BIMG_DIR}/3rdparty/nvtt/*.cpp + ${BIMG_DIR}/3rdparty/nvtt/*.h +) + +add_library( nvtt STATIC ${NVTT_SOURCES} ) +target_include_directories( nvtt + PUBLIC + $ + $ ) +set_target_properties( nvtt PROPERTIES FOLDER "bgfx/3rdparty" ) +target_link_libraries( nvtt PUBLIC bx ) \ No newline at end of file diff --git a/extern/rendering/cmake/3rdparty/pvrtc.cmake b/extern/rendering/cmake/3rdparty/pvrtc.cmake new file mode 100644 index 000000000..f2fa4fb42 --- /dev/null +++ b/extern/rendering/cmake/3rdparty/pvrtc.cmake @@ -0,0 +1,19 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET pvrtc ) + return() +endif() + +file( GLOB PVRTC_SOURCES ${BIMG_DIR}/3rdparty/pvrtc/*.cpp ${BIMG_DIR}/3rdparty/pvrtc/*.h ) + +add_library( pvrtc STATIC ${PVRTC_SOURCES} ) +target_include_directories( pvrtc PUBLIC $ ) +set_target_properties( pvrtc PROPERTIES FOLDER "bgfx/3rdparty" ) \ No newline at end of file diff --git a/extern/rendering/cmake/3rdparty/spirv-cross.cmake b/extern/rendering/cmake/3rdparty/spirv-cross.cmake new file mode 100644 index 000000000..a0f30f74f --- /dev/null +++ b/extern/rendering/cmake/3rdparty/spirv-cross.cmake @@ -0,0 +1,53 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET spirv-cross ) + return() +endif() + +set( SPIRV_CROSS_INCLUDES + ${BGFX_DIR}/3rdparty/spirv-cross +) + + +file( + GLOB + SPIRV_CROSS_SOURCES + ${BGFX_DIR}/3rdparty/spirv-cross/spirv_cfg.cpp + ${BGFX_DIR}/3rdparty/spirv-cross/spirv_cpp.cpp + ${BGFX_DIR}/3rdparty/spirv-cross/spirv_cross.cpp + ${BGFX_DIR}/3rdparty/spirv-cross/spirv_cross_c.cpp + ${BGFX_DIR}/3rdparty/spirv-cross/spirv_cross_parsed_ir.cpp + ${BGFX_DIR}/3rdparty/spirv-cross/spirv_cross_util.cpp + ${BGFX_DIR}/3rdparty/spirv-cross/spirv_glsl.cpp + ${BGFX_DIR}/3rdparty/spirv-cross/spirv_hlsl.cpp + ${BGFX_DIR}/3rdparty/spirv-cross/spirv_msl.cpp + ${BGFX_DIR}/3rdparty/spirv-cross/spirv_parser.cpp + ${BGFX_DIR}/3rdparty/spirv-cross/spirv_reflect.cpp + ${BGFX_DIR}/3rdparty/spirv-cross/*.h +) + +add_library( spirv-cross STATIC ${SPIRV_CROSS_SOURCES} ) +target_compile_definitions( spirv-cross PRIVATE SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS ) +target_include_directories( spirv-cross PUBLIC + ${BGFX_DIR}/3rdparty/spirv-cross + ${BGFX_DIR}/3rdparty/spirv-cross/include +) + +if( MSVC ) + target_compile_options( spirv-cross PRIVATE + "/wd4018" + "/wd4245" + "/wd4706" + "/wd4715" + ) +endif() + +set_target_properties( spirv-cross PROPERTIES FOLDER "bgfx/3rdparty" ) diff --git a/extern/rendering/cmake/3rdparty/spirv-tools.cmake b/extern/rendering/cmake/3rdparty/spirv-tools.cmake new file mode 100644 index 000000000..207145441 --- /dev/null +++ b/extern/rendering/cmake/3rdparty/spirv-tools.cmake @@ -0,0 +1,67 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET spirv-tools ) + return() +endif() + +set( SPIRV_TOOLS_INCLUDES + ${BGFX_DIR}/3rdparty/spirv-headers/include + ${BGFX_DIR}/3rdparty/spirv-tools + ${BGFX_DIR}/3rdparty/spirv-tools/include + ${BGFX_DIR}/3rdparty/spirv-tools/include/generated + ${BGFX_DIR}/3rdparty/spirv-tools/source +) + + +file( + GLOB + SPIRV_TOOLS_SOURCES + ${BGFX_DIR}/3rdparty/spirv-tools/source/*.cpp + ${BGFX_DIR}/3rdparty/spirv-tools/source/*.h + ${BGFX_DIR}/3rdparty/spirv-tools/source/opt/*.cpp + ${BGFX_DIR}/3rdparty/spirv-tools/source/opt/*.h + ${BGFX_DIR}/3rdparty/spirv-tools/source/reduce/*.cpp + ${BGFX_DIR}/3rdparty/spirv-tools/source/reduce/*.h + ${BGFX_DIR}/3rdparty/spirv-tools/source/util/*.cpp + ${BGFX_DIR}/3rdparty/spirv-tools/source/util/*.h + ${BGFX_DIR}/3rdparty/spirv-tools/source/val/*.cpp + ${BGFX_DIR}/3rdparty/spirv-tools/source/val/*.h +) + +add_library( spirv-tools STATIC ${SPIRV_TOOLS_SOURCES} ) +target_include_directories( spirv-tools PUBLIC + ${BGFX_DIR}/3rdparty/spirv-headers/include + ${BGFX_DIR}/3rdparty/spirv-tools + ${BGFX_DIR}/3rdparty/spirv-tools/include + ${BGFX_DIR}/3rdparty/spirv-tools/include/generated + ${BGFX_DIR}/3rdparty/spirv-tools/source +) + +if( MSVC ) + target_compile_options( spirv-tools PRIVATE + "/wd4127" + "/wd4389" + "/wd4702" + "/wd4706" + ) +else() + target_compile_options( spirv-tools PRIVATE + "-Wno-switch" + ) + + if(MINGW OR ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") + target_compile_options( spirv-tools PRIVATE + "-Wno-misleading-indentation" + ) + endif() +endif() + +set_target_properties( spirv-tools PROPERTIES FOLDER "bgfx/3rdparty" ) diff --git a/extern/rendering/cmake/3rdparty/webgpu.cmake b/extern/rendering/cmake/3rdparty/webgpu.cmake new file mode 100644 index 000000000..14ec1a7e4 --- /dev/null +++ b/extern/rendering/cmake/3rdparty/webgpu.cmake @@ -0,0 +1,38 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( TARGET webgpu ) + return() +endif() + + +set( WEBGPU_INCLUDES + ${BGFX_DIR}/3rdparty/webgpu/include/webgpu/ + ${BGFX_DIR}/3rdparty/webgpu/include +) + + +file( + GLOB + WEBGPU_SOURCES + ${BGFX_DIR}/3rdparty/webgpu/include/webgpu/*.h + # ${BGFX_DIR}/3rdparty/webgpu/webgpu_cpp.cpp # requires dawn to be installed +) + +# Library without sources is interface +#add_library( webgpu STATIC ${WEBGPU_SOURCES} ) +add_library(webgpu INTERFACE) +target_include_directories( webgpu + # PUBLIC + INTERFACE + $ +) + +# set_target_properties( webgpu PROPERTIES FOLDER "bgfx/3rdparty" ) diff --git a/extern/rendering/cmake/Config.cmake.in b/extern/rendering/cmake/Config.cmake.in new file mode 100644 index 000000000..54dea4007 --- /dev/null +++ b/extern/rendering/cmake/Config.cmake.in @@ -0,0 +1,4 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/@TARGETS_EXPORT_NAME@.cmake") +check_required_components("@PROJECT_NAME@") \ No newline at end of file diff --git a/extern/rendering/cmake/addons.cmake b/extern/rendering/cmake/addons.cmake new file mode 100644 index 000000000..6b2f7fb03 --- /dev/null +++ b/extern/rendering/cmake/addons.cmake @@ -0,0 +1,16 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( BGFX_CUSTOM_TARGETS ) + add_custom_target( addons ) + set_target_properties( addons PROPERTIES FOLDER "bgfx/addons" ) +endif() + +include( cmake/tools/brtshaderc.cmake ) diff --git a/extern/rendering/cmake/bgfx.cmake b/extern/rendering/cmake/bgfx.cmake new file mode 100644 index 000000000..17996fecb --- /dev/null +++ b/extern/rendering/cmake/bgfx.cmake @@ -0,0 +1,133 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +# To prevent this warning: https://cmake.org/cmake/help/git-stage/policy/CMP0072.html +if(POLICY CMP0072) + cmake_policy(SET CMP0072 NEW) +endif() + +# Ensure the directory exists +if( NOT IS_DIRECTORY ${BGFX_DIR} ) + message( SEND_ERROR "Could not load bgfx, directory does not exist. ${BGFX_DIR}" ) + return() +endif() + +if(NOT APPLE) + set(BGFX_AMALGAMATED_SOURCE ${BGFX_DIR}/src/amalgamated.cpp) +else() + set(BGFX_AMALGAMATED_SOURCE ${BGFX_DIR}/src/amalgamated.mm) +endif() + +# Grab the bgfx source files +file( GLOB BGFX_SOURCES ${BGFX_DIR}/src/*.cpp ${BGFX_DIR}/src/*.mm ${BGFX_DIR}/src/*.h ${BGFX_DIR}/include/bgfx/*.h ${BGFX_DIR}/include/bgfx/c99/*.h ) +if(BGFX_AMALGAMATED) + set(BGFX_NOBUILD ${BGFX_SOURCES}) + list(REMOVE_ITEM BGFX_NOBUILD ${BGFX_AMALGAMATED_SOURCE}) + foreach(BGFX_SRC ${BGFX_NOBUILD}) + set_source_files_properties( ${BGFX_SRC} PROPERTIES HEADER_FILE_ONLY ON ) + endforeach() +else() + # Do not build using amalgamated sources + set_source_files_properties( ${BGFX_DIR}/src/amalgamated.cpp PROPERTIES HEADER_FILE_ONLY ON ) + set_source_files_properties( ${BGFX_DIR}/src/amalgamated.mm PROPERTIES HEADER_FILE_ONLY ON ) +endif() + +# Create the bgfx target +add_library( bgfx ${BGFX_LIBRARY_TYPE} ${BGFX_SOURCES} ) + +if(BGFX_CONFIG_RENDERER_WEBGPU) + include(cmake/3rdparty/webgpu.cmake) + target_compile_definitions( bgfx PRIVATE BGFX_CONFIG_RENDERER_WEBGPU=1) + if (EMSCRIPTEN) + target_link_options(bgfx PRIVATE "-s USE_WEBGPU=1") + else() + target_link_libraries(bgfx PRIVATE webgpu) + endif() +endif() + +# Enable BGFX_CONFIG_DEBUG in Debug configuration +target_compile_definitions( bgfx PRIVATE "$<$:BGFX_CONFIG_DEBUG=1>" ) +if(BGFX_CONFIG_DEBUG) + target_compile_definitions( bgfx PRIVATE BGFX_CONFIG_DEBUG=1) +endif() + +if( NOT ${BGFX_OPENGL_VERSION} STREQUAL "" ) + target_compile_definitions( bgfx PRIVATE BGFX_CONFIG_RENDERER_OPENGL_MIN_VERSION=${BGFX_OPENGL_VERSION} ) +endif() + +if( NOT ${BGFX_OPENGLES_VERSION} STREQUAL "" ) + target_compile_definitions( bgfx PRIVATE BGFX_CONFIG_RENDERER_OPENGLES_MIN_VERSION=${BGFX_OPENGLES_VERSION} ) +endif() + +# Special Visual Studio Flags +if( MSVC ) + target_compile_definitions( bgfx PRIVATE "_CRT_SECURE_NO_WARNINGS" ) +endif() + +# Includes +target_include_directories( bgfx + PRIVATE + ${BGFX_DIR}/3rdparty + ${BGFX_DIR}/3rdparty/dxsdk/include + ${BGFX_DIR}/3rdparty/khronos + PUBLIC + $ + $) + +# bgfx depends on bx and bimg +target_link_libraries( bgfx PUBLIC bx bimg ) + +# ovr support +if( BGFX_USE_OVR ) + target_link_libraries( bgfx PUBLIC ovr ) +endif() + +# Frameworks required on iOS, tvOS and macOS +if( ${CMAKE_SYSTEM_NAME} MATCHES iOS|tvOS ) + target_link_libraries (bgfx PUBLIC "-framework OpenGLES -framework Metal -framework UIKit -framework CoreGraphics -framework QuartzCore") +elseif( APPLE ) + find_library( COCOA_LIBRARY Cocoa ) + find_library( METAL_LIBRARY Metal ) + find_library( QUARTZCORE_LIBRARY QuartzCore ) + mark_as_advanced( COCOA_LIBRARY ) + mark_as_advanced( METAL_LIBRARY ) + mark_as_advanced( QUARTZCORE_LIBRARY ) + target_link_libraries( bgfx PUBLIC ${COCOA_LIBRARY} ${METAL_LIBRARY} ${QUARTZCORE_LIBRARY} ) +endif() + +if( UNIX AND NOT APPLE AND NOT EMSCRIPTEN AND NOT ANDROID ) + find_package(X11 REQUIRED) + find_package(OpenGL REQUIRED) + #The following commented libraries are linked by bx + #find_package(Threads REQUIRED) + #find_library(LIBRT_LIBRARIES rt) + #find_library(LIBDL_LIBRARIES dl) + target_link_libraries( bgfx PUBLIC ${X11_LIBRARIES} ${OPENGL_LIBRARIES}) +endif() + +# Exclude mm files if not on OS X +if( NOT APPLE ) + set_source_files_properties( ${BGFX_DIR}/src/glcontext_eagl.mm PROPERTIES HEADER_FILE_ONLY ON ) + set_source_files_properties( ${BGFX_DIR}/src/glcontext_nsgl.mm PROPERTIES HEADER_FILE_ONLY ON ) + set_source_files_properties( ${BGFX_DIR}/src/renderer_mtl.mm PROPERTIES HEADER_FILE_ONLY ON ) +endif() + +# Exclude glx context on non-unix +if( NOT UNIX OR APPLE ) + set_source_files_properties( ${BGFX_DIR}/src/glcontext_glx.cpp PROPERTIES HEADER_FILE_ONLY ON ) +endif() + +# Put in a "bgfx" folder in Visual Studio +set_target_properties( bgfx PROPERTIES FOLDER "bgfx" ) + +# in Xcode we need to specify this file as objective-c++ (instead of renaming to .mm) +if (XCODE) + set_source_files_properties(${BGFX_DIR}/src/renderer_vk.cpp PROPERTIES LANGUAGE OBJCXX XCODE_EXPLICIT_FILE_TYPE sourcecode.cpp.objcpp) +endif() diff --git a/extern/rendering/cmake/bimg.cmake b/extern/rendering/cmake/bimg.cmake new file mode 100644 index 000000000..56a047e27 --- /dev/null +++ b/extern/rendering/cmake/bimg.cmake @@ -0,0 +1,44 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +# Third party libs +include( cmake/3rdparty/astc-codec.cmake ) +include( cmake/3rdparty/astc.cmake ) +include( cmake/3rdparty/edtaa3.cmake ) +include( cmake/3rdparty/etc1.cmake ) +include( cmake/3rdparty/etc2.cmake ) +include( cmake/3rdparty/iqa.cmake ) +include( cmake/3rdparty/libsquish.cmake ) +include( cmake/3rdparty/nvtt.cmake ) +include( cmake/3rdparty/pvrtc.cmake ) + +# Ensure the directory exists +if( NOT IS_DIRECTORY ${BIMG_DIR} ) + message( SEND_ERROR "Could not load bimg, directory does not exist. ${BIMG_DIR}" ) + return() +endif() + +# Grab the bimg source files +file( GLOB BIMG_SOURCES ${BIMG_DIR}/src/*.cpp ) + +# Create the bimg target +add_library( bimg STATIC ${BIMG_SOURCES} ) + +# Add include directory of bimg +target_include_directories( bimg + PUBLIC + $ + $) + +# bimg dependencies +target_link_libraries( bimg bx astc-codec astc edtaa3 etc1 etc2 iqa squish nvtt pvrtc ) + +# Put in a "bgfx" folder in Visual Studio +set_target_properties( bimg PROPERTIES FOLDER "bgfx" ) \ No newline at end of file diff --git a/extern/rendering/cmake/bx.cmake b/extern/rendering/cmake/bx.cmake new file mode 100644 index 000000000..f93f65f5e --- /dev/null +++ b/extern/rendering/cmake/bx.cmake @@ -0,0 +1,90 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +# Ensure the directory exists +if( NOT IS_DIRECTORY ${BX_DIR} ) + message( SEND_ERROR "Could not load bx, directory does not exist. ${BX_DIR}" ) + return() +endif() + +# Grab the bx source files +file( GLOB BX_SOURCES ${BX_DIR}/src/*.cpp ) + +if(BX_AMALGAMATED) + set(BX_NOBUILD ${BX_SOURCES}) + list(REMOVE_ITEM BX_NOBUILD ${BX_DIR}/src/amalgamated.cpp) + foreach(BX_SRC ${BX_NOBUILD}) + set_source_files_properties( ${BX_SRC} PROPERTIES HEADER_FILE_ONLY ON ) + endforeach() +else() + set_source_files_properties( ${BX_DIR}/src/amalgamated.cpp PROPERTIES HEADER_FILE_ONLY ON ) +endif() + +# Create the bx target +add_library( bx STATIC ${BX_SOURCES} ) + +# Link against psapi on Windows +if( WIN32 ) + target_link_libraries( bx PUBLIC psapi ) +endif() + +include(GNUInstallDirs) + +# Add include directory of bx +target_include_directories( bx + PUBLIC + $ + $ + $ ) + +# Build system specific configurations +if( MSVC OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" ) + target_include_directories( bx + PUBLIC + $ + $ ) +elseif( MINGW ) + target_include_directories( bx + PUBLIC + $ + $ ) +elseif( APPLE ) + target_include_directories( bx + PUBLIC + $ + $ ) +endif() + +# All configurations +target_compile_definitions( bx PUBLIC "__STDC_LIMIT_MACROS" ) +target_compile_definitions( bx PUBLIC "__STDC_FORMAT_MACROS" ) +target_compile_definitions( bx PUBLIC "__STDC_CONSTANT_MACROS" ) + +target_compile_definitions( bx PRIVATE "$<$:BX_CONFIG_DEBUG=1>" ) +if(BGFX_CONFIG_DEBUG) + target_compile_definitions( bx PRIVATE BX_CONFIG_DEBUG=1) +endif() + +# Additional dependencies on Unix +if( UNIX AND NOT APPLE AND NOT ANDROID ) + # Threads + find_package( Threads ) + target_link_libraries( bx ${CMAKE_THREAD_LIBS_INIT} dl ) + + # Real time (for clock_gettime) + target_link_libraries( bx rt ) +elseif(APPLE) + find_library( FOUNDATION_LIBRARY Foundation) + mark_as_advanced( FOUNDATION_LIBRARY ) + target_link_libraries( bx PUBLIC ${FOUNDATION_LIBRARY} ) +endif() + +# Put in a "bgfx" folder in Visual Studio +set_target_properties( bx PROPERTIES FOLDER "bgfx" ) \ No newline at end of file diff --git a/extern/rendering/cmake/examples.cmake b/extern/rendering/cmake/examples.cmake new file mode 100644 index 000000000..8c6837771 --- /dev/null +++ b/extern/rendering/cmake/examples.cmake @@ -0,0 +1,276 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +include( CMakeParseArguments ) + +include( cmake/util/ConfigureDebugging.cmake ) + +include( cmake/3rdparty/dear-imgui.cmake ) +include( cmake/3rdparty/meshoptimizer.cmake ) + +function( add_bgfx_shader FILE FOLDER ) + get_filename_component( FILENAME "${FILE}" NAME_WE ) + string( SUBSTRING "${FILENAME}" 0 2 TYPE ) + if( "${TYPE}" STREQUAL "fs" ) + set( TYPE "FRAGMENT" ) + set( D3D_PREFIX "ps" ) + elseif( "${TYPE}" STREQUAL "vs" ) + set( TYPE "VERTEX" ) + set( D3D_PREFIX "vs" ) + elseif( "${TYPE}" STREQUAL "cs" ) + set( TYPE "COMPUTE" ) + set( D3D_PREFIX "cs" ) + else() + set( TYPE "" ) + endif() + + if( NOT "${TYPE}" STREQUAL "" ) + set( COMMON FILE ${FILE} ${TYPE} INCLUDES ${BGFX_DIR}/src ) + set( OUTPUTS "" ) + set( OUTPUTS_PRETTY "" ) + + if( WIN32 ) + # dx9 + if( NOT "${TYPE}" STREQUAL "COMPUTE" ) + set( DX9_OUTPUT ${BGFX_DIR}/examples/runtime/shaders/dx9/${FILENAME}.bin ) + shaderc_parse( DX9 ${COMMON} WINDOWS PROFILE ${D3D_PREFIX}_3_0 O 3 OUTPUT ${DX9_OUTPUT} ) + list( APPEND OUTPUTS "DX9" ) + set( OUTPUTS_PRETTY "${OUTPUTS_PRETTY}DX9, " ) + endif() + + # dx11 + set( DX11_OUTPUT ${BGFX_DIR}/examples/runtime/shaders/dx11/${FILENAME}.bin ) + if( NOT "${TYPE}" STREQUAL "COMPUTE" ) + shaderc_parse( DX11 ${COMMON} WINDOWS PROFILE ${D3D_PREFIX}_5_0 O 3 OUTPUT ${DX11_OUTPUT} ) + else() + shaderc_parse( DX11 ${COMMON} WINDOWS PROFILE ${D3D_PREFIX}_5_0 O 1 OUTPUT ${DX11_OUTPUT} ) + endif() + list( APPEND OUTPUTS "DX11" ) + set( OUTPUTS_PRETTY "${OUTPUTS_PRETTY}DX11, " ) + endif() + + if( APPLE ) + # metal + set( METAL_OUTPUT ${BGFX_DIR}/examples/runtime/shaders/metal/${FILENAME}.bin ) + shaderc_parse( METAL ${COMMON} OSX PROFILE metal OUTPUT ${METAL_OUTPUT} ) + list( APPEND OUTPUTS "METAL" ) + set( OUTPUTS_PRETTY "${OUTPUTS_PRETTY}Metal, " ) + endif() + + # essl + if( NOT "${TYPE}" STREQUAL "COMPUTE" ) + set( ESSL_OUTPUT ${BGFX_DIR}/examples/runtime/shaders/essl/${FILENAME}.bin ) + shaderc_parse( ESSL ${COMMON} ANDROID OUTPUT ${ESSL_OUTPUT} ) + list( APPEND OUTPUTS "ESSL" ) + set( OUTPUTS_PRETTY "${OUTPUTS_PRETTY}ESSL, " ) + endif() + + # glsl + set( GLSL_OUTPUT ${BGFX_DIR}/examples/runtime/shaders/glsl/${FILENAME}.bin ) + if( NOT "${TYPE}" STREQUAL "COMPUTE" ) + shaderc_parse( GLSL ${COMMON} LINUX PROFILE 120 OUTPUT ${GLSL_OUTPUT} ) + else() + shaderc_parse( GLSL ${COMMON} LINUX PROFILE 430 OUTPUT ${GLSL_OUTPUT} ) + endif() + list( APPEND OUTPUTS "GLSL" ) + set( OUTPUTS_PRETTY "${OUTPUTS_PRETTY}GLSL, " ) + + # spirv + if( NOT "${TYPE}" STREQUAL "COMPUTE" ) + set( SPIRV_OUTPUT ${BGFX_DIR}/examples/runtime/shaders/spirv/${FILENAME}.bin ) + shaderc_parse( SPIRV ${COMMON} LINUX PROFILE spirv OUTPUT ${SPIRV_OUTPUT} ) + list( APPEND OUTPUTS "SPIRV" ) + set( OUTPUTS_PRETTY "${OUTPUTS_PRETTY}SPIRV" ) + set( OUTPUT_FILES "" ) + set( COMMANDS "" ) + endif() + + foreach( OUT ${OUTPUTS} ) + list( APPEND OUTPUT_FILES ${${OUT}_OUTPUT} ) + list( APPEND COMMANDS COMMAND "$" ${${OUT}} ) + get_filename_component( OUT_DIR ${${OUT}_OUTPUT} DIRECTORY ) + file( MAKE_DIRECTORY ${OUT_DIR} ) + endforeach() + + file( RELATIVE_PATH PRINT_NAME ${BGFX_DIR}/examples ${FILE} ) + add_custom_command( + MAIN_DEPENDENCY + ${FILE} + OUTPUT + ${OUTPUT_FILES} + ${COMMANDS} + COMMENT "Compiling shader ${PRINT_NAME} for ${OUTPUTS_PRETTY}" + ) + endif() +endfunction() + +function( add_example ARG_NAME ) + # Parse arguments + cmake_parse_arguments( ARG "COMMON" "" "DIRECTORIES;SOURCES" ${ARGN} ) + + # Get all source files + list( APPEND ARG_DIRECTORIES "${BGFX_DIR}/examples/${ARG_NAME}" ) + set( SOURCES "" ) + set( SHADERS "" ) + foreach( DIR ${ARG_DIRECTORIES} ) + if( APPLE ) + file( GLOB GLOB_SOURCES ${DIR}/*.mm ) + list( APPEND SOURCES ${GLOB_SOURCES} ) + endif() + file( GLOB GLOB_SOURCES ${DIR}/*.c ${DIR}/*.cpp ${DIR}/*.h ${DIR}/*.sc ) + list( APPEND SOURCES ${GLOB_SOURCES} ) + file( GLOB GLOB_SHADERS ${DIR}/*.sc ) + list( APPEND SHADERS ${GLOB_SHADERS} ) + endforeach() + + # Add target + if( ARG_COMMON ) + add_library( example-${ARG_NAME} STATIC EXCLUDE_FROM_ALL ${SOURCES} ) + target_include_directories( example-${ARG_NAME} PUBLIC ${BGFX_DIR}/examples/common ) + target_link_libraries( example-${ARG_NAME} PUBLIC bgfx dear-imgui meshoptimizer ) + if( BGFX_WITH_GLFW ) + find_package( glfw3 REQUIRED ) + target_link_libraries( example-${ARG_NAME} PUBLIC glfw ) + target_compile_definitions( example-${ARG_NAME} PUBLIC ENTRY_CONFIG_USE_GLFW ) + elseif( BGFX_WITH_SDL ) + find_package( SDL2 REQUIRED ) + target_link_libraries( example-${ARG_NAME} PUBLIC ${SDL2_LIBRARIES} ) + target_compile_definitions( example-${ARG_NAME} PUBLIC ENTRY_CONFIG_USE_SDL ) + elseif( UNIX AND NOT APPLE ) + target_link_libraries( example-${ARG_NAME} PUBLIC X11 ) + endif() + else() + if( BGFX_INSTALL_EXAMPLES ) + add_executable( example-${ARG_NAME} WIN32 ${SOURCES} ) + else() + add_executable( example-${ARG_NAME} WIN32 EXCLUDE_FROM_ALL ${SOURCES} ) + endif() + target_link_libraries( example-${ARG_NAME} example-common ) + configure_debugging( example-${ARG_NAME} WORKING_DIR ${BGFX_DIR}/examples/runtime ) + if( MSVC ) + set_target_properties( example-${ARG_NAME} PROPERTIES LINK_FLAGS "/ENTRY:\"mainCRTStartup\"" ) + endif() + if( BGFX_CUSTOM_TARGETS ) + add_dependencies( examples example-${ARG_NAME} ) + endif() + if( IOS ) + set_target_properties(example-${ARG_NAME} PROPERTIES MACOSX_BUNDLE ON + MACOSX_BUNDLE_GUI_IDENTIFIER example-${ARG_NAME} + MACOSX_BUNDLE_BUNDLE_VERSION 0 + MACOSX_BUNDLE_SHORT_VERSION_STRING 0 + XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer") + endif() + endif() + target_compile_definitions( example-${ARG_NAME} PRIVATE "-D_CRT_SECURE_NO_WARNINGS" "-D__STDC_FORMAT_MACROS" "-DENTRY_CONFIG_IMPLEMENT_MAIN=1" ) + + # Configure shaders + if( NOT ARG_COMMON AND NOT IOS AND NOT EMSCRIPTEN) + foreach( SHADER ${SHADERS} ) + add_bgfx_shader( ${SHADER} ${ARG_NAME} ) + endforeach() + source_group( "Shader Files" FILES ${SHADERS}) + endif() + + if (NOT ARG_COMMON AND EMSCRIPTEN) + target_link_libraries(example-${ARG_NAME} + "-s PRECISE_F32=1" + "-s TOTAL_MEMORY=268435456" + "--memory-init-file 1") + endif() + + # Directory name + set_target_properties( example-${ARG_NAME} PROPERTIES FOLDER "bgfx/examples" ) + + if (IOS OR WIN32) + # on iOS we need to build a bundle so have to copy the data rather than symlink + # and on windows we can't create symlinks + add_custom_command( TARGET example-${ARG_NAME} COMMAND ${CMAKE_COMMAND} -E copy_directory ${BGFX_DIR}/examples/runtime/ $) + else() + # For everything else symlink some folders into our output directory + add_custom_command( TARGET example-${ARG_NAME} COMMAND ${CMAKE_COMMAND} -E create_symlink ${BGFX_DIR}/examples/runtime/font $/font) + add_custom_command( TARGET example-${ARG_NAME} COMMAND ${CMAKE_COMMAND} -E create_symlink ${BGFX_DIR}/examples/runtime/images $/images) + add_custom_command( TARGET example-${ARG_NAME} COMMAND ${CMAKE_COMMAND} -E create_symlink ${BGFX_DIR}/examples/runtime/meshes $/meshes) + add_custom_command( TARGET example-${ARG_NAME} COMMAND ${CMAKE_COMMAND} -E create_symlink ${BGFX_DIR}/examples/runtime/shaders $/shaders) + add_custom_command( TARGET example-${ARG_NAME} COMMAND ${CMAKE_COMMAND} -E create_symlink ${BGFX_DIR}/examples/runtime/text $/text) + add_custom_command( TARGET example-${ARG_NAME} COMMAND ${CMAKE_COMMAND} -E create_symlink ${BGFX_DIR}/examples/runtime/textures $/textures) + endif() + +endfunction() + +# Build all examples target +if( BGFX_CUSTOM_TARGETS ) + add_custom_target( examples ) + set_target_properties( examples PROPERTIES FOLDER "bgfx/examples" ) +endif() + +# Add common library for examples +add_example( + common + COMMON + DIRECTORIES + ${BGFX_DIR}/examples/common/debugdraw + ${BGFX_DIR}/examples/common/font + ${BGFX_DIR}/examples/common/imgui + ${BGFX_DIR}/examples/common/nanovg + ${BGFX_DIR}/examples/common/ps +) + +# Only add examples if set, otherwise we still need exmaples common for tools +if( BGFX_BUILD_EXAMPLES ) + # Add examples + set( + BGFX_EXAMPLES + 00-helloworld + 01-cubes + 02-metaballs + 03-raymarch + 04-mesh + 05-instancing + 06-bump + 07-callback + 08-update + 09-hdr + 10-font + 11-fontsdf + 12-lod + 13-stencil + 14-shadowvolumes + 15-shadowmaps-simple + 16-shadowmaps + 17-drawstress + 18-ibl + 19-oit + 20-nanovg +# 21-deferred + 22-windows + 23-vectordisplay + 24-nbody + 25-c99 + 26-occlusion + 27-terrain + 28-wireframe + 29-debugdraw + 30-picking + 31-rsm + 32-particles + 33-pom + 34-mvs + 35-dynamic + 36-sky +# 37-gpudrivenrendering + 38-bloom + 39-assao +# 40-svt + ) + + foreach( EXAMPLE ${BGFX_EXAMPLES} ) + add_example( ${EXAMPLE} ) + endforeach() +endif() diff --git a/extern/rendering/cmake/ovr.cmake b/extern/rendering/cmake/ovr.cmake new file mode 100644 index 000000000..4894e206e --- /dev/null +++ b/extern/rendering/cmake/ovr.cmake @@ -0,0 +1,47 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +# The ovr location is customizable via cache variable BGFX_OVR_DIR +if( NOT BGFX_OVR_DIR ) + set( BGFX_OVR_DIR "${CMAKE_CURRENT_SOURCE_DIR}/LibOVR" CACHE STRING "Location of LibOVR." ) +endif() + +# Ensure the directory exists +if( NOT IS_DIRECTORY ${BGFX_OVR_DIR} ) + message( SEND_ERROR "Could not load LibOVR, directory does not exist. ${BGFX_OVR_DIR}" ) + return() +endif() + +# Create the ovr target +add_library( ovr INTERFACE ) + +# Add include directory of ovr +target_include_directories( ovr INTERFACE ${BGFX_OVR_DIR}/Include ) + +# Add bgfx configuration define +target_compile_definitions( ovr INTERFACE "BGFX_CONFIG_USE_OVR=1" ) + +# Build system specific configurations +if( CMAKE_SIZEOF_VOID_P EQUAL 8 ) + set( ARCH "x64" ) +else() + set( ARCH "Win32" ) +endif() +if( MSVC10 ) + target_link_libraries( ovr INTERFACE ${BGFX_OVR_DIR}/Lib/Windows/${ARCH}/Release/VS2010/LibOVR.lib ) +elseif( MSVC11 ) + target_link_libraries( ovr INTERFACE ${BGFX_OVR_DIR}/Lib/Windows/${ARCH}/Release/VS2012/LibOVR.lib ) +elseif( MSVC12 ) + target_link_libraries( ovr INTERFACE ${BGFX_OVR_DIR}/Lib/Windows/${ARCH}/Release/VS2013/LibOVR.lib ) +elseif( MSVC14 ) + target_link_libraries( ovr INTERFACE ${BGFX_OVR_DIR}/Lib/Windows/${ARCH}/Release/VS2015/LibOVR.lib ) +else() + message( STATUS "OVR not supported on this platform." ) +endif() diff --git a/extern/rendering/cmake/shared.cmake b/extern/rendering/cmake/shared.cmake new file mode 100644 index 000000000..ded8a534d --- /dev/null +++ b/extern/rendering/cmake/shared.cmake @@ -0,0 +1,35 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +add_library( bgfx-vertexlayout INTERFACE ) +configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/generated/vertexlayout.cpp.in + ${CMAKE_CURRENT_BINARY_DIR}/generated/vertexlayout.cpp ) +target_sources( bgfx-vertexlayout INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/generated/vertexlayout.cpp ) +target_include_directories( bgfx-vertexlayout INTERFACE ${BGFX_DIR}/include ) + +add_library( bgfx-shader-spirv INTERFACE ) +configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/generated/shader_spirv.cpp.in + ${CMAKE_CURRENT_BINARY_DIR}/generated/shader_spirv.cpp ) +target_sources( bgfx-shader-spirv INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/generated/shader_spirv.cpp ) +target_include_directories( bgfx-shader-spirv INTERFACE ${BGFX_DIR}/include ) + +add_library( bgfx-bounds INTERFACE ) +configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/generated/bounds.cpp.in + ${CMAKE_CURRENT_BINARY_DIR}/generated/bounds.cpp ) +target_sources( bgfx-bounds INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/generated/bounds.cpp ) +target_include_directories( bgfx-bounds INTERFACE ${BGFX_DIR}/include ) +target_include_directories( bgfx-bounds INTERFACE ${BGFX_DIR}/examples/common ) + +# Frameworks required on OS X +if( ${CMAKE_SYSTEM_NAME} MATCHES Darwin ) + find_library( COCOA_LIBRARY Cocoa ) + mark_as_advanced( COCOA_LIBRARY ) + target_link_libraries( bgfx-vertexlayout INTERFACE ${COCOA_LIBRARY} ) +endif() diff --git a/extern/rendering/cmake/tools.cmake b/extern/rendering/cmake/tools.cmake new file mode 100644 index 000000000..d65855e5b --- /dev/null +++ b/extern/rendering/cmake/tools.cmake @@ -0,0 +1,20 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +if( BGFX_CUSTOM_TARGETS ) + add_custom_target( tools ) + set_target_properties( tools PROPERTIES FOLDER "bgfx/tools" ) +endif() + +include( cmake/tools/geometryc.cmake ) +include( cmake/tools/geometryv.cmake ) +include( cmake/tools/shaderc.cmake ) +include( cmake/tools/texturec.cmake ) +include( cmake/tools/texturev.cmake ) diff --git a/extern/rendering/cmake/tools/brtshaderc.cmake b/extern/rendering/cmake/tools/brtshaderc.cmake new file mode 100644 index 000000000..4bffe12e3 --- /dev/null +++ b/extern/rendering/cmake/tools/brtshaderc.cmake @@ -0,0 +1,77 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +include( CMakeParseArguments ) + +include( cmake/3rdparty/fcpp.cmake ) +include( cmake/3rdparty/glsl-optimizer.cmake ) +include( cmake/3rdparty/glslang.cmake ) +include( cmake/3rdparty/spirv-cross.cmake ) +include( cmake/3rdparty/spirv-tools.cmake ) +include( cmake/3rdparty/webgpu.cmake ) + +if( TARGET brtshaderc ) + return() +endif() + +#file( GLOB brtcshader_SOURCES +#${BGFX_DIR}/tools/shaderc/shaderc_glsl.cpp +#${BGFX_DIR}/tools/shaderc/shaderc_hlsl.cpp +#${BGFX_DIR}/tools/shaderc/shaderc_pssl.cpp +#${BGFX_DIR}/tools/shaderc/shaderc_spirv.cpp +#${BGFX_DIR}/tools/shaderc/shaderc_metal.cpp +#${BGFX_DIR}/tools/brtshaderc/brtshaderc.cpp ) + +file( GLOB brtcshader_SOURCES ${BGFX_DIR}/tools/brtshaderc/brtshaderc.cpp ) + +add_library( brtshaderc STATIC EXCLUDE_FROM_ALL ${brtcshader_SOURCES} ) + +include_directories( +${BGFX_DIR}/examples/common +${BGFX_DIR}/3rdparty +${BGFX_DIR}/3rdparty/fcpp +${BGFX_DIR}/3rdparty/dxsdk/include +${BGFX_DIR}/3rdparty/khronos +$ +$ +$ +$ +$) + +include_directories( +${GLSL-OPTIMIZER_INCLUDES} +${GLSLLANG_INCLUDES} +${SPIRV_CROSS_INCLUDES} +${SPIRV_TOOLS_INCLUDES} +${WEBGPU_INCLUDES}) + +# Build system specific configurations +if( MSVC OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" ) + include_directories($ + $ ) +elseif( MINGW ) + include_directories($ + $ ) +elseif( APPLE ) + include_directories($ + $ ) +endif() + + + +target_compile_definitions( brtshaderc PRIVATE "-D_CRT_SECURE_NO_WARNINGS" ) +set_target_properties( brtshaderc PROPERTIES FOLDER "bgfx/tools" ) + +if (ANDROID) + target_link_libraries(brtshaderc PRIVATE log) +elseif (IOS) + set_target_properties(brtshaderc PROPERTIES MACOSX_BUNDLE ON + MACOSX_BUNDLE_GUI_IDENTIFIER brtshaderc) +endif() diff --git a/extern/rendering/cmake/tools/geometryc.cmake b/extern/rendering/cmake/tools/geometryc.cmake new file mode 100644 index 000000000..3ac78bc13 --- /dev/null +++ b/extern/rendering/cmake/tools/geometryc.cmake @@ -0,0 +1,26 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +include( CMakeParseArguments ) + +include( cmake/3rdparty/meshoptimizer.cmake ) + +add_executable( geometryc ${BGFX_DIR}/tools/geometryc/geometryc.cpp ) +target_compile_definitions( geometryc PRIVATE "-D_CRT_SECURE_NO_WARNINGS" ) +set_target_properties( geometryc PROPERTIES FOLDER "bgfx/tools" ) +target_link_libraries( geometryc bx bgfx-bounds bgfx-vertexlayout meshoptimizer ) +if( BGFX_CUSTOM_TARGETS ) + add_dependencies( tools geometryc ) +endif() + +if (IOS) + set_target_properties(geometryc PROPERTIES MACOSX_BUNDLE ON + MACOSX_BUNDLE_GUI_IDENTIFIER example-${ARG_NAME}) +endif() diff --git a/extern/rendering/cmake/tools/geometryv.cmake b/extern/rendering/cmake/tools/geometryv.cmake new file mode 100644 index 000000000..25b1c17f6 --- /dev/null +++ b/extern/rendering/cmake/tools/geometryv.cmake @@ -0,0 +1,21 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +add_executable( geometryv ${BGFX_DIR}/tools/geometryv/geometryv.cpp ) +set_target_properties( geometryv PROPERTIES FOLDER "bgfx/tools" ) +target_link_libraries( geometryv example-common ) +if( BGFX_CUSTOM_TARGETS ) + add_dependencies( tools geometryv ) +endif() + +if (IOS) + set_target_properties(geometryv PROPERTIES MACOSX_BUNDLE ON + MACOSX_BUNDLE_GUI_IDENTIFIER geometryv) +endif() diff --git a/extern/rendering/cmake/tools/shaderc.cmake b/extern/rendering/cmake/tools/shaderc.cmake new file mode 100644 index 000000000..ff50d0f30 --- /dev/null +++ b/extern/rendering/cmake/tools/shaderc.cmake @@ -0,0 +1,369 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +include( CMakeParseArguments ) + +include( cmake/3rdparty/fcpp.cmake ) +include( cmake/3rdparty/glsl-optimizer.cmake ) +include( cmake/3rdparty/glslang.cmake ) +include( cmake/3rdparty/spirv-cross.cmake ) +include( cmake/3rdparty/spirv-tools.cmake ) +include( cmake/3rdparty/webgpu.cmake ) + +add_executable( shaderc ${BGFX_DIR}/tools/shaderc/shaderc.cpp ${BGFX_DIR}/tools/shaderc/shaderc.h ${BGFX_DIR}/tools/shaderc/shaderc_glsl.cpp ${BGFX_DIR}/tools/shaderc/shaderc_hlsl.cpp ${BGFX_DIR}/tools/shaderc/shaderc_pssl.cpp ${BGFX_DIR}/tools/shaderc/shaderc_spirv.cpp ${BGFX_DIR}/tools/shaderc/shaderc_metal.cpp ) +target_compile_definitions( shaderc PRIVATE "-D_CRT_SECURE_NO_WARNINGS" ) +set_target_properties( shaderc PROPERTIES FOLDER "bgfx/tools" ) +target_link_libraries(shaderc PRIVATE bx bimg bgfx-vertexlayout bgfx-shader-spirv fcpp glsl-optimizer glslang spirv-cross spirv-tools webgpu) + +if( BGFX_CUSTOM_TARGETS ) + add_dependencies( tools shaderc ) +endif() + +if (ANDROID) + target_link_libraries(shaderc PRIVATE log) +elseif (IOS) + set_target_properties(shaderc PROPERTIES MACOSX_BUNDLE ON + MACOSX_BUNDLE_GUI_IDENTIFIER shaderc) +endif() + +function( add_shader ARG_FILE ) + # Parse arguments + cmake_parse_arguments( ARG "FRAGMENT;VERTEX;COMPUTE" "OUTPUT;GLSL_VERSION;DX9_MODEL;DX11_MODEL" "PLATFORMS" ${ARGN} ) + + # Get filename + get_filename_component( FILENAME "${ARG_FILE}" NAME_WE ) + + # Determine if fragment or vertex or compute + if( ARG_FRAGMENT AND ARG_VERTEX AND ARG_COMPUTE ) + message( SEND_ERROR "add_shader cannot be called with all FRAGMENT and VERTEX and COMPUTE." ) + return() + elseif( ARG_FRAGMENT AND ARG_VERTEX ) + message( SEND_ERROR "add_shader cannot be called with both FRAGMENT and VERTEX." ) + return() + elseif( ARG_FRAGMENT AND ARG_COMPUTE ) + message( SEND_ERROR "add_shader cannot be called with both FRAGMENT and COMPUTE." ) + return() + elseif( ARG_VERTEX AND ARG_COMPUTE ) + message( SEND_ERROR "add_shader cannot be called with both VERTEX and COMPUTE." ) + return() + endif() + + if( ARG_FRAGMENT ) + set( TYPE "FRAGMENT" ) + set( D3D_PREFIX "ps" ) + elseif( ARG_VERTEX ) + set( TYPE "VERTEX" ) + set( D3D_PREFIX "vs" ) + elseif( ARG_COMPUTE ) + set( TYPE "COMPUTE" ) + set( D3D_PREFIX "cs" ) + else() + message( SEND_ERROR "add_shader must be called with either FRAGMENT or VERTEX or COMPUTE." ) + return() + endif() + + # Determine compatible platforms + if( ARG_PLATFORMS ) + set( PLATFORMS ${ARG_PLATFORMS} ) + else() + if( MSVC ) + set( PLATFORMS dx9 dx11 glsl essl asm.js spirv ) + elseif( APPLE ) + set( PLATFORMS metal glsl essl asm.js spirv ) + else() + set( PLATFORMS glsl essl asm.js spirv ) + endif() + endif() + + # Build options + set( BASE_OPTIONS + FILE ${ARG_FILE} + ${TYPE} + INCLUDES ${BGFX_DIR}/src + ) + + # Parse profiles + set( DX9_PROFILE PROFILE ${D3D_PREFIX}_3_0 ) + if( ARG_DX9_MODEL ) + set( DX9_PROFILE PROFILE ${D3D_PREFIX}_${ARG_DX9_MODEL} ) + endif() + set( DX11_PROFILE PROFILE ${D3D_PREFIX}_5_0 ) + if( ARG_DX11_MODEL ) + set( DX11_PROFILE PROFILE ${D3D_PREFIX}_${ARG_DX11_MODEL} ) + endif() + set( GLSL_PROFILE PROFILE 120 ) + if( ARG_COMPUTE ) + set( GLSL_PROFILE PROFILE 430 ) + endif() + if( ARG_GLSL_VERSION ) + set( GLSL_PROFILE PROFILE ${ARG_GLSL_VERSION} ) + endif() + set( SPIRV_PROFILE PROFILE spirv ) + + # Add commands + set( OUTPUTS "" ) + set( COMMANDS "" ) + foreach( PLATFORM ${PLATFORMS} ) + set( OPTIONS ${BASE_OPTIONS} ) + set( OUTPUT "${ARG_OUTPUT}/${PLATFORM}/${FILENAME}.bin" ) + get_filename_component( OUTPUT "${OUTPUT}" ABSOLUTE ) + + if( "${PLATFORM}" STREQUAL "dx9" ) + list( APPEND OPTIONS + WINDOWS + ${DX9_PROFILE} + OUTPUT ${OUTPUT} + ) + elseif( "${PLATFORM}" STREQUAL "dx11" ) + list( APPEND OPTIONS + WINDOWS + ${DX11_PROFILE} + OUTPUT ${OUTPUT} + ) + elseif( "${PLATFORM}" STREQUAL "metal" ) + list( APPEND OPTIONS + OSX + PROFILE metal + OUTPUT ${OUTPUT} + ) + elseif( "${PLATFORM}" STREQUAL "glsl" ) + list( APPEND OPTIONS + LINUX + ${GLSL_PROFILE} + OUTPUT ${OUTPUT} + ) + elseif( "${PLATFORM}" STREQUAL "essl" ) + list( APPEND OPTIONS + ANDROID + OUTPUT ${OUTPUT} + ) + elseif( "${PLATFORM}" STREQUAL "asm.js" ) + list( APPEND OPTIONS + ASM_JS + OUTPUT ${OUTPUT} + ) + elseif( "${PLATFORM}" STREQUAL "spirv" ) + list( APPEND OPTIONS + LINUX + ${SPIRV_PROFILE} + OUTPUT ${OUTPUT} + ) + else() + message( SEND_ERROR "add_shader given bad platform: ${PLATFORM}" ) + return() + endif() + + list( APPEND OUTPUTS ${OUTPUT} ) + shaderc_parse( CMD ${OPTIONS} ) + list( APPEND COMMANDS COMMAND "${CMAKE_COMMAND}" -E make_directory "${ARG_OUTPUT}/${PLATFORM}" ) + list( APPEND COMMANDS COMMAND "$" ${CMD} ) + endforeach() + + # Add command + add_custom_command( + MAIN_DEPENDENCY + ${ARG_FILE} + OUTPUT + ${OUTPUTS} + ${COMMANDS} + COMMENT "Compiling shader ${ARG_FILE}" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + ) + + # Add to custom filter + source_group( "Shader Files" FILES ${ARG_FILE} ) +endfunction() + +# shaderc( FILE file OUTPUT file ... ) +# See shaderc_parse() below for inputs +function( shaderc ) + cmake_parse_arguments( ARG "" "FILE;OUTPUT;LABEL" "" ${ARGN} ) + set( LABEL "" ) + if( ARG_LABEL ) + set( LABEL " (${ARG_LABEL})" ) + endif() + shaderc_parse( CLI FILE ${ARG_FILE} OUTPUT ${ARG_OUTPUT} ${ARG_UNPARSED_ARGUMENTS} ) + get_filename_component( OUTDIR "${ARG_OUTPUT}" ABSOLUTE ) + get_filename_component( OUTDIR "${OUTDIR}" DIRECTORY ) + add_custom_command( OUTPUT ${ARG_OUTPUT} + COMMAND ${CMAKE_COMMAND} -E make_directory "${OUTDIR}" + COMMAND "$" ${CLI} + MAIN_DEPENDENCY ${ARG_FILE} + COMMENT "Compiling shader ${ARG_FILE}${LABEL}" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + ) +endfunction() + +# shaderc_parse( +# FILE filename +# OUTPUT filename +# FRAGMENT|VERTEX|COMPUTE +# ANDROID|ASM_JS|IOS|LINUX|NACL|OSX|WINDOWS +# [PROFILE profile] +# [O 0|1|2|3] +# [VARYINGDEF filename] +# [BIN2C filename] +# [INCLUDES include;include] +# [DEFINES include;include] +# [DEPENDS] +# [PREPROCESS] +# [RAW] +# [VERBOSE] +# [DEBUG] +# [DISASM] +# [WERROR] +# ) +function( shaderc_parse ARG_OUT ) + cmake_parse_arguments( ARG "DEPENDS;ANDROID;ASM_JS;IOS;LINUX;NACL;OSX;WINDOWS;PREPROCESS;RAW;FRAGMENT;VERTEX;COMPUTE;VERBOSE;DEBUG;DISASM;WERROR" "FILE;OUTPUT;VARYINGDEF;BIN2C;PROFILE;O" "INCLUDES;DEFINES" ${ARGN} ) + set( CLI "" ) + + # -f + if( ARG_FILE ) + list( APPEND CLI "-f" "${ARG_FILE}" ) + endif() + + # -i + if( ARG_INCLUDES ) + foreach( INCLUDE ${ARG_INCLUDES} ) + list( APPEND CLI "-i" ) + list( APPEND CLI "${INCLUDE}" ) + endforeach() + endif() + + # -o + if( ARG_OUTPUT ) + list( APPEND CLI "-o" "${ARG_OUTPUT}" ) + endif() + + # --bin2c + if( ARG_BIN2C ) + list( APPEND CLI "--bin2c" "${ARG_BIN2C}" ) + endif() + + # --depends + if( ARG_DEPENDS ) + list( APPEND CLI "--depends" ) + endif() + + # --platform + set( PLATFORM "" ) + set( PLATFORMS "ANDROID;ASM_JS;IOS;LINUX;NACL;OSX;WINDOWS" ) + foreach( P ${PLATFORMS} ) + if( ARG_${P} ) + if( PLATFORM ) + message( SEND_ERROR "Call to shaderc_parse() cannot have both flags ${PLATFORM} and ${P}." ) + return() + endif() + set( PLATFORM "${P}" ) + endif() + endforeach() + if( "${PLATFORM}" STREQUAL "" ) + message( SEND_ERROR "Call to shaderc_parse() must have a platform flag: ${PLATFORMS}" ) + return() + elseif( "${PLATFORM}" STREQUAL "ANDROID" ) + list( APPEND CLI "--platform" "android" ) + elseif( "${PLATFORM}" STREQUAL "ASM_JS" ) + list( APPEND CLI "--platform" "asm.js" ) + elseif( "${PLATFORM}" STREQUAL "IOS" ) + list( APPEND CLI "--platform" "ios" ) + elseif( "${PLATFORM}" STREQUAL "LINUX" ) + list( APPEND CLI "--platform" "linux" ) + elseif( "${PLATFORM}" STREQUAL "NACL" ) + list( APPEND CLI "--platform" "nacl" ) + elseif( "${PLATFORM}" STREQUAL "OSX" ) + list( APPEND CLI "--platform" "osx" ) + elseif( "${PLATFORM}" STREQUAL "WINDOWS" ) + list( APPEND CLI "--platform" "windows" ) + endif() + + # --preprocess + if( ARG_PREPROCESS ) + list( APPEND CLI "--preprocess" ) + endif() + + # --define + if( ARG_DEFINES ) + list( APPEND CLI "--defines" ) + set( DEFINES "" ) + foreach( DEFINE ${ARG_DEFINES} ) + if( NOT "${DEFINES}" STREQUAL "" ) + set( DEFINES "${DEFINES}\\\\;${DEFINE}" ) + else() + set( DEFINES "${DEFINE}" ) + endif() + endforeach() + list( APPEND CLI "${DEFINES}" ) + endif() + + # --raw + if( ARG_RAW ) + list( APPEND CLI "--raw" ) + endif() + + # --type + set( TYPE "" ) + set( TYPES "FRAGMENT;VERTEX;COMPUTE" ) + foreach( T ${TYPES} ) + if( ARG_${T} ) + if( TYPE ) + message( SEND_ERROR "Call to shaderc_parse() cannot have both flags ${TYPE} and ${T}." ) + return() + endif() + set( TYPE "${T}" ) + endif() + endforeach() + if( "${TYPE}" STREQUAL "" ) + message( SEND_ERROR "Call to shaderc_parse() must have a type flag: ${TYPES}" ) + return() + elseif( "${TYPE}" STREQUAL "FRAGMENT" ) + list( APPEND CLI "--type" "fragment" ) + elseif( "${TYPE}" STREQUAL "VERTEX" ) + list( APPEND CLI "--type" "vertex" ) + elseif( "${TYPE}" STREQUAL "COMPUTE" ) + list( APPEND CLI "--type" "compute" ) + endif() + + # --varyingdef + if( ARG_VARYINGDEF ) + list( APPEND CLI "--varyingdef" "${ARG_VARYINGDEF}" ) + endif() + + # --verbose + if( ARG_VERBOSE ) + list( APPEND CLI "--verbose" ) + endif() + + # --debug + if( ARG_DEBUG ) + list( APPEND CLI "--debug" ) + endif() + + # --disasm + if( ARG_DISASM ) + list( APPEND CLI "--disasm" ) + endif() + + # --profile + if( ARG_PROFILE ) + list( APPEND CLI "--profile" "${ARG_PROFILE}" ) + endif() + + # -O + if( ARG_O ) + list( APPEND CLI "-O" "${ARG_O}" ) + endif() + + # --Werror + if( ARG_WERROR ) + list( APPEND CLI "--Werror" ) + endif() + + set( ${ARG_OUT} ${CLI} PARENT_SCOPE ) +endfunction() diff --git a/extern/rendering/cmake/tools/texturec.cmake b/extern/rendering/cmake/tools/texturec.cmake new file mode 100644 index 000000000..82fb18d36 --- /dev/null +++ b/extern/rendering/cmake/tools/texturec.cmake @@ -0,0 +1,25 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +include( CMakeParseArguments ) + +add_executable( texturec ${BIMG_DIR}/tools/texturec/texturec.cpp ) +set_target_properties( texturec PROPERTIES FOLDER "bgfx/tools" ) +target_link_libraries( texturec bimg ) +if( BGFX_CUSTOM_TARGETS ) + add_dependencies( tools texturec ) +endif() + +if (ANDROID) + target_link_libraries( texturec log ) +elseif (IOS) + set_target_properties(texturec PROPERTIES MACOSX_BUNDLE ON + MACOSX_BUNDLE_GUI_IDENTIFIER texturec) +endif() \ No newline at end of file diff --git a/extern/rendering/cmake/tools/texturev.cmake b/extern/rendering/cmake/tools/texturev.cmake new file mode 100644 index 000000000..9c8435a35 --- /dev/null +++ b/extern/rendering/cmake/tools/texturev.cmake @@ -0,0 +1,23 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +include( CMakeParseArguments ) + +add_executable( texturev ${BGFX_DIR}/tools/texturev/texturev.cpp ) +set_target_properties( texturev PROPERTIES FOLDER "bgfx/tools" ) +target_link_libraries( texturev example-common ) +if( BGFX_CUSTOM_TARGETS ) + add_dependencies( tools texturev ) +endif() + +if (IOS) + set_target_properties(texturev PROPERTIES MACOSX_BUNDLE ON + MACOSX_BUNDLE_GUI_IDENTIFIER texturev) +endif() \ No newline at end of file diff --git a/extern/rendering/cmake/util/ConfigureDebugging.cmake b/extern/rendering/cmake/util/ConfigureDebugging.cmake new file mode 100644 index 000000000..859eee24e --- /dev/null +++ b/extern/rendering/cmake/util/ConfigureDebugging.cmake @@ -0,0 +1,159 @@ +# ConfigureDebugging.cmake +# Written in 2017 by Joshua Brookover + +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. + +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . +# +# configure_debugging( TARGET [OPTIONS...] ) +# +# Configures the debugging settings in visual studio. +# Results in a no-op on non visual studio builds. +# Must be called in the same cmake file as the add_executable command. +# +# See OPTIONS variable in the function for supported user settings. +# See CONFIGS variable in the function for supported cmake configurations. +# See PROCESSORS variable in the function for supported architecture configurations. +# +# All variables can be set with one of the following formats: +# +# (OPTION) +# (OPTION)_(CONFIG) +# (OPTION)_(CONFIG)_(ARCH) +# (OPTION)_(ARCH) +# +# So, some examples (variables should be all caps): +# +# WORKING_DIR +# WORKING_DIR_X64 +# WORKING_DIR_RELEASE_WIN32 +# WORKING_DIR_X64 +# +# An example of a full command: +# +# configure_debugging(target COMMAND "node.exe" COMMAND_X64 "node64.exe" WORKING_DIR ${CMAKE_SOURCE_DIR} DEBUGGER_ENV "PATH=%PATH%\\;$(ProjectDir)") + +include( CMakeParseArguments ) + +function( configure_debugging ARG_TARGET ) + if( MSVC ) + # Visual Studio Options + set( + OPTIONS + WORKING_DIR LocalDebuggerWorkingDirectory + DEBUGGER_ENV LocalDebuggerEnvironment + COMMAND LocalDebuggerCommand + COMMAND_ARGS LocalDebuggerCommandArguments + ) + + # Valid Configurations + set( + CONFIGS + Debug + Release + MinSizeRel + RelWithDebInfo + ) + + # Processors + set( + PROCESSORS + Win32 + x64 + ) + + # Begin hackery + if( ${CMAKE_SIZEOF_VOID_P} EQUAL 8 ) + set( ACTIVE_PROCESSOR "x64" ) + else() + set( ACTIVE_PROCESSOR "Win32" ) + endif() + # Fix issues with semicolons, thx cmake + foreach( ARG ${ARGN} ) + string( REPLACE ";" "\\\\\\\\\\\\\\;" RES "${ARG}" ) + list( APPEND ARGS "${RES}" ) + endforeach() + # Build options for cmake_parse_arguments, result is ONE_ARG variable + set( ODD ON ) + foreach( OPTION ${OPTIONS} ) + if( ODD ) + set( ARG ${OPTION} ) + list( APPEND ONE_ARG ${ARG} ) + foreach( CONFIG ${CONFIGS} ) + string( TOUPPER ${CONFIG} CONFIG ) + list( APPEND ONE_ARG ${ARG}_${CONFIG} ) + foreach( PROCESSOR ${PROCESSORS} ) + string( TOUPPER ${PROCESSOR} PROCESSOR ) + list( APPEND ONE_ARG ${ARG}_${CONFIG}_${PROCESSOR} ) + endforeach() + endforeach() + foreach( PROCESSOR ${PROCESSORS} ) + string( TOUPPER ${PROCESSOR} PROCESSOR ) + list( APPEND ONE_ARG ${ARG}_${PROCESSOR} ) + endforeach() + set( ODD OFF ) + else() + set( ODD ON ) + endif() + endforeach() + cmake_parse_arguments( ARG "" "${ONE_ARG}" "" ${ARGS} ) + # Parse options, fills in all variables of format ARG_(ARG)_(CONFIG)_(PROCESSOR), for example ARG_WORKING_DIR_DEBUG_X64 + set( ODD ON ) + foreach( OPTION ${OPTIONS} ) + if( ODD ) + set( ARG ${OPTION} ) + foreach( CONFIG ${CONFIGS} ) + string( TOUPPER ${CONFIG} CONFIG_CAP ) + if( "${ARG_${ARG}_${CONFIG_CAP}}" STREQUAL "" ) + set( ARG_${ARG}_${CONFIG_CAP} ${ARG_${ARG}} ) + endif() + foreach( PROCESSOR ${PROCESSORS} ) + string( TOUPPER ${PROCESSOR} PROCESSOR_CAP ) + if( "${ARG_${ARG}_${CONFIG_CAP}_${PROCESSOR_CAP}}" STREQUAL "" ) + if( "${ARG_${ARG}_${PROCESSOR_CAP}}" STREQUAL "" ) + set( ARG_${ARG}_${CONFIG_CAP}_${PROCESSOR_CAP} ${ARG_${ARG}_${CONFIG_CAP}} ) + else() + set( ARG_${ARG}_${CONFIG_CAP}_${PROCESSOR_CAP} ${ARG_${ARG}_${PROCESSOR_CAP}} ) + endif() + endif() + if( NOT "${ARG_${ARG}_${CONFIG_CAP}_${PROCESSOR_CAP}}" STREQUAL "" ) + endif() + endforeach() + endforeach() + set( ODD OFF ) + else() + set( ODD ON ) + endif() + endforeach() + # Create string to put in proj.vcxproj.user file + set( RESULT "\n" ) + foreach( CONFIG ${CONFIGS} ) + string( TOUPPER ${CONFIG} CONFIG_CAPS ) + foreach( PROCESSOR ${PROCESSORS} ) + if( "${PROCESSOR}" STREQUAL "${ACTIVE_PROCESSOR}" ) + string( TOUPPER ${PROCESSOR} PROCESSOR_CAPS ) + set( RESULT "${RESULT}\n " ) + set( ODD ON ) + foreach( OPTION ${OPTIONS} ) + if( ODD ) + set( ARG ${OPTION} ) + set( ODD OFF ) + else() + set( VALUE ${ARG_${ARG}_${CONFIG_CAPS}_${PROCESSOR_CAPS}} ) + if( NOT "${VALUE}" STREQUAL "" ) + set( RESULT "${RESULT}\n <${OPTION}>${VALUE}" ) + endif() + set( ODD ON ) + endif() + endforeach() + set( RESULT "${RESULT}\n " ) + endif() + endforeach() + endforeach() + set( RESULT "${RESULT}\n" ) + file( WRITE ${CMAKE_CURRENT_BINARY_DIR}/${ARG_TARGET}.vcxproj.user "${RESULT}" ) + endif() +endfunction() diff --git a/extern/rendering/cmake/version.cmake b/extern/rendering/cmake/version.cmake new file mode 100644 index 000000000..2bf256bc8 --- /dev/null +++ b/extern/rendering/cmake/version.cmake @@ -0,0 +1,37 @@ +# bgfx versioning scheme: +# bgfx 1.104.7082 +# ^ ^^^ ^^^^ +# | | +--- Commit number (https://github.com/bkaradzic/bgfx / git rev-list --count HEAD) +# | +------- API version (from https://github.com/bkaradzic/bgfx/blob/master/scripts/bgfx.idl#L4) +# +--------- Major revision (always 1) + +# BGFX_API_VERSION generated from https://github.com/bkaradzic/bgfx/blob/master/scripts/bgfx.idl#L4 +# bgfx/src/version.h: +# BGFX_REV_NUMBER +# BGFX_REV_SHA1 + +find_package(Git QUIET) + +execute_process(COMMAND "${GIT_EXECUTABLE}" -C bgfx log --pretty=format:'%h' -n 1 + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE GIT_REV + ERROR_QUIET) + +execute_process(COMMAND "${GIT_EXECUTABLE}" -C bgfx rev-list --count HEAD + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE GIT_REV_COUNT + ERROR_QUIET) + +# read version(100) from bgfx.idl +file(READ "${CMAKE_CURRENT_SOURCE_DIR}/bgfx/scripts/bgfx.idl" BGFX_IDL) +string(REGEX MATCH "version\\(([^\)]+)\\)" + BGFX_API_VERSION ${BGFX_IDL}) +set(BGFX_API_VERSION ${CMAKE_MATCH_1}) +set(BGFX_REV_NUMBER ${GIT_REV_COUNT}) +set(BGFX_REV ${GIT_REV}) + +# set project specific versions +set(PROJECT_VERSION 1.${BGFX_API_VERSION}.${BGFX_REV_NUMBER}) +set(PROJECT_VERSION_MAJOR 1) +set(PROJECT_VERSION_MINOR ${BGFX_API_VERSION}) +set(PROJECT_VERSION_PATCH ${BGFX_REV_NUMBER}) diff --git a/extern/rendering/generated/bounds.cpp.in b/extern/rendering/generated/bounds.cpp.in new file mode 100644 index 000000000..1e3029e7d --- /dev/null +++ b/extern/rendering/generated/bounds.cpp.in @@ -0,0 +1 @@ +#include "@BGFX_DIR@/examples/common/bounds.cpp" diff --git a/extern/rendering/generated/shader_spirv.cpp.in b/extern/rendering/generated/shader_spirv.cpp.in new file mode 100644 index 000000000..89feeb025 --- /dev/null +++ b/extern/rendering/generated/shader_spirv.cpp.in @@ -0,0 +1 @@ +#include "@BGFX_DIR@/src/shader_spirv.cpp" diff --git a/extern/rendering/generated/vertexlayout.cpp.in b/extern/rendering/generated/vertexlayout.cpp.in new file mode 100644 index 000000000..a007bb59e --- /dev/null +++ b/extern/rendering/generated/vertexlayout.cpp.in @@ -0,0 +1 @@ +#include "@BGFX_DIR@/src/vertexlayout.cpp" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 559a4439c..5001ee738 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -34,6 +34,9 @@ else() # Always generate debug information add_compile_options(-g) add_link_options(-g) + + target_include_directories( iqa PUBLIC $ ) + endif() add_subdirectory(apps) diff --git a/src/apps/ENGINE/CMakeLists.txt b/src/apps/ENGINE/CMakeLists.txt index b87ce6667..3b5c527e3 100644 --- a/src/apps/ENGINE/CMakeLists.txt +++ b/src/apps/ENGINE/CMakeLists.txt @@ -63,7 +63,7 @@ add_executable(${PROJECT_NAME} WIN32 token.cpp token.h VmaInit.h - + ${STORM_RESOURCE_DIR}/engine/resource.rc ) @@ -142,4 +142,8 @@ add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory "${STORM_SOURCE_DIR}/shared" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/resource/shared" -) \ No newline at end of file +) + +if( MSVC ) + set_target_properties( ${PROJECT_NAME} PROPERTIES LINK_FLAGS "/ENTRY:\"mainCRTStartup\"" ) +endif() diff --git a/src/apps/ENGINE/Main.cpp b/src/apps/ENGINE/Main.cpp index a049c3e19..97562a4d9 100644 --- a/src/apps/ENGINE/Main.cpp +++ b/src/apps/ENGINE/Main.cpp @@ -11,10 +11,30 @@ #include #include +#include "bx/bx.h" +#include "bgfx/bgfx.h" +#include "bgfx/platform.h" + +#include "sprite_renderer.h" + +#include "common.h" + +#include "dx9render.h" +#include + + VFILE_SERVICE *fio = nullptr; CORE core; S_DEBUG CDebug; +VDX9RENDER *renderService; + +std::shared_ptr texture; +std::shared_ptr texture2; + +long PortraitID; + + using namespace storm; namespace @@ -45,7 +65,8 @@ void HandleWindowEvent(const OSWindow::Event &event) } } -int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) + +int main(int argc, char **argv) { // Prevent multiple instances if (!CreateEventA(nullptr, false, false, "Global\\FBBD2286-A9F1-4303-B60C-743C3D7AA7BE") || @@ -132,19 +153,74 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, // evaluate SteamApi singleton steamapi::SteamApi::getInstance(!bSteam); + // Collect information about the window from SDL std::shared_ptr window = storm::OSWindow::Create(width, height, fullscreen); window->SetTitle("Sea Dogs"); core.Set_Hwnd(static_cast(window->OSHandle())); window->Subscribe(HandleWindowEvent); window->Show(); + // and give the pointer to the window to pd + bgfx::PlatformData pd; + // TODO for platforms this goes inside OSWindow->OSHandle() +#if BX_PLATFORM_LINUX || BX_PLATFORM_BSD + pd.ndt = wmi.info.x11.display; + pd.nwh = (void *)(uintptr_t)wmi.info.x11.window; +#elif BX_PLATFORM_OSX + pd.ndt = NULL; + pd.nwh = wmi.info.cocoa.window; +#elif BX_PLATFORM_WINDOWS + pd.ndt = NULL; + pd.nwh = static_cast(window->OSHandle()); +#elif BX_PLATFORM_STEAMLINK + pd.ndt = wmi.info.vivante.display; + pd.nwh = wmi.info.vivante.window; +#endif // BX_PLATFORM_ + pd.context = NULL; + pd.backBuffer = NULL; + pd.backBufferDS = NULL; + + // Tell bgfx about the platform and window + bgfx::setPlatformData(pd); + + // Render an empty frame + bgfx::renderFrame(); + + // Init bgfx + bgfx::Init init; + init.type = bgfx::RendererType::Direct3D11; + init.vendorId = BGFX_PCI_ID_NONE; + init.resolution.width = width; + init.resolution.height = height; + init.resolution.reset = BGFX_RESET_NONE; + + bgfx::init(init); + + const bgfx::Caps *caps = bgfx::getCaps(); + // bool swapChainSupported = 0 != (caps->supported & BGFX_CAPS_SWAP_CHAIN); + + // Enable m_debug text. + bgfx::setDebug(BGFX_DEBUG_TEXT); + + // Set view 0 clear state. + // bgfx::setViewClear(0, BGFX_CLEAR_NONE, 0x303030ff, 1.0f, 0); + + bgfx::setViewRect(0, 0, 0, uint16_t(width), uint16_t(height)); + bgfx::setViewRect(1, 0, 0, uint16_t(width), uint16_t(height)); + //bgfx::setViewRect(2, 0, 0, uint16_t(width), uint16_t(height)); + + /* Init stuff */ core.InitBase(); + core.Run(); // to allow service initialization, remove/comment after tests + + renderService = static_cast(core.CreateService("dx9render")); + /* Message loop */ - auto dwOldTime = GetTickCount(); + auto dwOldTime = GetTickCount64(); - isRunning = true; + isRunning = renderService != nullptr; while (isRunning) { SDL_PumpEvents(); @@ -155,12 +231,14 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, if (dwMaxFPS) { const auto dwMS = 1000u / dwMaxFPS; - const auto dwNewTime = GetTickCount(); + const auto dwNewTime = GetTickCount64(); if (dwNewTime - dwOldTime < dwMS) continue; dwOldTime = dwNewTime; } + const auto runResult = core.Run(); + if (!isHold && !runResult) { isHold = true; @@ -181,7 +259,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, core.ReleaseBase(); ClipCursor(nullptr); + // Shutdown bgfx. + bgfx::shutdown(); + + SDL_Quit(); return 0; -} +} \ No newline at end of file diff --git a/src/apps/ENGINE/file_service.cpp b/src/apps/ENGINE/file_service.cpp index cb07a21ff..f6c95156a 100644 --- a/src/apps/ENGINE/file_service.cpp +++ b/src/apps/ENGINE/file_service.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include #define COMMENT ';' #define SECTION_A '[' @@ -78,6 +81,24 @@ bool FILE_SERVICE::_WriteFile(std::fstream &fileS, const void *s, std::streamsiz } } +bool FILE_SERVICE::_OldReadFile(std::filesystem::path path, void *buffer, uint32_t bytes_to_read, uint32_t *bytes_read, uint32_t seek_to) +{ + std::basic_ifstream fh; + + + fh.open(path.wstring(), std::fstream::in | std::fstream::binary); + + if (!fh.is_open()) + { + return false; + } + fh.seekg(seek_to); + fh.read((unsigned char *)buffer, bytes_to_read); + *bytes_read = fh.gcount(); + + return *bytes_read == bytes_to_read; +} + bool FILE_SERVICE::_ReadFile(std::fstream &fileS, void *s, std::streamsize count) { fileS.exceptions(std::fstream::failbit | std::fstream::badbit); diff --git a/src/apps/ENGINE/file_service.h b/src/apps/ENGINE/file_service.h index d54b08579..b4f5589a8 100644 --- a/src/apps/ENGINE/file_service.h +++ b/src/apps/ENGINE/file_service.h @@ -102,6 +102,9 @@ class FILE_SERVICE : public VFILE_SERVICE std::filesystem::file_time_type _GetLastWriteTime(const char *filename) override; void _FlushFileBuffers(std::fstream &fileS) override; std::string _GetCurrentDirectory() override; + + bool _OldReadFile(std::filesystem::path path, void *buffer, uint32_t bytes_to_read, uint32_t *bytes_read, uint32_t seek_to = 0) override; + std::string _GetExecutableDirectory() override; std::uintmax_t _GetFileSize(const char *filename) override; void _SetCurrentDirectory(const char *pathName) override; diff --git a/src/libs/Common_h/CMakeLists.txt b/src/libs/Common_h/CMakeLists.txt index 37e1d3bd8..ed1f87253 100644 --- a/src/libs/Common_h/CMakeLists.txt +++ b/src/libs/Common_h/CMakeLists.txt @@ -11,7 +11,86 @@ target_include_directories(${PROJECT_NAME} ) target_link_libraries(${PROJECT_NAME} - INTERFACE + INTERFACE ${DirectX_D3DX9_LIBRARY} CONAN_PKG::spdlog ) + +set(PROJECT_SOURCES + math3d/Color.h + math3d/Matrix.h + math3d/Plane.h + math3d/Quaternion.h + math3d/Sphere.h + math3d/Triangle.h + math3d/Vector.h + math3d/Vector4.h + + particles/gmx_QSort.h + particles/iparticlemanager.h + particles/iparticlesservice.h + particles/iparticlesystem.h + + Sd2_h/CannonTrace.h + Sd2_h/SaveLoad.h + Sd2_h/VAI_ObjBase.h + + Animation.h + attributes.h + Character.h + collide.h + controls.h + core.h + Cvector.h + CVector4.h + defines.h + dtimer.h + dx9render.h + Entity.h + entity_state.h + EntityManager.h + filesystem.h + geometry.h + geos.h + inlines.h + Intel.h + Island_Base.h + math3D.h + Matrix.h + message.h + model.h + object.h + particles.h + PtcData.h + rands.h + s_import_func.h + safequeue.h + sail_base.h + script_libriary.h + sea_base.h + service.h + ship_base.h + ship_msg.h + ShipLights.h + storm_assert.h + stringService.h + strutils.h + tga.h + IVBufferManager.h + triangle.h + types3d.h + utf8.h + v_s_stack.h + vdata.h + vfile_service.h + VideoTexture.h + vmodule_api.h + vparticle_system.h + Weather_Base.h + + sprite_renderer.h + primitive_renderer.h + +) + +add_custom_target(${PROJECT_NAME}_ SOURCES ${PROJECT_SOURCES}) diff --git a/src/libs/Common_h/Matrix.h b/src/libs/Common_h/Matrix.h index 3440e5aa1..ba086c520 100644 --- a/src/libs/Common_h/Matrix.h +++ b/src/libs/Common_h/Matrix.h @@ -4,6 +4,10 @@ #include #include +#include "gtc/matrix_transform.hpp" +#include "gtc/type_ptr.hpp" +#include "matrix.hpp" + //============================================================================================ /* @@ -93,6 +97,10 @@ class CMatrix // Calculate scaling matrix CMatrix &BuildScale(const CVECTOR &scale); + + // Check equality + bool operator==(const CMatrix &other); + // Equal void operator=(const CMatrix &matrix); @@ -112,6 +120,7 @@ class CMatrix // Transposition void Transposition(); void Transposition3X3(); + CMatrix Transpose(); CMatrix &Transposition4x4(); // Rotate @@ -399,6 +408,26 @@ inline void CMatrix::operator=(const CMatrix &matrix) }*/ } +inline bool CMatrix::operator==(const CMatrix &matrix) +{ + return this->matrix[0] == matrix.matrix[0] && + this->matrix[1] == matrix.matrix[1] && + this->matrix[2] == matrix.matrix[2] && + this->matrix[3] == matrix.matrix[3] && + this->matrix[4] == matrix.matrix[4] && + this->matrix[5] == matrix.matrix[5] && + this->matrix[6] == matrix.matrix[6] && + this->matrix[7] == matrix.matrix[7] && + this->matrix[8] == matrix.matrix[8] && + this->matrix[9] == matrix.matrix[9] && + this->matrix[10] == matrix.matrix[10] && + this->matrix[11] == matrix.matrix[11] && + this->matrix[12] == matrix.matrix[12] && + this->matrix[13] == matrix.matrix[13] && + this->matrix[14] == matrix.matrix[14] && + this->matrix[15] == matrix.matrix[15]; +} + // Multiply inline void CMatrix::operator*=(CMatrix &matrix) { @@ -542,6 +571,66 @@ inline void CMatrix::Transposition3X3() }*/ } +inline CMatrix CMatrix::Transpose() +{ + CMatrix ret; + + /*glm::mat4 currentMatrix(0); + + currentMatrix[0][0] = matrix[0]; + currentMatrix[0][1] = matrix[1]; + currentMatrix[0][2] = matrix[2]; + currentMatrix[0][3] = matrix[3]; + + currentMatrix[1][0] = matrix[4]; + currentMatrix[1][1] = matrix[5]; + currentMatrix[1][2] = matrix[6]; + currentMatrix[1][3] = matrix[7]; + + currentMatrix[2][0] = matrix[8]; + currentMatrix[2][1] = matrix[9]; + currentMatrix[2][2] = matrix[10]; + currentMatrix[2][3] = matrix[11]; + + currentMatrix[3][0] = matrix[12]; + currentMatrix[3][1] = matrix[13]; + currentMatrix[3][2] = matrix[14]; + currentMatrix[3][3] = matrix[15]; + + currentMatrix = glm::transpose(currentMatrix); + + ret.m[0][0] = currentMatrix[0][0]; + ret.m[0][1] = currentMatrix[0][1]; + ret.m[0][2] = currentMatrix[0][2]; + ret.m[0][3] = currentMatrix[0][3]; + + ret.m[1][0] = currentMatrix[1][0]; + ret.m[1][1] = currentMatrix[1][1]; + ret.m[1][2] = currentMatrix[1][2]; + ret.m[1][3] = currentMatrix[1][3]; + + ret.m[2][0] = currentMatrix[2][0]; + ret.m[2][1] = currentMatrix[2][1]; + ret.m[2][2] = currentMatrix[2][2]; + ret.m[2][3] = currentMatrix[2][3]; + + ret.m[3][0] = currentMatrix[3][0]; + ret.m[3][1] = currentMatrix[3][1]; + ret.m[3][2] = currentMatrix[3][2]; + ret.m[3][3] = currentMatrix[3][3];*/ + + for (int n = 0; n < 4 * 4; n++) + { + int i = n / 4; + int j = n % 4; + ret.matrix[n] = matrix[4 * j + i]; + } + + return ret; + + +} + inline CMatrix &CMatrix::Transposition4x4() { float tmp; diff --git a/src/libs/Common_h/defines.h b/src/libs/Common_h/defines.h index 98622ae07..6aa68c9bb 100644 --- a/src/libs/Common_h/defines.h +++ b/src/libs/Common_h/defines.h @@ -235,6 +235,16 @@ template T Sqr(T t1) return (t1 * t1); }; +#define BGFX_TEXTURE_RELEASE(rs, idtex) \ + { \ + if (rs != NULL && idtex != -1) \ + { \ + rs->BGFXTextureRelease(idtex); \ + idtex = -1; \ + } \ + } + + #define TEXTURE_RELEASE(rs, idtex) \ { \ if (rs != NULL && idtex != -1) \ diff --git a/src/libs/Common_h/dx9render.h b/src/libs/Common_h/dx9render.h index a803f13d2..d0e201ca5 100644 --- a/src/libs/Common_h/dx9render.h +++ b/src/libs/Common_h/dx9render.h @@ -11,6 +11,8 @@ #include "types3d.h" #include "utf8.h" +#include "glm.hpp" + #define FONT_DEFAULT 0 // define print text alignments @@ -66,6 +68,10 @@ struct RS_LINE2D class CVideoTexture; +struct TextureResource; +class SpriteRenderer; +class PrimitiveRenderer; + class VDX9RENDER : public SERVICE { public: @@ -107,6 +113,11 @@ class VDX9RENDER : public SERVICE virtual void SetTransform(long type, D3DMATRIX *mtx) = 0; virtual void GetTransform(long type, D3DMATRIX *mtx) = 0; + virtual void BGFXSetTransform(long type, D3DMATRIX *mtx) = 0; + virtual void BGFXSetTransformUpdateViews(long type, D3DMATRIX *mtx) = 0; + virtual void BGFXGetTransform(long type, D3DMATRIX *mtx) = 0; + + virtual bool SetCamera(const CVECTOR &pos, const CVECTOR &ang, float perspective) = 0; virtual bool SetCamera(const CVECTOR &pos, const CVECTOR &ang) = 0; virtual bool SetCamera(CVECTOR lookFrom, CVECTOR lookTo, CVECTOR up) = 0; @@ -117,8 +128,10 @@ class VDX9RENDER : public SERVICE // DX9Render: Textures Section virtual long TextureCreate(const char *fname) = 0; + virtual long BGFXTextureCreate(const char *fname) = 0; virtual bool TextureSet(long stage, long texid) = 0; virtual bool TextureRelease(long texid) = 0; + virtual bool BGFXTextureRelease(long texid) = 0; // DX9Render: Fonts Section virtual long Print(long x, long y, const char *format, ...) = 0; @@ -142,6 +155,19 @@ class VDX9RENDER : public SERVICE virtual bool TechniqueExecuteStart(const char *cBlockName) = 0; virtual bool TechniqueExecuteNext() = 0; + virtual std::shared_ptr GetSpriteRenderer() = 0; + virtual std::shared_ptr GetPrimitiveRenderer() = 0; + + virtual void DrawSprite(std::shared_ptr texture, uint32_t color, + const glm::vec2 &position, float depth) = 0; + + virtual void DrawSprite(std::shared_ptr texture, const glm::vec4 &src, uint32_t color, + const glm::vec2 &position, const glm::vec2 &origin, const glm::vec2 &scale, float angle, + float depth, bool flip_x, bool flip_y) = 0; + + virtual void DrawSprites(std::shared_ptr texture, std::vector &vertices, glm::vec2 &u, + glm::vec2 &v, uint32_t &color) = 0; + // DX9Render: Draw Section virtual void DrawRects(RS_RECT *pRSR, uint32_t dwRectsNum, const char *cBlockName = nullptr, uint32_t dwSubTexturesX = 1, uint32_t dwSubTexturesY = 1, float fScaleX = 1.0f, @@ -318,4 +344,5 @@ class VDX9RENDER : public SERVICE virtual void SetGLOWParams(float _fBlurBrushSize, long _GlowIntensity, long _GlowPasses) = 0; virtual IDirect3DBaseTexture9 *GetTextureFromID(long nTextureID) = 0; + virtual std::shared_ptr GetBGFXTextureFromID(long nTextureID) = 0; }; diff --git a/src/libs/Common_h/math3d/Triangle.h b/src/libs/Common_h/math3d/Triangle.h index 66a0ce29c..35bb6d7eb 100644 --- a/src/libs/Common_h/math3d/Triangle.h +++ b/src/libs/Common_h/math3d/Triangle.h @@ -12,7 +12,7 @@ #include "Vector.h" // Class for representing a triangle in 3D space -class Triangle +class StormTriangle { public: union { @@ -38,17 +38,17 @@ class Triangle // ----------------------------------------------------------- public: // Empty constructor - Triangle(){}; + StormTriangle(){}; // Copy constructor - Triangle(const Triangle &t); - Triangle(const Vector *v); + StormTriangle(const StormTriangle &t); + StormTriangle(const Vector *v); // ----------------------------------------------------------- // Operators // ----------------------------------------------------------- public: // per component multiplication with assignment - Triangle &operator*=(const Vector &v); + StormTriangle &operator*=(const Vector &v); // ----------------------------------------------------------- // Utilities @@ -78,7 +78,7 @@ class Triangle bool FindClosestPoint(const Vector &trgNormal, Vector &pointOnPlane) const; // Convert vertex coordinates - Triangle &Transform(const Matrix &mtx); + StormTriangle &Transform(const Matrix &mtx); enum CoIntersectionResult { @@ -97,7 +97,7 @@ class Triangle }; // Checking triangles for intersections in one plane - CoIntersectionResult IsCoplanarIntersection(const Triangle &t, float intsEps = 0.0000001f) const; + CoIntersectionResult IsCoplanarIntersection(const StormTriangle &t, float intsEps = 0.0000001f) const; private: static long z_sysClipTriangleEdgePlane(Plane plane, Vector src[8], Vector dst[8], long count); @@ -108,7 +108,7 @@ class Triangle // =========================================================== // Empty constructor -inline Triangle::Triangle(const Triangle &t) +inline StormTriangle::StormTriangle(const StormTriangle &t) { p1 = t.p1; p2 = t.p2; @@ -116,7 +116,7 @@ inline Triangle::Triangle(const Triangle &t) } // Fill all components -inline Triangle::Triangle(const Vector *v) +inline StormTriangle::StormTriangle(const Vector *v) { p[0] = v[0]; p[1] = v[1]; @@ -128,7 +128,7 @@ inline Triangle::Triangle(const Vector *v) // =========================================================== // per component multiplication with assignment -inline Triangle &Triangle::operator*=(const Vector &v) +inline StormTriangle &StormTriangle::operator*=(const Vector &v) { p1 *= v; p2 *= v; @@ -139,9 +139,9 @@ inline Triangle &Triangle::operator*=(const Vector &v) /*!\relates Triangle Multiply triangle by matrix */ -inline Triangle operator*(const Matrix &mtx, const Triangle &t) +inline StormTriangle operator*(const Matrix &mtx, const StormTriangle &t) { - Triangle trg; + StormTriangle trg; trg.p1 = mtx * t.p1; trg.p2 = mtx * t.p2; trg.p3 = mtx * t.p3; @@ -151,9 +151,9 @@ inline Triangle operator*(const Matrix &mtx, const Triangle &t) /*!\relates Triangle Multiply triangle by matrix */ -inline Triangle operator*(const Triangle &t, const Matrix &mtx) +inline StormTriangle operator*(const StormTriangle &t, const Matrix &mtx) { - Triangle trg; + StormTriangle trg; trg.p1 = mtx * t.p1; trg.p2 = mtx * t.p2; trg.p3 = mtx * t.p3; @@ -165,25 +165,25 @@ inline Triangle operator*(const Triangle &t, const Matrix &mtx) // =========================================================== // Get normal -inline Vector Triangle::GetNormal() const +inline Vector StormTriangle::GetNormal() const { return !((p1 - p2) ^ (p1 - p3)); } // Get midpoint -inline Vector Triangle::GetCenter() const +inline Vector StormTriangle::GetCenter() const { return (p1 + p2 + p3) * 0.33333333333333f; } // Get triangle -inline Plane Triangle::GetPlane() const +inline Plane StormTriangle::GetPlane() const { return Plane(GetNormal(), p1); } // Plane passing through the face (p [start% 2], p [(start + 1)% 2]) -inline Plane Triangle::OrtoPlane(long start) const +inline Plane StormTriangle::OrtoPlane(long start) const { Plane plane; // Vertices @@ -197,25 +197,25 @@ inline Plane Triangle::OrtoPlane(long start) const } // The plane passing through the face (p [0], p [1]) -inline Plane Triangle::OrtoPlane01() const +inline Plane StormTriangle::OrtoPlane01() const { return OrtoPlane(0); } // The plane passing through the face (p [1], p [2]) -inline Plane Triangle::OrtoPlane12() const +inline Plane StormTriangle::OrtoPlane12() const { return OrtoPlane(1); } // The plane passing through the face (p [2], p [0]) -inline Plane Triangle::OrtoPlane20() const +inline Plane StormTriangle::OrtoPlane20() const { return OrtoPlane(2); } // Get previous index of triangle vertex -inline long Triangle::PrevIndex(long index) +inline long StormTriangle::PrevIndex(long index) { index--; if (index < 0) @@ -226,7 +226,7 @@ inline long Triangle::PrevIndex(long index) } // Get the next vertex index of a triangle -inline long Triangle::NextIndex(long index) +inline long StormTriangle::NextIndex(long index) { index++; if (index < 0) @@ -237,7 +237,7 @@ inline long Triangle::NextIndex(long index) } // Find the nearest point in a triangle to a given one, lying in the plane of the triangle, true - inside the triangle -inline bool Triangle::FindClosestPoint(const Vector &trgNormal, Vector &pointOnPlane) const +inline bool StormTriangle::FindClosestPoint(const Vector &trgNormal, Vector &pointOnPlane) const { const Vector *cPoint = nullptr; for (long i = 0; i < 3; i++) @@ -282,7 +282,7 @@ inline bool Triangle::FindClosestPoint(const Vector &trgNormal, Vector &pointOnP } // Convert vertex coordinates -inline Triangle &Triangle::Transform(const Matrix &mtx) +inline StormTriangle &StormTriangle::Transform(const Matrix &mtx) { p1 = mtx.MulVertex(p1); p2 = mtx.MulVertex(p2); @@ -291,7 +291,7 @@ inline Triangle &Triangle::Transform(const Matrix &mtx) } // Checking triangles for intersections in one plane -inline Triangle::CoIntersectionResult Triangle::IsCoplanarIntersection(const Triangle &t, float intsEps) const +inline StormTriangle::CoIntersectionResult StormTriangle::IsCoplanarIntersection(const StormTriangle &t, float intsEps) const { // Checking the dimensions of the triangles if (~(p1 - p2) < intsEps * intsEps || ~(p2 - p3) < intsEps * intsEps || ~(p3 - p1) < intsEps * intsEps) @@ -347,7 +347,7 @@ inline Triangle::CoIntersectionResult Triangle::IsCoplanarIntersection(const Tri return cir_intersection; } -inline long Triangle::z_sysClipTriangleEdgePlane(Plane plane, Vector src[8], Vector dst[8], long count) +inline long StormTriangle::z_sysClipTriangleEdgePlane(Plane plane, Vector src[8], Vector dst[8], long count) { float ds = plane * src[0], de; long c = 0; diff --git a/src/libs/Common_h/primitive_renderer.h b/src/libs/Common_h/primitive_renderer.h new file mode 100644 index 000000000..976dc9f45 --- /dev/null +++ b/src/libs/Common_h/primitive_renderer.h @@ -0,0 +1,124 @@ +#ifndef PRIMITIVE_RENDERER_DEF +#define PRIMITIVE_RENDERER_DEF + +#include "bgfx_utils.h" + +#include "glm.hpp" + +#include +#include + +struct VERTEX_POSITION_COLOR +{ + float x; + float y; + float z; + + uint32_t color; + + static void Init() + { + pc_layout.begin() + .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) + .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) + .end(); + }; + + static bgfx::VertexLayout pc_layout; +}; + +struct VERTEX_POSITION_TEXTURE +{ + float x; + float y; + float z; + + float tu, tv; + + static void Init() + { + pt_layout.begin() + .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) + .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float, true) + .end(); + }; + + static bgfx::VertexLayout pt_layout; +}; + +struct VERTEX_POSITION_TEXTURE_COLOR +{ + float x; + float y; + float z; + + float tu, tv; + uint32_t color; + + VERTEX_POSITION_TEXTURE_COLOR(float x, float y, float z, float tu, float tv, uint32_t color) + { + this->x = x; + this->y = y; + this->z = z; + this->tu = tu; + this->tv = tv; + this->color = color; + } + + VERTEX_POSITION_TEXTURE_COLOR(const VERTEX_POSITION_TEXTURE_COLOR &other) + { + this->x = other.x; + this->y = other.y; + this->z = other.z; + + this->tu = other.tu; + this->tv = other.tv; + this->color = other.color; + } + + static void Init() + { + ptc_layout.begin() + .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) + .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float, true) + .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) + .end(); + }; + + static bgfx::VertexLayout ptc_layout; +}; + + + +class PrimitiveRenderer +{ + bgfx::UniformHandle s_texture; + bgfx::ProgramHandle m_primitiveProg; + bgfx::ProgramHandle m_texturedProg; + bgfx::ProgramHandle m_texturedColorProg; + + long m_width; + long m_height; + + public: + std::shared_ptr Texture; + + PrimitiveRenderer(long m_fbWidth, long m_fbHeight); + ~PrimitiveRenderer(); + + void PushVertices(std::vector &vertices); + void PushVertices(std::vector &vertices); + void PushVertices(std::vector &vertices); + + void TestSubmit(std::vector &vertices); + void Submit(std::vector &vertices, std::vector> &uv, + std::vector &colors); + + private: + void ReleaseTexture(std::shared_ptr texture); + void Submit(std::vector &vertices); + void Submit(std::vector &vertices); + void Submit(std::vector &vertices); +}; + +#endif diff --git a/src/libs/Common_h/sprite_renderer.h b/src/libs/Common_h/sprite_renderer.h new file mode 100644 index 000000000..3806fc422 --- /dev/null +++ b/src/libs/Common_h/sprite_renderer.h @@ -0,0 +1,85 @@ +#ifndef SPRITE_RENDERER_DEF +#define SPRITE_RENDERER_DEF + +#include "bgfx_utils.h" + +#include "glm.hpp" + +#include +#include + + +struct SPRITE_VERTEX +{ + float x; + float y; + + // uint32_t color; + float tu, tv; + + static void Init() + { + sr_layout.begin() + .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float) + //.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) + .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float, true) + .end(); + }; + + static bgfx::VertexLayout sr_layout; +}; + +struct SpriteInfo +{ + std::vector vertices; + float depth; + std::shared_ptr texture; +}; + +static const uint32_t InitialQueueSize = 1024; + +class SpriteRenderer +{ + static const uint32_t MaxBatchSize = 2048; + + bgfx::UniformHandle s_texColor; + //bgfx::UniformHandle u_color; + bgfx::ProgramHandle m_prog; + + + long m_width; + long m_height; + + std::vector m_spriteQueue; + + std::vector m_sortedSprites; + + uint32_t m_spriteQueueSize; + uint32_t m_spriteQueueCount; + + std::vector m_backbufferMemory; + std::shared_ptr m_backbufferTexture; + + public: + std::shared_ptr Texture; + + SpriteRenderer(long m_fbWidth, long m_fbHeight); + //SpriteRenderer(); + + ~SpriteRenderer(); + std::shared_ptr GetBackBufferTexture(); + + void BlitBackBuffer(unsigned char *data); + + void SetBackbufferTexture(std::shared_ptr texture); + void ReleaseTexture(std::shared_ptr texture); + + void SetViewProjection(); + + void UpdateIndexBuffer(std::vector indices); + void PushVertices(std::vector vertices, float depth = 0.0f); + + void Submit(); +}; + +#endif diff --git a/src/libs/Common_h/vfile_service.h b/src/libs/Common_h/vfile_service.h index 247d1d98c..c14ee71e6 100644 --- a/src/libs/Common_h/vfile_service.h +++ b/src/libs/Common_h/vfile_service.h @@ -24,6 +24,10 @@ class VFILE_SERVICE virtual bool _WriteFile(std::fstream &fileS, const void *s, std::streamsize count) = 0; virtual bool _ReadFile(std::fstream &fileS, void *s, std::streamsize count) = 0; virtual bool _FileOrDirectoryExists(const char *p) = 0; + + virtual bool _OldReadFile(std::filesystem::path path, void *buffer, uint32_t bytes_to_read, uint32_t *bytes_read, + uint32_t seek_to = 0) = 0; + virtual std::vector _GetPathsOrFilenamesByMask(const char *sourcePath, const char *mask, bool getPaths, bool onlyDirs = false, bool onlyFiles = true) = 0; virtual std::vector _GetFsPathsByMask(const char *sourcePath, const char *mask, diff --git a/src/libs/Lighter/LGeometry.h b/src/libs/Lighter/LGeometry.h index a00343741..a0d4f2691 100644 --- a/src/libs/Lighter/LGeometry.h +++ b/src/libs/Lighter/LGeometry.h @@ -63,7 +63,7 @@ class LGeometry long numVrt; long maxVrt; - std::vector trg; + std::vector trg; long numTrg; long maxTrg; diff --git a/src/libs/Lighter/LTypes.h b/src/libs/Lighter/LTypes.h index cd9f17748..90c5550d4 100644 --- a/src/libs/Lighter/LTypes.h +++ b/src/libs/Lighter/LTypes.h @@ -77,7 +77,7 @@ struct Vertex long cindex; // Index in final buffer of object colors }; -struct Triangle +struct StormTriangle { CVECTOR n; // Normal float sq; // Area diff --git a/src/libs/Lighter/LightProcessor.cpp b/src/libs/Lighter/LightProcessor.cpp index e26d41a76..468d045eb 100644 --- a/src/libs/Lighter/LightProcessor.cpp +++ b/src/libs/Lighter/LightProcessor.cpp @@ -250,7 +250,7 @@ void LightProcessor::CalcShadows() } // Distribute shading from triangle to vertices -void LightProcessor::ApplyTriangleShadows(Triangle &t) +void LightProcessor::ApplyTriangleShadows(StormTriangle &t) { auto &ls = *lights; const auto num = ls.Num(); diff --git a/src/libs/Lighter/LightProcessor.h b/src/libs/Lighter/LightProcessor.h index 6b22f6684..fa34227de 100644 --- a/src/libs/Lighter/LightProcessor.h +++ b/src/libs/Lighter/LightProcessor.h @@ -44,7 +44,7 @@ class LightProcessor // Calculate lighting void CalcLights(long lit = -1, bool isCos = true, bool isAtt = true, bool isSdw = true); // Distribute shading from triangle to vertices - void ApplyTriangleShadows(Triangle &t); + void ApplyTriangleShadows(StormTriangle &t); private: LGeometry *geometry; diff --git a/src/libs/Pcs_controls/pcs_controls.cpp b/src/libs/Pcs_controls/pcs_controls.cpp index aaa39b840..e8f99eab4 100644 --- a/src/libs/Pcs_controls/pcs_controls.cpp +++ b/src/libs/Pcs_controls/pcs_controls.cpp @@ -23,6 +23,15 @@ PCS_CONTROLS::PCS_CONTROLS() nControlsNum = 0; + // nMouseXPrev = nMouseYPrev = 0; + /*RECT r; + GetWindowRect(core.GetAppHWND(), &r); + nMouseXPrev = r.left + (r.right - r.left) / 2; + nMouseYPrev = r.top + (r.bottom - r.top) / 2;*/ + + POINT p; + GetCursorPos(&p); + nLastControlTime = 0; nMouseWheel = 0; @@ -504,6 +513,14 @@ void PCS_CONTROLS::Update(uint32_t DeltaTime) nMouseWheel = 0; + RECT r; + GetWindowRect(core.GetAppHWND(), &r); + POINT p; + + GetCursorPos(&p); + + + //SetCursorPos(nMouseXPrev, nMouseYPrev); nLastControlTime += DeltaTime; } diff --git a/src/libs/Xinterface/Nodes/xi_2picture.cpp b/src/libs/Xinterface/Nodes/xi_2picture.cpp index 7e5204025..0f93a2b2e 100644 --- a/src/libs/Xinterface/Nodes/xi_2picture.cpp +++ b/src/libs/Xinterface/Nodes/xi_2picture.cpp @@ -4,6 +4,8 @@ #include "vfile_service.h" +#include "primitive_renderer.h" + void SetRectanglePos(XI_ONETEX_VERTEX v[4], const FXYPOINT ¢er, const FXYPOINT &size) { v[0].pos.x = v[1].pos.x = center.x - size.x / 2; @@ -44,10 +46,41 @@ void CXI_TWOPICTURE::Draw(bool bSelected, uint32_t Delta_Time) { if (m_bUse) { - m_rs->TextureSet(0, m_idOneTex); + /*m_rs->TextureSet(0, m_idOneTex); m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_vOne, sizeof(XI_ONETEX_VERTEX), "iIcon"); m_rs->TextureSet(0, m_idTwoTex); - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_vTwo, sizeof(XI_ONETEX_VERTEX), "iIcon"); + m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_vTwo, sizeof(XI_ONETEX_VERTEX), "iIcon");*/ + + auto texture = m_rs->GetBGFXTextureFromID(m_idOneTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + std::vector vertices; + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{m_vOne[0].pos.x, m_vOne[0].pos.y, m_vOne[0].pos.z, + m_vOne[0].tu, + m_vOne[0].tv, m_vOne[0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{m_vOne[2].pos.x, m_vOne[2].pos.y, m_vOne[2].pos.z, + m_vOne[2].tu, m_vOne[2].tv, m_vOne[2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{m_vOne[1].pos.x, m_vOne[1].pos.y, m_vOne[1].pos.z, + m_vOne[1].tu, m_vOne[1].tv, m_vOne[1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{m_vOne[3].pos.x, m_vOne[3].pos.y, m_vOne[3].pos.z, + m_vOne[3].tu, m_vOne[3].tv, m_vOne[3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + + vertices.clear(); + + texture = m_rs->GetBGFXTextureFromID(m_idTwoTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{m_vTwo[0].pos.x, m_vTwo[0].pos.y, m_vTwo[0].pos.z, + m_vTwo[0].tu, m_vTwo[0].tv, m_vTwo[0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{m_vTwo[2].pos.x, m_vTwo[2].pos.y, m_vTwo[2].pos.z, + m_vTwo[2].tu, m_vTwo[2].tv, m_vTwo[2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{m_vTwo[1].pos.x, m_vTwo[1].pos.y, m_vTwo[1].pos.z, + m_vTwo[1].tu, m_vTwo[1].tv, m_vTwo[1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{m_vTwo[3].pos.x, m_vTwo[3].pos.y, m_vTwo[3].pos.z, + m_vTwo[3].tu, m_vTwo[3].tv, m_vTwo[3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); } } @@ -80,12 +113,12 @@ void CXI_TWOPICTURE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co // textures if (ReadIniString(ini1, name1, ini2, name2, "oneTexName", param, sizeof(param), "")) - m_idOneTex = m_rs->TextureCreate(param); + m_idOneTex = m_rs->BGFXTextureCreate(param); else m_idOneTex = -1; if (ReadIniString(ini1, name1, ini2, name2, "twoTexName", param, sizeof(param), "")) - m_idTwoTex = m_rs->TextureCreate(param); + m_idTwoTex = m_rs->BGFXTextureCreate(param); else m_idTwoTex = -1; @@ -118,8 +151,8 @@ void CXI_TWOPICTURE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co void CXI_TWOPICTURE::ReleaseAll() { - TEXTURE_RELEASE(m_rs, m_idOneTex); - TEXTURE_RELEASE(m_rs, m_idTwoTex); + BGFX_TEXTURE_RELEASE(m_rs, m_idOneTex); + BGFX_TEXTURE_RELEASE(m_rs, m_idTwoTex); } int CXI_TWOPICTURE::CommandExecute(int wActCode) diff --git a/src/libs/Xinterface/Nodes/xi_VImgScroll.cpp b/src/libs/Xinterface/Nodes/xi_VImgScroll.cpp index 172ca67f3..3948ce0e8 100644 --- a/src/libs/Xinterface/Nodes/xi_VImgScroll.cpp +++ b/src/libs/Xinterface/Nodes/xi_VImgScroll.cpp @@ -2,6 +2,8 @@ #include "core.h" +#include "primitive_renderer.h" + #define MAXIMAGEQUANTITY 100 long GetTexFromEvent(VDATA *vdat); @@ -129,7 +131,7 @@ void CXI_VIMAGESCROLL::Draw(bool bSelected, uint32_t Delta_Time) FXYRECT textureRect; for (auto i = 0; i < 4; i++) pV[i].pos.z = 1.f; - pPictureService->GetTexturePos(m_nBorderPicture, textureRect); + pPictureService->BGFXGetTexturePos(m_nBorderPicture, textureRect); pV[0].tu = textureRect.left; pV[0].tv = textureRect.top; pV[1].tu = textureRect.left; @@ -150,9 +152,22 @@ void CXI_VIMAGESCROLL::Draw(bool bSelected, uint32_t Delta_Time) // show select border if (m_bShowBorder /*&& !m_bLockStatus*/ && m_nShowOrder < 0) { - m_rs->TextureSet(0, m_texBorder); + + auto texture = m_rs->GetBGFXTextureFromID(m_texBorder); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[0].pos.x, pV[0].pos.y, pV[0].pos.z, pV[0].tu, pV[0].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[2].pos.x, pV[2].pos.y, pV[2].pos.z, pV[2].tu, pV[2].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[1].pos.x, pV[1].pos.y, pV[1].pos.z, pV[1].tu, pV[1].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[3].pos.x, pV[3].pos.y, pV[3].pos.z, pV[3].tu, pV[3].tv}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + + /*m_rs->TextureSet(0, m_texBorder); m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONLYONETEX_FVF, 2, pV, sizeof(XI_ONLYONETEX_VERTEX), - "iScrollImages_border"); + "iScrollImages_border");*/ } XI_ONETEX_VERTEX v[4]; @@ -175,7 +190,8 @@ void CXI_VIMAGESCROLL::Draw(bool bSelected, uint32_t Delta_Time) if (m_Image[pScroll->imageNum].ptex[n] != -1) { - m_rs->TextureSet(0, m_Image[pScroll->imageNum].ptex[n]); + auto texture = m_rs->GetBGFXTextureFromID(m_Image[pScroll->imageNum].ptex[n]); + m_rs->GetPrimitiveRenderer()->Texture = texture; rectTex.left = 0.f; rectTex.top = 0.f; rectTex.right = 1.f; @@ -184,22 +200,27 @@ void CXI_VIMAGESCROLL::Draw(bool bSelected, uint32_t Delta_Time) else if (m_Image[pScroll->imageNum].img[n] != -1) { // get texture rectangle - pPictureService->GetTexturePos(m_Image[pScroll->imageNum].img[n], rectTex); - m_rs->TextureSet(0, m_nGroupTex[m_Image[pScroll->imageNum].tex[n]]); + pPictureService->BGFXGetTexturePos(m_Image[pScroll->imageNum].img[n], rectTex); + + auto texture = m_rs->GetBGFXTextureFromID(m_Image[pScroll->imageNum].tex[n]); + m_rs->GetPrimitiveRenderer()->Texture = texture; } else { if (m_idBadPic[n] != -1 && m_idBadTexture[n] != -1) // partial use of texture for a "bad" picture { - m_rs->TextureSet(0, m_nGroupTex[m_idBadTexture[n]]); - pPictureService->GetTexturePos(m_idBadPic[n], rectTex); + auto texture = m_rs->GetBGFXTextureFromID(m_nGroupTex[m_idBadTexture[n]]); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + pPictureService->BGFXGetTexturePos(m_idBadPic[n], rectTex); } else // "bad" picture for the whole texture { if (m_idBadTexture[n] != -1) { - m_rs->TextureSet(0, m_idBadTexture[n]); + auto texture = m_rs->GetBGFXTextureFromID(m_idBadTexture[n]); + m_rs->GetPrimitiveRenderer()->Texture = texture; rectTex.left = 0.f; rectTex.top = 0.f; rectTex.right = 1.f; @@ -243,13 +264,43 @@ void CXI_VIMAGESCROLL::Draw(bool bSelected, uint32_t Delta_Time) v[0].color = v[1].color = v[2].color = v[3].color = m_dwNormalColor[n]; if (m_Image[pScroll->imageNum].bUseSpecTechnique[n]) { - m_rs->SetRenderState(D3DRS_TEXTUREFACTOR, m_dwSpecTechniqueARGB); + /*m_rs->SetRenderState(D3DRS_TEXTUREFACTOR, m_dwSpecTechniqueARGB); m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, v, sizeof(XI_ONETEX_VERTEX), - m_sSpecTechniqueName); + m_sSpecTechniqueName);*/ + + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{v[0].pos.x, v[0].pos.y, v[0].pos.z, v[0].tu, + v[0].tv, v[0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{v[2].pos.x, v[2].pos.y, v[2].pos.z, v[2].tu, + v[2].tv, v[1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{v[1].pos.x, v[1].pos.y, v[1].pos.z, v[1].tu, + v[1].tv, v[2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{v[3].pos.x, v[3].pos.y, v[3].pos.z, v[3].tu, + v[3].tv, v[3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } else - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, v, sizeof(XI_ONETEX_VERTEX), - "iScrollImages_main"); + { + /* m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, v, sizeof(XI_ONETEX_VERTEX), + "iScrollImages_main");*/ + + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{v[0].pos.x, v[0].pos.y, v[0].pos.z, v[0].tu, + v[0].tv, v[0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{v[2].pos.x, v[2].pos.y, v[2].pos.z, v[2].tu, + v[2].tv, v[1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{v[1].pos.x, v[1].pos.y, v[1].pos.z, v[1].tu, + v[1].tv, v[2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{v[3].pos.x, v[3].pos.y, v[3].pos.z, v[3].tu, + v[3].tv, v[3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + + } pScroll->bCurNotUse = false; } else @@ -297,9 +348,22 @@ void CXI_VIMAGESCROLL::Draw(bool bSelected, uint32_t Delta_Time) if (m_bShowBorder && n == curShowOrder) { // show select border - m_rs->TextureSet(0, m_texBorder); + /*m_rs->TextureSet(0, m_texBorder); m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONLYONETEX_FVF, 2, pV, sizeof(XI_ONLYONETEX_VERTEX), - "iScrollImages_border"); + "iScrollImages_border");*/ + + auto texture = m_rs->GetBGFXTextureFromID(m_texBorder); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[0].pos.x, pV[0].pos.y, pV[0].pos.z, pV[0].tu, pV[0].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[2].pos.x, pV[2].pos.y, pV[2].pos.z, pV[2].tu, pV[2].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[1].pos.x, pV[1].pos.y, pV[1].pos.z, pV[1].tu, pV[1].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[3].pos.x, pV[3].pos.y, pV[3].pos.z, pV[3].tu, pV[3].tv}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + bDoShowBorder = true; } } @@ -307,9 +371,22 @@ void CXI_VIMAGESCROLL::Draw(bool bSelected, uint32_t Delta_Time) if (m_bShowBorder && !bDoShowBorder) { // show select border - m_rs->TextureSet(0, m_texBorder); + /*m_rs->TextureSet(0, m_texBorder); m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONLYONETEX_FVF, 2, pV, sizeof(XI_ONLYONETEX_VERTEX), - "iScrollImages_border"); + "iScrollImages_border");*/ + + auto texture = m_rs->GetBGFXTextureFromID(m_texBorder); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[0].pos.x, pV[0].pos.y, pV[0].pos.z, pV[0].tu, pV[0].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[2].pos.x, pV[2].pos.y, pV[2].pos.z, pV[2].tu, pV[2].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[1].pos.x, pV[1].pos.y, pV[1].pos.z, pV[1].tu, pV[1].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[3].pos.x, pV[3].pos.y, pV[3].pos.z, pV[3].tu, pV[3].tv}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } } } @@ -486,7 +563,7 @@ void CXI_VIMAGESCROLL::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, throw std::runtime_error("allocate memory error"); } memcpy(m_sGroupName[i], stmp, len); - m_nGroupTex[i] = pPictureService->GetTextureID(m_sGroupName[i]); + m_nGroupTex[i] = pPictureService->BGFXGetTextureID(m_sGroupName[i]); } } } @@ -507,7 +584,7 @@ void CXI_VIMAGESCROLL::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, sprintf_s(param, "BadPicture%d", n + 1); if ((sBadPict = pAttribute->GetAttribute(param)) != nullptr) { - m_idBadTexture[n] = m_rs->TextureCreate(sBadPict); + m_idBadTexture[n] = m_rs->BGFXTextureCreate(sBadPict); m_idBadPic[n] = -1; } else @@ -518,7 +595,7 @@ void CXI_VIMAGESCROLL::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, { sprintf_s(param, "BadPic%d", n + 1); m_idBadPic[n] = - pPictureService->GetImageNum(m_sGroupName[m_idBadTexture[n]], pAttribute->GetAttribute(param)); + pPictureService->BGFXGetImageNum(m_sGroupName[m_idBadTexture[n]], pAttribute->GetAttribute(param)); } else m_idBadPic[n] = -1; @@ -620,7 +697,7 @@ void CXI_VIMAGESCROLL::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, m_Image[i].tex[n] = pListEntity->GetAttributeAsDword(param, -1); sprintf_s(param, "img%d", n + 1); if (m_Image[i].tex[n] != -1) - m_Image[i].img[n] = pPictureService->GetImageNum(m_sGroupName[m_Image[i].tex[n]], + m_Image[i].img[n] = pPictureService->BGFXGetImageNum(m_sGroupName[m_Image[i].tex[n]], pListEntity->GetAttribute(param)); else m_Image[i].img[n] = -1; @@ -644,8 +721,8 @@ void CXI_VIMAGESCROLL::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, if ((m_sBorderGroupName = new char[len]) == nullptr) throw std::runtime_error("allocate memory error"); memcpy(m_sBorderGroupName, param1, len); - m_texBorder = pPictureService->GetTextureID(m_sBorderGroupName); - m_nBorderPicture = pPictureService->GetImageNum(m_sBorderGroupName, tmpstr); + m_texBorder = pPictureService->BGFXGetTextureID(m_sBorderGroupName); + m_nBorderPicture = pPictureService->BGFXGetImageNum(m_sBorderGroupName, tmpstr); m_bShowBorder = m_texBorder != -1; } else @@ -893,13 +970,13 @@ void CXI_VIMAGESCROLL::ReleaseAll() for (i = 0; i < m_nGroupQuantity; i++) { - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName[i], m_nGroupTex[i]); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName[i], m_nGroupTex[i]); STORM_DELETE(m_sGroupName[i]); } STORM_DELETE(m_sGroupName); STORM_DELETE(m_nGroupTex); - PICTURE_TEXTURE_RELEASE(pPictureService, m_sBorderGroupName, m_texBorder); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sBorderGroupName, m_texBorder); STORM_DELETE(m_sBorderGroupName); // release all strings parameters @@ -1106,7 +1183,7 @@ void CXI_VIMAGESCROLL::ChangeScroll(int nScrollItemNum) m_Image[i].tex[n] = pAttribute->GetAttributeAsDword(param, -1); sprintf_s(param, "img%d", n + 1); if (m_Image[i].tex[n] != -1) - m_Image[i].img[n] = pPictureService->GetImageNum(m_sGroupName[m_Image[i].tex[n]], + m_Image[i].img[n] = pPictureService->BGFXGetImageNum(m_sGroupName[m_Image[i].tex[n]], pAttribute->GetAttribute(param)); else m_Image[i].img[n] = -1; @@ -1190,7 +1267,7 @@ void CXI_VIMAGESCROLL::RefreshScroll() // release old groups for (i = 0; i < m_nGroupQuantity; i++) { - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName[i], m_nGroupTex[i]); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName[i], m_nGroupTex[i]); STORM_DELETE(m_sGroupName[i]); } STORM_DELETE(m_sGroupName); @@ -1262,7 +1339,7 @@ void CXI_VIMAGESCROLL::RefreshScroll() throw std::runtime_error("allocate memory error"); } memcpy(m_sGroupName[i], stmp, len); - m_nGroupTex[i] = pPictureService->GetTextureID(m_sGroupName[i]); + m_nGroupTex[i] = pPictureService->BGFXGetTextureID(m_sGroupName[i]); } } } @@ -1285,7 +1362,7 @@ void CXI_VIMAGESCROLL::RefreshScroll() { sprintf_s(param, "BadPic%d", n + 1); m_idBadPic[n] = - pPictureService->GetImageNum(m_sGroupName[m_idBadTexture[n]], pAttribute->GetAttribute(param)); + pPictureService->BGFXGetImageNum(m_sGroupName[m_idBadTexture[n]], pAttribute->GetAttribute(param)); } else m_idBadPic[n] = -1; @@ -1387,7 +1464,7 @@ void CXI_VIMAGESCROLL::RefreshScroll() m_Image[i].tex[n] = pListEntity->GetAttributeAsDword(param, -1); sprintf_s(param, "img%d", n + 1); if (m_Image[i].tex[n] != -1) - m_Image[i].img[n] = pPictureService->GetImageNum(m_sGroupName[m_Image[i].tex[n]], + m_Image[i].img[n] = pPictureService->BGFXGetImageNum(m_sGroupName[m_Image[i].tex[n]], pListEntity->GetAttribute(param)); else m_Image[i].img[n] = -1; @@ -1619,14 +1696,14 @@ void CXI_VIMAGESCROLL::UpdateTexturesGroup() throw std::runtime_error("allocate memory error"); } memcpy(m_sGroupName[i], stmp, len); - m_nGroupTex[i] = pPictureService->GetTextureID(m_sGroupName[i]); + m_nGroupTex[i] = pPictureService->BGFXGetTextureID(m_sGroupName[i]); } } // delete old groups for (i = 0; i < nPrevQ; i++) { - PICTURE_TEXTURE_RELEASE(pPictureService, pPrevGroup[i], prevTex[i]); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, pPrevGroup[i], prevTex[i]); STORM_DELETE(pPrevGroup[i]); } STORM_DELETE(pPrevGroup); diff --git a/src/libs/Xinterface/Nodes/xi_border.cpp b/src/libs/Xinterface/Nodes/xi_border.cpp index cedb4a6ac..9f43618a0 100644 --- a/src/libs/Xinterface/Nodes/xi_border.cpp +++ b/src/libs/Xinterface/Nodes/xi_border.cpp @@ -5,6 +5,8 @@ #include "vfile_service.h" #include "xi_image.h" +#include "primitive_renderer.h" + CXI_BORDER::CXI_BORDER() { m_rs = nullptr; @@ -41,10 +43,28 @@ void CXI_BORDER::Draw(bool bSelected, uint32_t Delta_Time) if (m_idTex >= 0) { - m_rs->TextureSet(0, m_idTex); - if (m_idVBuf >= 0 && m_idIBuf >= 0) - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 0, m_nSquareQ * 4, 0, m_nSquareQ * 2, - "iBounder"); + auto texture = m_rs->GetBGFXTextureFromID(m_idTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + for (long n = 0; n < pV.size(); n += 4) + { + auto& pVertices = pV; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[n + 0].pos.x, pVertices[n + 0].pos.y, + pVertices[n + 0].pos.z, pVertices[n + 0].tu, + pVertices[n + 0].tv, pVertices[n + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[n + 2].pos.x, pVertices[n + 2].pos.y, + pVertices[n + 2].pos.z, pVertices[n + 2].tu, + pVertices[n + 2].tv, pVertices[n + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[n + 1].pos.x, pVertices[n + 1].pos.y, + pVertices[n + 1].pos.z, pVertices[n + 1].tu, + pVertices[n + 1].tv, pVertices[n + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[n + 3].pos.x, pVertices[n + 3].pos.y, + pVertices[n + 3].pos.z, pVertices[n + 3].tu, + pVertices[n + 3].tv, pVertices[n + 3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } } } } @@ -60,7 +80,7 @@ bool CXI_BORDER::Init(INIFILE *ini1, const char *name1, INIFILE *ini2, const cha void CXI_BORDER::ReleaseAll() { m_bUse = false; - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName.c_str(), m_idTex); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName.c_str(), m_idTex); VERTEX_BUFFER_RELEASE(m_rs, m_idVBuf); INDEX_BUFFER_RELEASE(m_rs, m_idIBuf); STORM_DELETE(m_pBackImage); @@ -142,7 +162,7 @@ void CXI_BORDER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const if (ReadIniString(ini1, name1, ini2, name2, "groupName", param, sizeof(param), "")) { m_sGroupName = param; - m_idTex = pPictureService->GetTextureID(m_sGroupName.c_str()); + m_idTex = pPictureService->BGFXGetTextureID(m_sGroupName.c_str()); } // create index and vertex buffers @@ -156,85 +176,85 @@ void CXI_BORDER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const // left top corner m_nLeftTopPicture = -1; if (ReadIniString(ini1, name1, ini2, name2, "lefttop_pic", param, sizeof(param), "")) - m_nLeftTopPicture = pPictureService->GetImageNum(m_sGroupName.c_str(), param); + m_nLeftTopPicture = pPictureService->BGFXGetImageNum(m_sGroupName.c_str(), param); if (m_nLeftTopPicture < 0) { m_frLeftTopUV.left = m_frLeftTopUV.top = 0.f; m_frLeftTopUV.right = m_frLeftTopUV.bottom = 1.f; } else - pPictureService->GetTexturePos(m_nLeftTopPicture, m_frLeftTopUV); + pPictureService->BGFXGetTexturePos(m_nLeftTopPicture, m_frLeftTopUV); m_pntLeftTopSize = GetIniLongPoint(ini1, name1, ini2, name2, "lefttop_size", XYPOINT(9, 9)); // right top corner m_nRightTopPicture = -1; if (ReadIniString(ini1, name1, ini2, name2, "righttop_pic", param, sizeof(param), "")) - m_nRightTopPicture = pPictureService->GetImageNum(m_sGroupName.c_str(), param); + m_nRightTopPicture = pPictureService->BGFXGetImageNum(m_sGroupName.c_str(), param); if (m_nRightTopPicture < 0) { m_frRightTopUV.left = m_frRightTopUV.top = 0.f; m_frRightTopUV.right = m_frRightTopUV.bottom = 1.f; } else - pPictureService->GetTexturePos(m_nRightTopPicture, m_frRightTopUV); + pPictureService->BGFXGetTexturePos(m_nRightTopPicture, m_frRightTopUV); m_pntRightTopSize = GetIniLongPoint(ini1, name1, ini2, name2, "righttop_size", XYPOINT(9, 9)); // left bottom corner m_nLeftBottomPicture = -1; if (ReadIniString(ini1, name1, ini2, name2, "leftbottom_pic", param, sizeof(param), "")) - m_nLeftBottomPicture = pPictureService->GetImageNum(m_sGroupName.c_str(), param); + m_nLeftBottomPicture = pPictureService->BGFXGetImageNum(m_sGroupName.c_str(), param); if (m_nLeftBottomPicture < 0) { m_frLeftBottomUV.left = m_frLeftBottomUV.top = 0.f; m_frLeftBottomUV.right = m_frLeftBottomUV.bottom = 1.f; } else - pPictureService->GetTexturePos(m_nLeftBottomPicture, m_frLeftBottomUV); + pPictureService->BGFXGetTexturePos(m_nLeftBottomPicture, m_frLeftBottomUV); m_pntLeftBottomSize = GetIniLongPoint(ini1, name1, ini2, name2, "leftbottom_size", XYPOINT(9, 9)); // left bottom corner m_nRightBottomPicture = -1; if (ReadIniString(ini1, name1, ini2, name2, "rightbottom_pic", param, sizeof(param), "")) - m_nRightBottomPicture = pPictureService->GetImageNum(m_sGroupName.c_str(), param); + m_nRightBottomPicture = pPictureService->BGFXGetImageNum(m_sGroupName.c_str(), param); if (m_nRightBottomPicture < 0) { m_frRightBottomUV.left = m_frRightBottomUV.top = 0.f; m_frRightBottomUV.right = m_frRightBottomUV.bottom = 1.f; } else - pPictureService->GetTexturePos(m_nRightBottomPicture, m_frRightBottomUV); + pPictureService->BGFXGetTexturePos(m_nRightBottomPicture, m_frRightBottomUV); m_pntRightBottomSize = GetIniLongPoint(ini1, name1, ini2, name2, "rightbottom_size", XYPOINT(9, 9)); // top edge m_nTopLinePicture = -1; if (ReadIniString(ini1, name1, ini2, name2, "topline_pic", param, sizeof(param), "")) - m_nTopLinePicture = pPictureService->GetImageNum(m_sGroupName.c_str(), param); + m_nTopLinePicture = pPictureService->BGFXGetImageNum(m_sGroupName.c_str(), param); if (m_nTopLinePicture < 0) { m_frTopLineUV.left = m_frTopLineUV.top = 0.f; m_frTopLineUV.right = m_frTopLineUV.bottom = 1.f; } else - pPictureService->GetTexturePos(m_nTopLinePicture, m_frTopLineUV); + pPictureService->BGFXGetTexturePos(m_nTopLinePicture, m_frTopLineUV); m_nTopLineHeight = GetIniLong(ini1, name1, ini2, name2, "topline_height", 5); // bottom edge m_nBottomLinePicture = -1; if (ReadIniString(ini1, name1, ini2, name2, "bottomline_pic", param, sizeof(param), "")) - m_nBottomLinePicture = pPictureService->GetImageNum(m_sGroupName.c_str(), param); + m_nBottomLinePicture = pPictureService->BGFXGetImageNum(m_sGroupName.c_str(), param); if (m_nBottomLinePicture < 0) { m_frBottomLineUV.left = m_frBottomLineUV.top = 0.f; m_frBottomLineUV.right = m_frBottomLineUV.bottom = 1.f; } else - pPictureService->GetTexturePos(m_nBottomLinePicture, m_frBottomLineUV); + pPictureService->BGFXGetTexturePos(m_nBottomLinePicture, m_frBottomLineUV); m_nBottomLineHeight = GetIniLong(ini1, name1, ini2, name2, "bottomline_height", 5); // left edge m_nLeftLinePicture = -1; if (ReadIniString(ini1, name1, ini2, name2, "leftline_pic", param, sizeof(param), "")) - m_nLeftLinePicture = pPictureService->GetImageNum(m_sGroupName.c_str(), param); + m_nLeftLinePicture = pPictureService->BGFXGetImageNum(m_sGroupName.c_str(), param); if (m_nLeftLinePicture < 0) { m_frLeftLineUV.left = m_frLeftLineUV.top = 0.f; @@ -247,14 +267,14 @@ void CXI_BORDER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const // right edge m_nRightLinePicture = -1; if (ReadIniString(ini1, name1, ini2, name2, "rightline_pic", param, sizeof(param), "")) - m_nRightLinePicture = pPictureService->GetImageNum(m_sGroupName.c_str(), param); + m_nRightLinePicture = pPictureService->BGFXGetImageNum(m_sGroupName.c_str(), param); if (m_nRightLinePicture < 0) { m_frRightLineUV.left = m_frRightLineUV.top = 0.f; m_frRightLineUV.right = m_frRightLineUV.bottom = 1.f; } else - pPictureService->GetTexturePos(m_nRightLinePicture, m_frRightLineUV); + pPictureService->BGFXGetTexturePos(m_nRightLinePicture, m_frRightLineUV); m_nRightLineWidth = GetIniLong(ini1, name1, ini2, name2, "rightline_width", 5); if (m_pCaptionImage && m_nCaptionHeight > 0) @@ -299,6 +319,9 @@ void CXI_BORDER::FillVertexBuffers() { if (m_idVBuf < 0) return; + + //@BGFX TODO implement batched vertex submission + /* auto *pV = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); for (long n = 0; n < m_nSquareQ * 4; n++) @@ -342,38 +365,87 @@ void CXI_BORDER::FillVertexBuffers() } m_rs->UnLockVertexBuffer(m_idVBuf); -} -void CXI_BORDER::WriteVertexForSquare(XI_ONETEX_VERTEX *pV, FXYRECT &UVRect, uint32_t dwColor, long left, long top, + */ + + pV.clear(); + + pV.reserve(32); + pV.resize(32); + + // top line + WriteVertexForSquare(0, m_frTopLineUV, m_dwColor, m_rect.left + m_pntLeftTopSize.x, m_rect.top, + m_rect.right - m_pntRightTopSize.x, m_rect.top + m_nTopLineHeight); + // bottom line + WriteVertexForSquare(4, m_frBottomLineUV, m_dwColor, m_rect.left + m_pntLeftTopSize.x, + m_rect.bottom - m_nBottomLineHeight, m_rect.right - m_pntRightTopSize.x, m_rect.bottom); + // left line + WriteVertexForSquare(8, m_frLeftLineUV, m_dwColor, m_rect.left, m_rect.top + m_pntLeftTopSize.y, + m_rect.left + m_nLeftLineWidth, m_rect.bottom - m_pntLeftBottomSize.y); + // right line + WriteVertexForSquare(12, m_frRightLineUV, m_dwColor, m_rect.right - m_nRightLineWidth, + m_rect.top + m_pntRightTopSize.y, m_rect.right, m_rect.bottom - m_pntRightBottomSize.y); + // left top corner + WriteVertexForSquare(16, m_frLeftTopUV, m_dwColor, m_rect.left, m_rect.top, m_rect.left + m_pntLeftTopSize.x, + m_rect.top + m_pntLeftTopSize.y); + // right top corner + WriteVertexForSquare(20, m_frRightTopUV, m_dwColor, m_rect.right - m_pntRightTopSize.x, m_rect.top, + m_rect.right, m_rect.top + m_pntRightTopSize.y); + // left bottom corner + WriteVertexForSquare(24, m_frLeftBottomUV, m_dwColor, m_rect.left, m_rect.bottom - m_pntLeftBottomSize.y, + m_rect.left + m_pntLeftBottomSize.x, m_rect.bottom); + // right bottom corner + WriteVertexForSquare(28, m_frRightBottomUV, m_dwColor, m_rect.right - m_pntRightBottomSize.x, + m_rect.bottom - m_pntRightBottomSize.y, m_rect.right, m_rect.bottom); + + // caption + if (m_nCaptionHeight > 0 && m_pCaptionImage) + { + pV.push_back({}); + pV.push_back({}); + pV.push_back({}); + pV.push_back({}); + + // caption line + WriteVertexForSquare(32, m_frTopLineUV, m_dwColor, m_rect.left + m_nLeftLineWidth, + m_rect.top + m_nCaptionHeight, m_rect.right - m_nRightLineWidth, + m_rect.top + m_nCaptionHeight + m_mCaptionDividerHeight); + } + + } + +/*void CXI_BORDER::WriteVertexForSquare(XI_ONETEX_VERTEX *pV, FXYRECT &UVRect, uint32_t dwColor, long left, long top, + long right, long bottom)*/ +void CXI_BORDER::WriteVertexForSquare(uint32_t index, FXYRECT &UVRect, uint32_t dwColor, long left, long top, long right, long bottom) { - pV[0].color = dwColor; - pV[0].pos.x = static_cast(left); - pV[0].pos.y = static_cast(top); - pV[0].pos.z = 1.f; - pV[0].tu = UVRect.left; - pV[0].tv = UVRect.top; - - pV[1].color = dwColor; - pV[1].pos.x = static_cast(left); - pV[1].pos.y = static_cast(bottom); - pV[1].pos.z = 1.f; - pV[1].tu = UVRect.left; - pV[1].tv = UVRect.bottom; - - pV[2].color = dwColor; - pV[2].pos.x = static_cast(right); - pV[2].pos.y = static_cast(top); - pV[2].pos.z = 1.f; - pV[2].tu = UVRect.right; - pV[2].tv = UVRect.top; - - pV[3].color = dwColor; - pV[3].pos.x = static_cast(right); - pV[3].pos.y = static_cast(bottom); - pV[3].pos.z = 1.f; - pV[3].tu = UVRect.right; - pV[3].tv = UVRect.bottom; + pV[index + 0].color = dwColor; + pV[index + 0].pos.x = static_cast(left); + pV[index + 0].pos.y = static_cast(top); + pV[index + 0].pos.z = 1.f; + pV[index + 0].tu = UVRect.left; + pV[index + 0].tv = UVRect.top; + + pV[index + 1].color = dwColor; + pV[index + 1].pos.x = static_cast(left); + pV[index + 1].pos.y = static_cast(bottom); + pV[index + 1].pos.z = 1.f; + pV[index + 1].tu = UVRect.left; + pV[index + 1].tv = UVRect.bottom; + + pV[index + 2].color = dwColor; + pV[index + 2].pos.x = static_cast(right); + pV[index + 2].pos.y = static_cast(top); + pV[index + 2].pos.z = 1.f; + pV[index + 2].tu = UVRect.right; + pV[index + 2].tv = UVRect.top; + + pV[index + 3].color = dwColor; + pV[index + 3].pos.x = static_cast(right); + pV[index + 3].pos.y = static_cast(bottom); + pV[index + 3].pos.z = 1.f; + pV[index + 3].tu = UVRect.right; + pV[index + 3].tv = UVRect.bottom; } uint32_t CXI_BORDER::MessageProc(long msgcode, MESSAGE &message) diff --git a/src/libs/Xinterface/Nodes/xi_border.h b/src/libs/Xinterface/Nodes/xi_border.h index 396d29b4d..d54575f57 100644 --- a/src/libs/Xinterface/Nodes/xi_border.h +++ b/src/libs/Xinterface/Nodes/xi_border.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "../inode.h" class CXI_IMAGE; @@ -30,10 +32,14 @@ class CXI_BORDER : public CINODE void LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const char *name2) override; void FillIndexBuffers() const; void FillVertexBuffers(); - void WriteVertexForSquare(XI_ONETEX_VERTEX *pV, FXYRECT &UVRect, uint32_t dwColor, long left, long top, long right, + /*void WriteVertexForSquare(XI_ONETEX_VERTEX *pV, FXYRECT &UVRect, uint32_t dwColor, long left, long top, long right, + long bottom);*/ + void WriteVertexForSquare(uint32_t index, FXYRECT &UVRect, uint32_t dwColor, long left, long top, long right, long bottom); protected: + + std::vector pV; // back picture CXI_IMAGE *m_pBackImage; diff --git a/src/libs/Xinterface/Nodes/xi_bounder.cpp b/src/libs/Xinterface/Nodes/xi_bounder.cpp index 83570246c..895ba9de5 100644 --- a/src/libs/Xinterface/Nodes/xi_bounder.cpp +++ b/src/libs/Xinterface/Nodes/xi_bounder.cpp @@ -5,6 +5,9 @@ #include "vfile_service.h" +#include "primitive_renderer.h" + + CXI_BOUNDER::CXI_BOUNDER() { m_rs = nullptr; @@ -12,10 +15,7 @@ CXI_BOUNDER::CXI_BOUNDER() m_idTex = -1L; m_sGroupName = nullptr; - m_idVBuf = -1L; - m_idIBuf = -1L; m_nVert = 0; - m_nIndx = 0; m_nNodeType = NODETYPE_BOUNDER; } @@ -33,8 +33,31 @@ void CXI_BOUNDER::Draw(bool bSelected, uint32_t Delta_Time) { if (m_bUse) { - m_rs->TextureSet(0, m_idTex); - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 0, m_nVert, 0, m_nIndx, "iBounder"); + + auto texture = m_rs->GetBGFXTextureFromID(m_idTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + + for (long n = 0; n < pVert.size(); n += 4) + { + auto& pVertices = pVert; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[n + 0].pos.x, pVertices[n + 0].pos.y, + pVertices[n + 0].pos.z, pVertices[n + 0].tu, + pVertices[n + 0].tv, pVertices[n + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[n + 2].pos.x, pVertices[n + 2].pos.y, + pVertices[n + 2].pos.z, pVertices[n + 2].tu, + pVertices[n + 2].tv, pVertices[n + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[n + 1].pos.x, pVertices[n + 1].pos.y, + pVertices[n + 1].pos.z, pVertices[n + 1].tu, + pVertices[n + 1].tv, pVertices[n + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[n + 3].pos.x, pVertices[n + 3].pos.y, + pVertices[n + 3].pos.z, pVertices[n + 3].tu, + pVertices[n + 3].tv, pVertices[n + 3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + + } } } @@ -50,11 +73,8 @@ void CXI_BOUNDER::ReleaseAll() { m_bUse = false; - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); STORM_DELETE(m_sGroupName); - - VERTEX_BUFFER_RELEASE(m_rs, m_idVBuf); - INDEX_BUFFER_RELEASE(m_rs, m_idIBuf); } void CXI_BOUNDER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const char *name2) @@ -74,7 +94,7 @@ void CXI_BOUNDER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const const auto len = strlen(param) + 1; m_sGroupName = new char[len]; memcpy(m_sGroupName, param, len); - m_idTex = pPictureService->GetTextureID(m_sGroupName); + m_idTex = pPictureService->BGFXGetTextureID(m_sGroupName); } // get pictures name @@ -84,16 +104,16 @@ void CXI_BOUNDER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const { tmpstr = param; tmpstr = GetSubStr(tmpstr, param2, sizeof(param2) - 1); - m_idAngle = pPictureService->GetImageNum(m_sGroupName, param2); - m_idHorzLine = pPictureService->GetImageNum(m_sGroupName, tmpstr); + m_idAngle = pPictureService->BGFXGetImageNum(m_sGroupName, param2); + m_idHorzLine = pPictureService->BGFXGetImageNum(m_sGroupName, tmpstr); } // get picture width & height XYRECT rectTmp; - pPictureService->GetTexturePos(m_idAngle, rectTmp); + pPictureService->BGFXGetTexturePos(m_idAngle, rectTmp); m_fAngleWidth = static_cast(rectTmp.right - rectTmp.left + 1); m_fAngleHeight = static_cast(rectTmp.bottom - rectTmp.top + 1); - pPictureService->GetTexturePos(m_idHorzLine, rectTmp); + pPictureService->BGFXGetTexturePos(m_idHorzLine, rectTmp); auto fLineWidth = static_cast(rectTmp.right - rectTmp.left + 1); auto fBoxWidth = static_cast(m_rect.right - m_rect.left) - m_fAngleWidth * 2.f; @@ -111,15 +131,10 @@ void CXI_BOUNDER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const // create index and vertex buffers m_nVert = 4 * 4 + m_nHorzLineQuantity * 4 * 2 + m_nVertLineQuantity * 4 * 2; - m_nIndx = 2 * 4 + 2 * m_nHorzLineQuantity * 2 + 2 * m_nVertLineQuantity * 2; - m_idVBuf = m_rs->CreateVertexBuffer(XI_ONETEX_FVF, m_nVert * sizeof(XI_ONETEX_VERTEX), D3DUSAGE_WRITEONLY); - m_idIBuf = m_rs->CreateIndexBuffer(m_nIndx * 3 * 2); - // Fill buffers - auto *pVert = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); - auto *pIndx = static_cast(m_rs->LockIndexBuffer(m_idIBuf)); - if (pVert == nullptr || pIndx == nullptr) - throw std::runtime_error("can not create the index&vertex buffers"); + pVert.resize(m_nVert); + + // Fill vertices for (i = 0; i < m_nVert; i++) { @@ -134,13 +149,6 @@ void CXI_BOUNDER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const FXYRECT fRectTmp; for (i = 0; i < 4; i++) { - pIndx[inum++] = vnum; - pIndx[inum++] = vnum + 1; - pIndx[inum++] = vnum + 2; - pIndx[inum++] = vnum + 2; - pIndx[inum++] = vnum + 1; - pIndx[inum++] = vnum + 3; - switch (i) { case 0: @@ -148,28 +156,29 @@ void CXI_BOUNDER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const fRectTmp.right = static_cast(m_rect.left) + m_fAngleWidth; fRectTmp.top = static_cast(m_rect.top); fRectTmp.bottom = static_cast(m_rect.top) + m_fAngleHeight; - pPictureService->GetTexturePos(m_idAngle, texRectTmp); + pPictureService->BGFXGetTexturePos(m_idAngle, texRectTmp); break; case 1: fRectTmp.left = static_cast(m_rect.right) - m_fAngleWidth; fRectTmp.right = static_cast(m_rect.right); fRectTmp.top = static_cast(m_rect.top); fRectTmp.bottom = static_cast(m_rect.top) + m_fAngleHeight; - pPictureService->GetTexturePos(TEXTURE_MODIFY_HORZFLIP, m_idAngle, texRectTmp); + pPictureService->BGFXGetTexturePos(TEXTURE_MODIFY_HORZFLIP, m_idAngle, texRectTmp); break; case 2: fRectTmp.left = static_cast(m_rect.left); fRectTmp.right = static_cast(m_rect.left) + m_fAngleWidth; fRectTmp.top = static_cast(m_rect.bottom) - m_fAngleHeight; fRectTmp.bottom = static_cast(m_rect.bottom); - pPictureService->GetTexturePos(TEXTURE_MODIFY_VERTFLIP, m_idAngle, texRectTmp); + pPictureService->BGFXGetTexturePos(TEXTURE_MODIFY_VERTFLIP, m_idAngle, texRectTmp); break; case 3: fRectTmp.left = static_cast(m_rect.right) - m_fAngleWidth; fRectTmp.right = static_cast(m_rect.right); fRectTmp.top = static_cast(m_rect.bottom) - m_fAngleHeight; fRectTmp.bottom = static_cast(m_rect.bottom); - pPictureService->GetTexturePos(TEXTURE_MODIFY_HORZFLIP | TEXTURE_MODIFY_VERTFLIP, m_idAngle, texRectTmp); + pPictureService->BGFXGetTexturePos(TEXTURE_MODIFY_HORZFLIP | TEXTURE_MODIFY_VERTFLIP, m_idAngle, + texRectTmp); break; } @@ -196,8 +205,8 @@ void CXI_BOUNDER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const // fill horizontal & vertical line rectangles FXYRECT tmpRect1, tmpRect2; - pPictureService->GetTexturePos(m_idHorzLine, tmpRect1); - pPictureService->GetTexturePos(TEXTURE_MODIFY_VERTFLIP, m_idHorzLine, tmpRect2); + pPictureService->BGFXGetTexturePos(m_idHorzLine, tmpRect1); + pPictureService->BGFXGetTexturePos(TEXTURE_MODIFY_VERTFLIP, m_idHorzLine, tmpRect2); auto fXTop = m_rect.left + m_fAngleWidth; auto fYTop = m_rect.bottom - m_fAngleHeight; auto lineType = 0; // top horizontal line @@ -219,13 +228,6 @@ void CXI_BOUNDER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const fYTop = m_rect.bottom - m_fAngleHeight; } - pIndx[inum++] = vnum; - pIndx[inum++] = vnum + 1; - pIndx[inum++] = vnum + 2; - pIndx[inum++] = vnum + 2; - pIndx[inum++] = vnum + 1; - pIndx[inum++] = vnum + 3; - // if (lineType == 0 || lineType == 2) { @@ -312,9 +314,6 @@ void CXI_BOUNDER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const fXTop += fHorzLineWidth; fYTop -= fVertLineWidth; } - - m_rs->UnLockVertexBuffer(m_idVBuf); - m_rs->UnLockIndexBuffer(m_idIBuf); } bool CXI_BOUNDER::IsClick(int buttonID, long xPos, long yPos) @@ -324,12 +323,6 @@ bool CXI_BOUNDER::IsClick(int buttonID, long xPos, long yPos) void CXI_BOUNDER::ChangePosition(XYRECT &rNewPos) { - // Fill buffers - auto *pVert = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); - - if (pVert == nullptr) - throw std::runtime_error("can not create the index&vertex buffers"); - long i; m_rect = rNewPos; @@ -359,28 +352,29 @@ void CXI_BOUNDER::ChangePosition(XYRECT &rNewPos) fRectTmp.right = static_cast(m_rect.left) + m_fAngleWidth; fRectTmp.top = static_cast(m_rect.top); fRectTmp.bottom = static_cast(m_rect.top) + m_fAngleHeight; - pPictureService->GetTexturePos(m_idAngle, texRectTmp); + pPictureService->BGFXGetTexturePos(m_idAngle, texRectTmp); break; case 1: fRectTmp.left = static_cast(m_rect.right) - m_fAngleWidth; fRectTmp.right = static_cast(m_rect.right); fRectTmp.top = static_cast(m_rect.top); fRectTmp.bottom = static_cast(m_rect.top) + m_fAngleHeight; - pPictureService->GetTexturePos(TEXTURE_MODIFY_HORZFLIP, m_idAngle, texRectTmp); + pPictureService->BGFXGetTexturePos(TEXTURE_MODIFY_HORZFLIP, m_idAngle, texRectTmp); break; case 2: fRectTmp.left = static_cast(m_rect.left); fRectTmp.right = static_cast(m_rect.left) + m_fAngleWidth; fRectTmp.top = static_cast(m_rect.bottom) - m_fAngleHeight; fRectTmp.bottom = static_cast(m_rect.bottom); - pPictureService->GetTexturePos(TEXTURE_MODIFY_VERTFLIP, m_idAngle, texRectTmp); + pPictureService->BGFXGetTexturePos(TEXTURE_MODIFY_VERTFLIP, m_idAngle, texRectTmp); break; case 3: fRectTmp.left = static_cast(m_rect.right) - m_fAngleWidth; fRectTmp.right = static_cast(m_rect.right); fRectTmp.top = static_cast(m_rect.bottom) - m_fAngleHeight; fRectTmp.bottom = static_cast(m_rect.bottom); - pPictureService->GetTexturePos(TEXTURE_MODIFY_HORZFLIP | TEXTURE_MODIFY_VERTFLIP, m_idAngle, texRectTmp); + pPictureService->BGFXGetTexturePos(TEXTURE_MODIFY_HORZFLIP | TEXTURE_MODIFY_VERTFLIP, m_idAngle, + texRectTmp); break; } @@ -407,8 +401,8 @@ void CXI_BOUNDER::ChangePosition(XYRECT &rNewPos) // fill horizontal & vertical line rectangles FXYRECT tmpRect1, tmpRect2; - pPictureService->GetTexturePos(m_idHorzLine, tmpRect1); - pPictureService->GetTexturePos(TEXTURE_MODIFY_VERTFLIP, m_idHorzLine, tmpRect2); + pPictureService->BGFXGetTexturePos(m_idHorzLine, tmpRect1); + pPictureService->BGFXGetTexturePos(TEXTURE_MODIFY_VERTFLIP, m_idHorzLine, tmpRect2); auto fXTop = m_rect.left + m_fAngleWidth; auto fYTop = m_rect.bottom - m_fAngleHeight; auto lineType = 0; // top horizontal line @@ -516,8 +510,6 @@ void CXI_BOUNDER::ChangePosition(XYRECT &rNewPos) fXTop += fHorzLineWidth; fYTop -= fVertLineWidth; } - - m_rs->UnLockVertexBuffer(m_idVBuf); } void CXI_BOUNDER::SaveParametersToIni() diff --git a/src/libs/Xinterface/Nodes/xi_bounder.h b/src/libs/Xinterface/Nodes/xi_bounder.h index 1433fee2e..4e341bf55 100644 --- a/src/libs/Xinterface/Nodes/xi_bounder.h +++ b/src/libs/Xinterface/Nodes/xi_bounder.h @@ -31,11 +31,12 @@ class CXI_BOUNDER : public CINODE long m_idTex; uint32_t m_dwColor; - // picture index & vertex buffers - long m_idVBuf; // identificator of the vertex buffer - long m_idIBuf; // identificator of the index buffer - long m_nVert; // vertex quantity - long m_nIndx; // index quantity + // picture vertices + long m_nVert; // vertex quantity + + std::vector pVert; + + float m_fAngleWidth; float m_fAngleHeight; diff --git a/src/libs/Xinterface/Nodes/xi_button.cpp b/src/libs/Xinterface/Nodes/xi_button.cpp index 1e5dc9255..752d1fcce 100644 --- a/src/libs/Xinterface/Nodes/xi_button.cpp +++ b/src/libs/Xinterface/Nodes/xi_button.cpp @@ -1,5 +1,8 @@ #include "xi_button.h" +#include "primitive_renderer.h" + + CXI_BUTTON::CXI_BUTTON() { m_rs = nullptr; @@ -99,14 +102,24 @@ void CXI_BUTTON::Draw(bool bSelected, uint32_t Delta_Time) } } - if (m_idTex != -1) + /*if (m_idTex != -1) m_rs->TextureSet(0, m_idTex); else - m_rs->SetTexture(0, m_pTex ? m_pTex->m_pTexture : nullptr); + m_rs->SetTexture(0, m_pTex ? m_pTex->m_pTexture : nullptr);*/ //@BGFX TODO implement DirectX textures + + bool textureSet = false; + if (m_idTex != -1) + { + auto texture = m_rs->GetBGFXTextureFromID(m_idTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + textureSet = true; + } + if (m_idTex >= 0 || m_pTex != nullptr) { - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, vShadow, sizeof(XI_ONETEX_VERTEX), "iShadow"); + /*m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, vShadow, sizeof(XI_ONETEX_VERTEX), "iShadow"); if (m_bClickable && m_bSelected) m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, vFace, sizeof(XI_ONETEX_VERTEX), "iButton"); @@ -115,7 +128,77 @@ void CXI_BUTTON::Draw(bool bSelected, uint32_t Delta_Time) m_rs->SetRenderState(D3DRS_TEXTUREFACTOR, m_argbDisableColor); m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, vFace, sizeof(XI_ONETEX_VERTEX), "iDisabledNode"); + }*/ + + if (m_bClickable && m_bSelected) + { + + + { + auto pVertices = vShadow; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[0].pos.x, pVertices[0].pos.y, + pVertices[0].pos.z, pVertices[0].tu, + pVertices[0].tv, pVertices[0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[2].pos.x, pVertices[2].pos.y, + pVertices[2].pos.z, pVertices[2].tu, + pVertices[2].tv, pVertices[2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[1].pos.x, pVertices[1].pos.y, + pVertices[1].pos.z, pVertices[1].tu, + pVertices[1].tv, pVertices[1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[3].pos.x, pVertices[3].pos.y, + pVertices[3].pos.z, pVertices[3].tu, + pVertices[3].tv, pVertices[3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + + { + auto pVertices = vFace; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[0].pos.x, pVertices[0].pos.y, + pVertices[0].pos.z, pVertices[0].tu, + pVertices[0].tv, pVertices[0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[2].pos.x, pVertices[2].pos.y, + pVertices[2].pos.z, pVertices[2].tu, + pVertices[2].tv, pVertices[2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[1].pos.x, pVertices[1].pos.y, + pVertices[1].pos.z, pVertices[1].tu, + pVertices[1].tv, pVertices[1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[3].pos.x, pVertices[3].pos.y, + pVertices[3].pos.z, pVertices[3].tu, + pVertices[3].tv, pVertices[3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + } + else + { + + { + auto pVertices = vFace; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[0].pos.x, pVertices[0].pos.y, + pVertices[0].pos.z, pVertices[0].tu, + pVertices[0].tv, pVertices[0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[2].pos.x, pVertices[2].pos.y, + pVertices[2].pos.z, pVertices[2].tu, + pVertices[2].tv, pVertices[2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[1].pos.x, pVertices[1].pos.y, + pVertices[1].pos.z, pVertices[1].tu, + pVertices[1].tv, pVertices[1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[3].pos.x, pVertices[3].pos.y, + pVertices[3].pos.z, pVertices[3].tu, + pVertices[3].tv, pVertices[3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + } + + } if (m_idString != -1L) @@ -185,7 +268,7 @@ void CXI_BUTTON::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const // get group name and get texture for this if (ReadIniString(ini1, name1, ini2, name2, "group", param, sizeof(param), "")) { - m_idTex = pPictureService->GetTextureID(param); + m_idTex = pPictureService->BGFXGetTextureID(param); const auto len = strlen(param) + 1; m_sGroupName = new char[len]; if (m_sGroupName == nullptr) @@ -194,7 +277,7 @@ void CXI_BUTTON::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const // get button picture name if (ReadIniString(ini1, name1, ini2, name2, "picture", param, sizeof(param), "")) - pPictureService->GetTexturePos(m_sGroupName, param, m_tRect); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, m_tRect); } else { @@ -233,7 +316,7 @@ void CXI_BUTTON::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const void CXI_BUTTON::ReleaseAll() { - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); STORM_DELETE(m_sGroupName); FONT_RELEASE(m_rs, m_nFontNum); VIDEOTEXTURE_RELEASE(m_rs, m_pTex); @@ -323,7 +406,7 @@ uint32_t CXI_BUTTON::MessageProc(long msgcode, MESSAGE &message) if (m_sGroupName == nullptr || _stricmp(m_sGroupName, param) != 0) { - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); STORM_DELETE(m_sGroupName); m_sGroupName = new char[len]; diff --git a/src/libs/Xinterface/Nodes/xi_image.cpp b/src/libs/Xinterface/Nodes/xi_image.cpp index fe2f254a0..48758bf8a 100644 --- a/src/libs/Xinterface/Nodes/xi_image.cpp +++ b/src/libs/Xinterface/Nodes/xi_image.cpp @@ -1,6 +1,8 @@ #include "xi_image.h" #include "../xinterface.h" +#include "primitive_renderer.h" + #define DEFAULT_IMAGE_OBJECT_WIDTH 32 #define DEFAULT_IMAGE_OBJECT_HEIGHT 32 #define DEFAULT_IMAGE_OBJECT_COLOR 0xFF808080 @@ -44,7 +46,7 @@ CXI_IMAGE::~CXI_IMAGE() void CXI_IMAGE::LoadFromFile(const char *sFileName) { Unload(); - m_nTextureID = m_rs->TextureCreate(sFileName); + m_nTextureID = m_rs->BGFXTextureCreate(sFileName); UpdateTexture(); UpdatePosition(); } @@ -63,13 +65,13 @@ void CXI_IMAGE::LoadFromBase(const char *sListName, const char *sPictureName, bo } memcpy(m_pcPictureListName, sListName, len); } - m_nTextureID = XINTERFACE::GetPictureService()->GetTextureID(m_pcPictureListName); - m_nPictureNum = XINTERFACE::GetPictureService()->GetImageNum(m_pcPictureListName, sPictureName); + m_nTextureID = XINTERFACE::GetPictureService()->BGFXGetTextureID(m_pcPictureListName); + m_nPictureNum = XINTERFACE::GetPictureService()->BGFXGetImageNum(m_pcPictureListName, sPictureName); if (bGetSizeFromSource) { XYRECT rPos; - if (XINTERFACE::GetPictureService()->GetTexturePos(m_nPictureNum, rPos)) + if (XINTERFACE::GetPictureService()->BGFXGetTexturePos(m_nPictureNum, rPos)) { m_pntSize.x = rPos.right - rPos.left; m_pntSize.y = rPos.bottom - rPos.top; @@ -106,7 +108,7 @@ void CXI_IMAGE::LoadAccordingToString(const char *pcImageParam) case InterfaceToken_file: if (CXI_UTILS::StringGetTokenString(pcParam, tokenString, sizeof(tokenString))) { - m_nTextureID = m_rs->TextureCreate(tokenString); + m_nTextureID = m_rs->BGFXTextureCreate(tokenString); UpdateTexture(); } break; @@ -115,14 +117,14 @@ void CXI_IMAGE::LoadAccordingToString(const char *pcImageParam) if (CXI_UTILS::StringGetTokenString(pcParam, tokenString, sizeof(tokenString))) { CXI_UTILS::StringDoublicate(tokenString, m_pcPictureListName); - m_nTextureID = XINTERFACE::GetPictureService()->GetTextureID(m_pcPictureListName); + m_nTextureID = XINTERFACE::GetPictureService()->BGFXGetTextureID(m_pcPictureListName); } break; case InterfaceToken_picture_name: m_bThisIsColorRectangle = false; if (CXI_UTILS::StringGetTokenString(pcParam, tokenString, sizeof(tokenString))) { - m_nPictureNum = XINTERFACE::GetPictureService()->GetImageNum(m_pcPictureListName, tokenString); + m_nPictureNum = XINTERFACE::GetPictureService()->BGFXGetImageNum(m_pcPictureListName, tokenString); UpdateTexture(); } break; @@ -177,17 +179,49 @@ void CXI_IMAGE::Draw() if (m_bThisIsColorRectangle) { - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_vrtx, sizeof(XI_ONETEX_VERTEX), "iRectangle"); + //m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_vrtx, sizeof(XI_ONETEX_VERTEX), "iRectangle"); return; } if (IsImagePresent()) { - if (m_pTexture) + /*if (m_pTexture) //@BGFX TODO m_rs->SetTexture(0, m_pTexture); else - m_rs->TextureSet(0, m_nTextureID); - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_vrtx, sizeof(XI_ONETEX_VERTEX), "iVideo"); + m_rs->TextureSet(0, m_nTextureID);*/ + + if (m_pTexture) + { + + } + else + { + + auto texture = m_rs->GetBGFXTextureFromID(m_nTextureID); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + { + auto pVertices = m_vrtx; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[0].pos.x, pVertices[0].pos.y, + pVertices[0].pos.z, pVertices[0].tu, pVertices[0].tv, + pVertices[0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[2].pos.x, pVertices[2].pos.y, + pVertices[2].pos.z, pVertices[2].tu, pVertices[2].tv, + pVertices[2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[1].pos.x, pVertices[1].pos.y, + pVertices[1].pos.z, pVertices[1].tu, pVertices[1].tv, + pVertices[1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[3].pos.x, pVertices[3].pos.y, + pVertices[3].pos.z, pVertices[3].tu, pVertices[3].tv, + pVertices[3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + } + + //m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_vrtx, sizeof(XI_ONETEX_VERTEX), "iVideo"); } } @@ -310,11 +344,11 @@ void CXI_IMAGE::Unload() m_nPictureNum = -1; if (m_pcPictureListName) { - PICTURE_TEXTURE_RELEASE(XINTERFACE::GetPictureService(), m_pcPictureListName, m_nTextureID); + BGFX_PICTURE_TEXTURE_RELEASE(XINTERFACE::GetPictureService(), m_pcPictureListName, m_nTextureID); } else { - TEXTURE_RELEASE(m_rs, m_nTextureID); + BGFX_TEXTURE_RELEASE(m_rs, m_nTextureID); } } RELEASE(m_pTexture); @@ -334,13 +368,13 @@ void CXI_IMAGE::UpdateTexture() FXYRECT frTex; if (m_nPictureNum != -1) { - XINTERFACE::GetPictureService()->GetTexturePos(m_nPictureNum, frTex); + XINTERFACE::GetPictureService()->BGFXGetTexturePos(m_nPictureNum, frTex); } else { if (m_pcPictureListName) { - XINTERFACE::GetPictureService()->GetTextureCutForSize(m_pcPictureListName, m_nLeftTopCutUV, m_pntSize, -1, + XINTERFACE::GetPictureService()->BGFXGetTextureCutForSize(m_pcPictureListName, m_nLeftTopCutUV, m_pntSize, -1, -1, frTex); } else diff --git a/src/libs/Xinterface/Nodes/xi_lrchanger.cpp b/src/libs/Xinterface/Nodes/xi_lrchanger.cpp index 41a2a41b3..7377ec705 100644 --- a/src/libs/Xinterface/Nodes/xi_lrchanger.cpp +++ b/src/libs/Xinterface/Nodes/xi_lrchanger.cpp @@ -1,5 +1,7 @@ #include "xi_lrchanger.h" +#include "primitive_renderer.h" + void SetOneTextureCoordinate(XI_ONETEX_VERTEX v[4], const FXYRECT &tr) { v[0].tu = tr.left; @@ -85,7 +87,8 @@ void CXI_LRCHANGER::Draw(bool bSelected, uint32_t Delta_Time) vShadow[i].pos.z = 1.f; } - m_rs->TextureSet(0, m_idTex); + auto texture = m_rs->GetBGFXTextureFromID(m_idTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; // show left button SetOneTextureCoordinate(vFace, m_tLRect); @@ -100,8 +103,41 @@ void CXI_LRCHANGER::Draw(bool bSelected, uint32_t Delta_Time) SetRectanglePosition(vFace, m_posLRect); SetRectanglePosition(vShadow, m_posLRect + m_ShadowShift); } - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, vShadow, sizeof(XI_ONETEX_VERTEX), "iShadow"); - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, vFace, sizeof(XI_ONETEX_VERTEX), "iIcon"); + + + + { + auto pVertices = vShadow; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[0].pos.x, pVertices[0].pos.y, pVertices[0].pos.z, + pVertices[0].tu, pVertices[0].tv, pVertices[0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[2].pos.x, pVertices[2].pos.y, pVertices[2].pos.z, + pVertices[2].tu, pVertices[2].tv, pVertices[1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[1].pos.x, pVertices[1].pos.y, pVertices[1].pos.z, + pVertices[1].tu, pVertices[1].tv, pVertices[2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[3].pos.x, pVertices[3].pos.y, pVertices[3].pos.z, + pVertices[3].tu, pVertices[3].tv, pVertices[3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + + } + + + { + auto pVertices = vFace; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[0].pos.x, pVertices[0].pos.y, pVertices[0].pos.z, + pVertices[0].tu, pVertices[0].tv, pVertices[0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[2].pos.x, pVertices[2].pos.y, pVertices[2].pos.z, + pVertices[2].tu, pVertices[2].tv, pVertices[1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[1].pos.x, pVertices[1].pos.y, pVertices[1].pos.z, + pVertices[1].tu, pVertices[1].tv, pVertices[2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[3].pos.x, pVertices[3].pos.y, pVertices[3].pos.z, + pVertices[3].tu, pVertices[3].tv, pVertices[3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + // show right button SetOneTextureCoordinate(vFace, m_tRRect); SetOneTextureCoordinate(vShadow, m_tRRect); @@ -115,8 +151,38 @@ void CXI_LRCHANGER::Draw(bool bSelected, uint32_t Delta_Time) SetRectanglePosition(vFace, m_posRRect); SetRectanglePosition(vShadow, m_posRRect + m_ShadowShift); } - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, vShadow, sizeof(XI_ONETEX_VERTEX), "iShadow"); - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, vFace, sizeof(XI_ONETEX_VERTEX), "iIcon"); + + + { + auto pVertices = vShadow; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[0].pos.x, pVertices[0].pos.y, pVertices[0].pos.z, + pVertices[0].tu, pVertices[0].tv, pVertices[0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[2].pos.x, pVertices[2].pos.y, pVertices[2].pos.z, + pVertices[2].tu, pVertices[2].tv, pVertices[1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[1].pos.x, pVertices[1].pos.y, pVertices[1].pos.z, + pVertices[1].tu, pVertices[1].tv, pVertices[2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[3].pos.x, pVertices[3].pos.y, pVertices[3].pos.z, + pVertices[3].tu, pVertices[3].tv, pVertices[3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + { + auto pVertices = vFace; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[0].pos.x, pVertices[0].pos.y, pVertices[0].pos.z, + pVertices[0].tu, pVertices[0].tv, pVertices[0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[2].pos.x, pVertices[2].pos.y, pVertices[2].pos.z, + pVertices[2].tu, pVertices[2].tv, pVertices[2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[1].pos.x, pVertices[1].pos.y, pVertices[1].pos.z, + pVertices[1].tu, pVertices[1].tv, pVertices[1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[3].pos.x, pVertices[3].pos.y, pVertices[3].pos.z, + pVertices[3].tu, pVertices[3].tv, pVertices[3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + } } @@ -168,14 +234,14 @@ void CXI_LRCHANGER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, con if (m_sGroupName == nullptr) throw std::runtime_error("allocate memory error"); memcpy(m_sGroupName, param, len); - m_idTex = pPictureService->GetTextureID(param); + m_idTex = pPictureService->BGFXGetTextureID(param); } // get buttons picture name if (ReadIniString(ini1, name1, ini2, name2, "lpicture", param, sizeof(param), "")) - pPictureService->GetTexturePos(m_sGroupName, param, m_tLRect); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, m_tLRect); if (ReadIniString(ini1, name1, ini2, name2, "rpicture", param, sizeof(param), "")) - pPictureService->GetTexturePos(m_sGroupName, param, m_tRRect); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, m_tRRect); // get offset button image in case pressed button m_PressShift = GetIniFloatPoint(ini1, name1, ini2, name2, "pressPictureOffset", FXYPOINT(0.f, 0.f)); @@ -192,7 +258,7 @@ void CXI_LRCHANGER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, con void CXI_LRCHANGER::ReleaseAll() { - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); STORM_DELETE(m_sGroupName); } diff --git a/src/libs/Xinterface/Nodes/xi_picture.cpp b/src/libs/Xinterface/Nodes/xi_picture.cpp index cbc1c52a9..b77561eed 100644 --- a/src/libs/Xinterface/Nodes/xi_picture.cpp +++ b/src/libs/Xinterface/Nodes/xi_picture.cpp @@ -3,6 +3,8 @@ #include "storm_assert.h" #include "vfile_service.h" +#include "primitive_renderer.h" + CXI_PICTURE::CXI_PICTURE() { m_rs = nullptr; @@ -54,13 +56,62 @@ void CXI_PICTURE::Draw(bool bSelected, uint32_t Delta_Time) if (m_idTex != -1 || m_pTex || m_pD3D8Texture) { + bool textureSet = false; if (m_idTex != -1) - m_rs->TextureSet(0, m_idTex); - else if (m_pD3D8Texture) - m_rs->SetTexture(0, m_pD3D8Texture); + { + auto texture = m_rs->GetBGFXTextureFromID(m_idTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + textureSet = true; + } + //else if (m_pD3D8Texture) //@BGFX TODO + //m_rs->SetTexture(0, m_pD3D8Texture); + //else else - m_rs->SetTexture(0, m_pTex ? m_pTex->m_pTexture : nullptr); - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_v, sizeof(XI_ONETEX_VERTEX), "iVideo"); + { + //if (m_pTex) { // @BGFX TODO + //m_rs->SetTexture(0, m_pTex ? m_pTex->m_pTexture : nullptr); + //textureSet = true; + //} + } + + if (textureSet) + { + std::vector vertices; + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR(m_v[0].pos.x, m_v[0].pos.y, m_v[0].pos.z, m_v[0].tu, + m_v[0].tv, m_v[0].color)); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR(m_v[2].pos.x, m_v[2].pos.y, m_v[2].pos.z, m_v[2].tu, + m_v[2].tv, m_v[2].color)); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR(m_v[1].pos.x, m_v[1].pos.y, m_v[1].pos.z, m_v[1].tu, + m_v[1].tv, m_v[1].color)); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR(m_v[3].pos.x, m_v[3].pos.y, m_v[3].pos.z, m_v[3].tu, + m_v[3].tv, m_v[3].color)); + m_rs->GetPrimitiveRenderer()->TestSubmit(vertices); + + /*std::vector vertices; + + vertices.push_back({m_v[0].pos.x, m_v[0].pos.y, m_v[0].pos.z}); // top left + vertices.push_back({m_v[2].pos.x, m_v[2].pos.y, m_v[2].pos.z}); // top right + vertices.push_back({m_v[1].pos.x, m_v[1].pos.y, m_v[1].pos.z}); // bottom left + vertices.push_back({m_v[3].pos.x, m_v[3].pos.y, m_v[3].pos.z}); // bottom right + + std::vector> uv; + uv.push_back({m_v[0].tu, m_v[0].tv}); + uv.push_back({m_v[2].tu, m_v[2].tv}); + uv.push_back({m_v[1].tu, m_v[1].tv}); + uv.push_back({m_v[3].tu, m_v[3].tv}); + + std::vector colors; + + colors.push_back(m_v[0].color); + colors.push_back(m_v[1].color); + colors.push_back(m_v[2].color); + colors.push_back(m_v[3].color); + + m_rs->GetPrimitiveRenderer()->Submit(vertices, uv, colors);*/ + + } + } } } @@ -83,19 +134,19 @@ void CXI_PICTURE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const if (ReadIniString(ini1, name1, ini2, name2, "groupName", param, sizeof(param), "")) { - m_idTex = pPictureService->GetTextureID(param); + m_idTex = pPictureService->BGFXGetTextureID(param); const auto len = strlen(param) + 1; m_pcGroupName = new char[len]; Assert(m_pcGroupName); memcpy(m_pcGroupName, param, len); if (ReadIniString(ini1, name1, ini2, name2, "picName", param, sizeof(param), "")) - pPictureService->GetTexturePos(m_pcGroupName, param, texRect); + pPictureService->BGFXGetTexturePos(m_pcGroupName, param, texRect); } else { if (ReadIniString(ini1, name1, ini2, name2, "textureName", param, sizeof(param), "")) - m_idTex = m_rs->TextureCreate(param); + m_idTex = m_rs->BGFXTextureCreate(param); texRect = GetIniFloatRect(ini1, name1, ini2, name2, "textureRect", texRect); } @@ -220,14 +271,14 @@ void CXI_PICTURE::SetNewPictureByGroup(char *groupName, char *picName) m_pcGroupName = new char[strlen(groupName) + 1]; Assert(m_pcGroupName); memcpy(m_pcGroupName, groupName, len); - m_idTex = pPictureService->GetTextureID(groupName); + m_idTex = pPictureService->BGFXGetTextureID(groupName); } } if (m_pcGroupName && picName) { FXYRECT texRect; - pPictureService->GetTexturePos(m_pcGroupName, picName, texRect); + pPictureService->BGFXGetTexturePos(m_pcGroupName, picName, texRect); ChangeUV(texRect); } } @@ -435,6 +486,6 @@ void CXI_PICTURE::ReleasePicture() m_pD3D8Texture->Release(); m_pD3D8Texture = nullptr; STORM_DELETE(m_pcGroupName); - TEXTURE_RELEASE(m_rs, m_idTex); + BGFX_TEXTURE_RELEASE(m_rs, m_idTex); VIDEOTEXTURE_RELEASE(m_rs, m_pTex); } diff --git a/src/libs/Xinterface/Nodes/xi_rectangle.cpp b/src/libs/Xinterface/Nodes/xi_rectangle.cpp index af3f7927e..dc458ad2d 100644 --- a/src/libs/Xinterface/Nodes/xi_rectangle.cpp +++ b/src/libs/Xinterface/Nodes/xi_rectangle.cpp @@ -1,5 +1,8 @@ #include "xi_rectangle.h" +#include "primitive_renderer.h" + + CXI_RECTANGLE::CXI_RECTANGLE() { m_nNodeType = NODETYPE_RECTANGLE; @@ -14,8 +17,28 @@ void CXI_RECTANGLE::Draw(bool bSelected, uint32_t Delta_Time) { if (m_bUse) { - m_rs->TextureSet(0, 0); - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_NOTEX_FVF, 2, m_pVert, sizeof(XI_NOTEX_VERTEX), "iRectangle"); + //m_rs->TextureSet(0, 0); + + + for (int i = 0; i < 4; i += 4) + { + std::vector vertices; + + auto pV = m_pVert; + + vertices.push_back( + VERTEX_POSITION_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, pV[i + 0].color}); + vertices.push_back( + VERTEX_POSITION_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, pV[i + 0].color}); + vertices.push_back( + VERTEX_POSITION_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, pV[i + 0].color}); + vertices.push_back( + VERTEX_POSITION_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, pV[i + 0].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + //m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_NOTEX_FVF, 2, m_pVert, sizeof(XI_NOTEX_VERTEX), "iRectangle"); if (m_bBorder) { RS_LINE pLines[8]; diff --git a/src/libs/Xinterface/Nodes/xi_scrollbar.cpp b/src/libs/Xinterface/Nodes/xi_scrollbar.cpp index 5628712bc..767037d7a 100644 --- a/src/libs/Xinterface/Nodes/xi_scrollbar.cpp +++ b/src/libs/Xinterface/Nodes/xi_scrollbar.cpp @@ -1,5 +1,8 @@ #include "xi_scrollbar.h" +#include "primitive_renderer.h" + + #define CLICK_TYPE_CENTER 0 #define CLICK_TYPE_LEFT 1 #define CLICK_TYPE_RIGHT 2 @@ -18,10 +21,7 @@ CXI_SCROLLBAR::CXI_SCROLLBAR() m_nFontID = -1; - m_idVBuf = -1L; - m_idIBuf = -1L; m_nVert = 0; - m_nIndx = 0; m_bClickable = true; m_nNodeType = NODETYPE_SCROLLBAR; @@ -64,7 +64,7 @@ void CXI_SCROLLBAR::Draw(bool bSelected, uint32_t Delta_Time) { if (bSelected ^ m_bPrevSelectStatus) { - auto *pVert = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); + auto *pVert = m_vertices.data(); if (pVert != nullptr) { m_bPrevSelectStatus = bSelected; @@ -92,29 +92,153 @@ void CXI_SCROLLBAR::Draw(bool bSelected, uint32_t Delta_Time) pVert[idx + 7].tu = m_rectCenterTex.right; pVert[idx + 7].tv = m_rectCenterTex.bottom; } - - m_rs->UnLockVertexBuffer(m_idVBuf); } } - m_rs->TextureSet(0, m_idTex); + //m_rs->TextureSet(0, m_idTex); + + auto texture = m_rs->GetBGFXTextureFromID(m_idTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + // show shadow if (m_nPressedDelay > 0) if (m_bRightPress) - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 60, 12, 0, m_nIndx, "iShadow"); + { + for (int i = 60; i < 60 + 12; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + // m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 60, 12, 0, m_nIndx, "iShadow"); + } + else - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 36, 12, 0, m_nIndx, "iShadow"); + { + for (int i = 36; i < 36 + 12; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + //m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 36, 12, 0, m_nIndx, "iShadow"); + } + else - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 12, 12, 0, m_nIndx, "iShadow"); + { + for (int i = 12; i < 12 + 12; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + //m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 12, 12, 0, m_nIndx, "iShadow"); + } + // show button if (m_nPressedDelay > 0) - if (m_bRightPress) - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 48, 12, 0, m_nIndx, "iButton"); + if (m_bRightPress) + { + for (int i = 48; i < 48 + 12; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + //m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 48, 12, 0, m_nIndx, "iButton"); + } else - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 24, 12, 0, m_nIndx, "iButton"); + { + for (int i = 24; i < 24 + 12; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + //m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 24, 12, 0, m_nIndx, "iButton"); + } + else - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 0, 12, 0, m_nIndx, "iButton"); + { + for (int i = 0; i < 0 + 12; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + //m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 0, 12, 0, m_nIndx, "iButton"); + } + } if (m_bShowString) @@ -178,7 +302,7 @@ void CXI_SCROLLBAR::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, con if (m_sGroupName == nullptr) throw std::runtime_error("allocate memory error"); memcpy(m_sGroupName, param, len); - m_idTex = pPictureService->GetTextureID(param); + m_idTex = pPictureService->BGFXGetTextureID(param); } // get offset button image in case pressed button @@ -200,28 +324,15 @@ void CXI_SCROLLBAR::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, con m_nMaxDelay = GetIniLong(ini1, name1, ini2, name2, "pressDelay", 20); m_nVert = 12 * 6; // - m_nIndx = 3 * 2 * 3; // 3 rectangle * 2 treangle into rectangle * 3 vertex into triangle - m_idIBuf = m_rs->CreateIndexBuffer(m_nIndx * 2); - m_idVBuf = m_rs->CreateVertexBuffer(XI_ONETEX_FVF, m_nVert * sizeof(XI_ONETEX_VERTEX), D3DUSAGE_WRITEONLY); + m_vertices.reserve(m_nVert); + m_vertices.resize(m_nVert); + // Lock buffers for write - auto *pVert = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); - auto *pIndx = static_cast(m_rs->LockIndexBuffer(m_idIBuf)); - if (pVert == nullptr || pIndx == nullptr) - throw std::runtime_error("can not create the index&vertex buffers"); + auto *pVert = m_vertices.data(); // fill triangles buffer auto i = 0; - for (auto tidx = 0; tidx < 3; tidx++) - { - pIndx[i + 0] = tidx * 4; - pIndx[i + 1] = tidx * 4 + 1; - pIndx[i + 2] = tidx * 4 + 2; - pIndx[i + 3] = tidx * 4 + 2; - pIndx[i + 4] = tidx * 4 + 1; - pIndx[i + 5] = tidx * 4 + 3; - i += 6; - } // fill vertex buffer for (i = 0; i < m_nVert; i++) @@ -239,17 +350,14 @@ void CXI_SCROLLBAR::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, con for (; i < 12 * 6; i++) pVert[i].color = m_dwShadowColor; - m_rs->UnLockVertexBuffer(m_idVBuf); - m_rs->UnLockIndexBuffer(m_idIBuf); - if (ReadIniString(ini1, name1, ini2, name2, "leftPicture", param, sizeof(param), "")) - pPictureService->GetTexturePos(m_sGroupName, param, m_frLeftTex); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, m_frLeftTex); if (ReadIniString(ini1, name1, ini2, name2, "rightPicture", param, sizeof(param), "")) - pPictureService->GetTexturePos(m_sGroupName, param, m_frRightTex); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, m_frRightTex); if (ReadIniString(ini1, name1, ini2, name2, "centerPicture", param, sizeof(param), "")) - pPictureService->GetTexturePos(m_sGroupName, param, m_rectCenterTex); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, m_rectCenterTex); if (ReadIniString(ini1, name1, ini2, name2, "selectCenterPicture", param, sizeof(param), "")) - pPictureService->GetTexturePos(m_sGroupName, param, m_rectSelectCenterTex); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, m_rectSelectCenterTex); m_bPrevSelectStatus = false; m_nBarWidth = GetIniLong(ini1, name1, ini2, name2, "barWidth", -1); m_nSideWidth = GetIniLong(ini1, name1, ini2, name2, "sideWidth", -1); @@ -258,7 +366,6 @@ void CXI_SCROLLBAR::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, con if (m_nBarWidth < 0 && m_nSideWidth < 0) m_nSideWidth = (m_rect.bottom - m_rect.top); - m_nIndx /= 3; UpdatePosition(); m_bShowString = GetIniBool(ini1, name1, ini2, name2, "valueShow", false); @@ -278,10 +385,8 @@ void CXI_SCROLLBAR::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, con void CXI_SCROLLBAR::ReleaseAll() { - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); STORM_DELETE(m_sGroupName); - VERTEX_BUFFER_RELEASE(m_rs, m_idVBuf); - INDEX_BUFFER_RELEASE(m_rs, m_idIBuf); FONT_RELEASE(m_rs, m_nFontID); } @@ -406,9 +511,9 @@ uint32_t CXI_SCROLLBAR::MessageProc(long msgcode, MESSAGE &message) return 0; } -void CXI_SCROLLBAR::UpdatePosition() const +void CXI_SCROLLBAR::UpdatePosition() { - auto *pVert = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); + auto *pVert = m_vertices.data(); auto idx = 0; int sideWidth = m_nSideWidth; @@ -581,8 +686,6 @@ void CXI_SCROLLBAR::UpdatePosition() const pVert[idx + 10].pos.y = pVert[idx + 11].pos.y = static_cast(m_rect.bottom) + m_fYDeltaPress + m_fYShadowPress; idx += 12; - - m_rs->UnLockVertexBuffer(m_idVBuf); } void CXI_SCROLLBAR::WriteDataToAttribute() const diff --git a/src/libs/Xinterface/Nodes/xi_scrollbar.h b/src/libs/Xinterface/Nodes/xi_scrollbar.h index d329e887d..db0ca1a4b 100644 --- a/src/libs/Xinterface/Nodes/xi_scrollbar.h +++ b/src/libs/Xinterface/Nodes/xi_scrollbar.h @@ -29,7 +29,7 @@ class CXI_SCROLLBAR : public CINODE protected: void LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const char *name2) override; - void UpdatePosition() const; + void UpdatePosition(); void WriteDataToAttribute() const; void ChangeValue(bool bGrowing, bool bMultiply); @@ -61,10 +61,8 @@ class CXI_SCROLLBAR : public CINODE int m_clickType; // picture index & vertex buffers - long m_idVBuf; // identificator of the vertex buffer - long m_idIBuf; // identificator of the index buffer + std::vector m_vertices; long m_nVert; // vertex quantity - long m_nIndx; // index quantity FXYRECT m_frLeftTex; FXYRECT m_frRightTex; diff --git a/src/libs/Xinterface/Nodes/xi_scroller.cpp b/src/libs/Xinterface/Nodes/xi_scroller.cpp index f9f913933..dbafee57e 100644 --- a/src/libs/Xinterface/Nodes/xi_scroller.cpp +++ b/src/libs/Xinterface/Nodes/xi_scroller.cpp @@ -7,6 +7,9 @@ #include "xi_table.h" #include +#include "primitive_renderer.h" + + void SetVertexRectangleTex(XI_ONLYONETEX_VERTEX *pv, FXYRECT &texRect) { pv[0].tu = texRect.left; @@ -38,7 +41,6 @@ CXI_SCROLLER::CXI_SCROLLER() m_idBaseTex = -1; m_idRollerTex = -1; - m_idVBuf = -1; m_bSelected = true; m_bDragRoll = false; @@ -63,12 +65,100 @@ void CXI_SCROLLER::Draw(bool bSelected, uint32_t Delta_Time) MouseMove(); } - m_rs->TextureSet(0, m_idBaseTex); + /*m_rs->TextureSet(0, m_idBaseTex); m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONLYONETEX_VERTEX), 0, 2, "iStatusLine"); m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONLYONETEX_VERTEX), 4, 2, "iStatusLine"); m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONLYONETEX_VERTEX), 8, 2, "iStatusLine"); m_rs->TextureSet(0, m_idRollerTex); - m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONLYONETEX_VERTEX), 12, 2, "iStatusLine"); + m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONLYONETEX_VERTEX), 12, 2, "iStatusLine");*/ + + auto texture = m_rs->GetBGFXTextureFromID(m_idBaseTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + for (long n = 0; n < 4; n += 4) + { + auto &pVertices = m_vertices; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 0].pos.x, pVertices[n + 0].pos.y, + pVertices[n + 0].pos.z, pVertices[n + 0].tu, + pVertices[n + 0].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 2].pos.x, pVertices[n + 2].pos.y, + pVertices[n + 2].pos.z, pVertices[n + 2].tu, + pVertices[n + 2].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 1].pos.x, pVertices[n + 1].pos.y, + pVertices[n + 1].pos.z, pVertices[n + 1].tu, + pVertices[n + 1].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 3].pos.x, pVertices[n + 3].pos.y, + pVertices[n + 3].pos.z, pVertices[n + 3].tu, + pVertices[n + 3].tv}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + for (long n = 4; n < 8; n += 4) + { + auto &pVertices = m_vertices; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 0].pos.x, pVertices[n + 0].pos.y, + pVertices[n + 0].pos.z, pVertices[n + 0].tu, + pVertices[n + 0].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 2].pos.x, pVertices[n + 2].pos.y, + pVertices[n + 2].pos.z, pVertices[n + 2].tu, + pVertices[n + 2].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 1].pos.x, pVertices[n + 1].pos.y, + pVertices[n + 1].pos.z, pVertices[n + 1].tu, + pVertices[n + 1].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 3].pos.x, pVertices[n + 3].pos.y, + pVertices[n + 3].pos.z, pVertices[n + 3].tu, + pVertices[n + 3].tv}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + for (long n = 8; n < 12; n += 4) + { + auto &pVertices = m_vertices; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 0].pos.x, pVertices[n + 0].pos.y, + pVertices[n + 0].pos.z, pVertices[n + 0].tu, + pVertices[n + 0].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 2].pos.x, pVertices[n + 2].pos.y, + pVertices[n + 2].pos.z, pVertices[n + 2].tu, + pVertices[n + 2].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 1].pos.x, pVertices[n + 1].pos.y, + pVertices[n + 1].pos.z, pVertices[n + 1].tu, + pVertices[n + 1].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 3].pos.x, pVertices[n + 3].pos.y, + pVertices[n + 3].pos.z, pVertices[n + 3].tu, + pVertices[n + 3].tv}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + + texture = m_rs->GetBGFXTextureFromID(m_idRollerTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + for (long n = 12; n < 16; n += 4) + { + auto &pVertices = m_vertices; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 0].pos.x, pVertices[n + 0].pos.y, + pVertices[n + 0].pos.z, pVertices[n + 0].tu, + pVertices[n + 0].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 2].pos.x, pVertices[n + 2].pos.y, + pVertices[n + 2].pos.z, pVertices[n + 2].tu, + pVertices[n + 2].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 1].pos.x, pVertices[n + 1].pos.y, + pVertices[n + 1].pos.z, pVertices[n + 1].tu, + pVertices[n + 1].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pVertices[n + 3].pos.x, pVertices[n + 3].pos.y, + pVertices[n + 3].pos.z, pVertices[n + 3].tu, + pVertices[n + 3].tv}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + } } @@ -86,9 +176,8 @@ bool CXI_SCROLLER::Init(INIFILE *ini1, const char *name1, INIFILE *ini2, const c void CXI_SCROLLER::ReleaseAll() { m_bUse = false; - TEXTURE_RELEASE(m_rs, m_idBaseTex); - TEXTURE_RELEASE(m_rs, m_idRollerTex); - VERTEX_BUFFER_RELEASE(m_rs, m_idVBuf); + BGFX_TEXTURE_RELEASE(m_rs, m_idBaseTex); + BGFX_TEXTURE_RELEASE(m_rs, m_idRollerTex); m_asOwnedNodes.clear(); } @@ -139,11 +228,11 @@ void CXI_SCROLLER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, cons // Get texture name and load that texture m_idBaseTex = -1; if (ReadIniString(ini1, name1, ini2, name2, "baseTexture", param, sizeof(param), "")) - m_idBaseTex = m_rs->TextureCreate(param); + m_idBaseTex = m_rs->BGFXTextureCreate(param); m_idRollerTex = -1; if (ReadIniString(ini1, name1, ini2, name2, "rollerTexture", param, sizeof(param), "")) - m_idRollerTex = m_rs->TextureCreate(param); + m_idRollerTex = m_rs->BGFXTextureCreate(param); // Set buffers m_fOffTexHeight = GetIniFloat(ini1, name1, ini2, name2, "begEndTexSize", 0.f); @@ -158,7 +247,8 @@ void CXI_SCROLLER::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, cons m_ScrollTexRect = GetIniFloatRect(ini1, name1, ini2, name2, "scrollTexPos", FXYRECT(0.f, 0.f, 1.f, 1.f)); m_RollTexRect = GetIniFloatRect(ini1, name1, ini2, name2, "rollTexPos", FXYRECT(0.f, 0.f, 1.f, 1.f)); - m_idVBuf = m_rs->CreateVertexBuffer(XI_ONLYONETEX_FVF, 16 * sizeof(XI_ONLYONETEX_VERTEX), D3DUSAGE_WRITEONLY); + m_vertices.resize(16); + //m_idVBuf = m_rs->CreateVertexBuffer(XI_ONLYONETEX_FVF, 16 * sizeof(XI_ONLYONETEX_VERTEX), D3DUSAGE_WRITEONLY); FillVertexBuffer(); } @@ -299,11 +389,10 @@ void CXI_SCROLLER::SetRollerPos(float pos) m_rollerCur.top = m_rollerPlace.top + m_fPos * (m_rollerPlace.bottom - m_rollerPlace.top - m_rollerHeight); m_rollerCur.bottom = m_rollerCur.top + m_rollerHeight; - auto *pV = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); + auto *pV = m_vertices.data(); if (pV != nullptr) { SetVertexRectanglePos(&pV[12], m_rollerCur); - m_rs->UnLockVertexBuffer(m_idVBuf); } } @@ -317,7 +406,7 @@ void CXI_SCROLLER::LinkNodeChanged(float fPos) void CXI_SCROLLER::FillVertexBuffer() { - auto *pV = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); + auto *pV = m_vertices.data(); if (pV != nullptr) { for (auto i = 0; i < 16; i++) @@ -372,8 +461,6 @@ void CXI_SCROLLER::FillVertexBuffer() m_rollerCur.right = m_rollerPlace.right; m_rollerCur.bottom = m_rollerPlace.top + m_rollerHeight; SetVertexRectanglePos(&pV[12], m_rollerCur); - - m_rs->UnLockVertexBuffer(m_idVBuf); } } diff --git a/src/libs/Xinterface/Nodes/xi_scroller.h b/src/libs/Xinterface/Nodes/xi_scroller.h index 2d8adfe13..bb32acda8 100644 --- a/src/libs/Xinterface/Nodes/xi_scroller.h +++ b/src/libs/Xinterface/Nodes/xi_scroller.h @@ -46,7 +46,7 @@ class CXI_SCROLLER : public CINODE long m_idBaseTex; // border texture identificator long m_idRollerTex; // roller texture identificator - long m_idVBuf; + std::vector m_vertices; FXYRECT m_rollerPlace; FXYRECT m_rollerCur; diff --git a/src/libs/Xinterface/Nodes/xi_scrollimage.cpp b/src/libs/Xinterface/Nodes/xi_scrollimage.cpp index 70802b43f..5d71b5c76 100644 --- a/src/libs/Xinterface/Nodes/xi_scrollimage.cpp +++ b/src/libs/Xinterface/Nodes/xi_scrollimage.cpp @@ -1,5 +1,8 @@ #include "xi_scrollimage.h" +#include "primitive_renderer.h" + + #define MAXIMAGEQUANTITY 100 long GetTexFromEvent(VDATA *vdat) @@ -129,7 +132,7 @@ void CXI_SCROLLIMAGE::Draw(bool bSelected, uint32_t Delta_Time) FXYRECT textureRect; for (auto i = 0; i < 4; i++) pV[i].pos.z = 1.f; - pPictureService->GetTexturePos(m_nBorderPicture, textureRect); + pPictureService->BGFXGetTexturePos(m_nBorderPicture, textureRect); pV[0].tu = textureRect.left; pV[0].tv = textureRect.top; pV[1].tu = textureRect.left; @@ -150,9 +153,28 @@ void CXI_SCROLLIMAGE::Draw(bool bSelected, uint32_t Delta_Time) // show select border if (m_bShowBorder /*&& !m_bLockStatus*/ && m_nShowOrder < 0) { - m_rs->TextureSet(0, m_texBorder); - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONLYONETEX_FVF, 2, pV, sizeof(XI_ONLYONETEX_VERTEX), - "iScrollImages_border"); + auto texture = m_rs->GetBGFXTextureFromID(m_texBorder); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + for (int i = 0; i < 4; i += 4) + { + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + + //m_rs->TextureSet(0, m_texBorder); + //m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONLYONETEX_FVF, 2, pV, sizeof(XI_ONLYONETEX_VERTEX), "iScrollImages_border"); } XI_ONETEX_VERTEX v[4]; @@ -175,7 +197,9 @@ void CXI_SCROLLIMAGE::Draw(bool bSelected, uint32_t Delta_Time) if (m_Image[pScroll->imageNum].ptex[n] != -1) { - m_rs->TextureSet(0, m_Image[pScroll->imageNum].ptex[n]); + auto texture = m_rs->GetBGFXTextureFromID(m_Image[pScroll->imageNum].ptex[n]); + m_rs->GetPrimitiveRenderer()->Texture = texture; + rectTex.left = 0.f; rectTex.top = 0.f; rectTex.right = 1.f; @@ -184,16 +208,21 @@ void CXI_SCROLLIMAGE::Draw(bool bSelected, uint32_t Delta_Time) else if (m_Image[pScroll->imageNum].img[n] != -1) { // get texture rectangle - pPictureService->GetTexturePos(m_Image[pScroll->imageNum].img[n], rectTex); - m_rs->TextureSet(0, m_nGroupTex[m_Image[pScroll->imageNum].tex[n]]); + pPictureService->BGFXGetTexturePos(m_Image[pScroll->imageNum].img[n], rectTex); + + auto texture = m_rs->GetBGFXTextureFromID(m_nGroupTex[m_Image[pScroll->imageNum].tex[n]]); + m_rs->GetPrimitiveRenderer()->Texture = texture; + } else { if (m_idBadPic[n] != -1 && m_idBadTexture[n] != -1) // partial use of texture for a "bad" picture { - m_rs->TextureSet(0, m_nGroupTex[m_idBadTexture[n]]); - pPictureService->GetTexturePos(m_idBadPic[n], rectTex); + auto texture = m_rs->GetBGFXTextureFromID(m_nGroupTex[m_idBadTexture[n]]); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + pPictureService->BGFXGetTexturePos(m_idBadPic[n], rectTex); } else // "bad" picture for the whole texture { @@ -251,13 +280,59 @@ void CXI_SCROLLIMAGE::Draw(bool bSelected, uint32_t Delta_Time) v[0].color = v[1].color = v[2].color = v[3].color = m_dwNormalColor[n]; if (m_Image[pScroll->imageNum].bUseSpecTechnique[n]) { - m_rs->SetRenderState(D3DRS_TEXTUREFACTOR, m_dwSpecTechniqueARGB); - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, v, sizeof(XI_ONETEX_VERTEX), - m_sSpecTechniqueName); + //m_rs->SetRenderState(D3DRS_TEXTUREFACTOR, m_dwSpecTechniqueARGB); + + + for (int i = 0; i < 4; i += 4) + { + std::vector vertices; + + auto pVertices = v; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[i + 0].pos.x, pVertices[i + 0].pos.y, + pVertices[i + 0].pos.z, pVertices[i + 0].tu, + pVertices[i + 0].tv, pVertices[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[i + 2].pos.x, pVertices[i + 2].pos.y, + pVertices[i + 2].pos.z, pVertices[i + 2].tu, + pVertices[i + 2].tv, pVertices[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[i + 1].pos.x, pVertices[i + 1].pos.y, + pVertices[i + 1].pos.z, pVertices[i + 1].tu, + pVertices[i + 1].tv, pVertices[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[i + 3].pos.x, pVertices[i + 3].pos.y, + pVertices[i + 3].pos.z, pVertices[i + 3].tu, + pVertices[i + 3].tv, pVertices[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + //m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, v, sizeof(XI_ONETEX_VERTEX), m_sSpecTechniqueName); } else - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, v, sizeof(XI_ONETEX_VERTEX), - "iScrollImages_main"); + { + for (int i = 0; i < 4; i += 4) + { + std::vector vertices; + + auto pVertices = v; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[i + 0].pos.x, pVertices[i + 0].pos.y, + pVertices[i + 0].pos.z, pVertices[i + 0].tu, + pVertices[i + 0].tv, pVertices[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[i + 2].pos.x, pVertices[i + 2].pos.y, + pVertices[i + 2].pos.z, pVertices[i + 2].tu, + pVertices[i + 2].tv, pVertices[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[i + 1].pos.x, pVertices[i + 1].pos.y, + pVertices[i + 1].pos.z, pVertices[i + 1].tu, + pVertices[i + 1].tv, pVertices[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[i + 3].pos.x, pVertices[i + 3].pos.y, + pVertices[i + 3].pos.z, pVertices[i + 3].tu, + pVertices[i + 3].tv, pVertices[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + //m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, v, sizeof(XI_ONETEX_VERTEX), "iScrollImages_main"); + + } pScroll = pScroll->next; } @@ -501,7 +576,7 @@ void CXI_SCROLLIMAGE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, c throw std::runtime_error("allocate memory error"); } memcpy(m_sGroupName[i], stmp, len); - m_nGroupTex[i] = pPictureService->GetTextureID(m_sGroupName[i]); + m_nGroupTex[i] = pPictureService->BGFXGetTextureID(m_sGroupName[i]); } } } @@ -513,7 +588,7 @@ void CXI_SCROLLIMAGE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, c sprintf_s(param, "BadPicture%d", n + 1); if ((sBadPict = pAttribute->GetAttribute(param)) != nullptr) { - m_idBadTexture[n] = m_rs->TextureCreate(sBadPict); + m_idBadTexture[n] = m_rs->BGFXTextureCreate(sBadPict); m_idBadPic[n] = -1; } else @@ -523,8 +598,8 @@ void CXI_SCROLLIMAGE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, c if (m_idBadTexture[n] >= 0) { sprintf_s(param, "BadPic%d", n + 1); - m_idBadPic[n] = - pPictureService->GetImageNum(m_sGroupName[m_idBadTexture[n]], pAttribute->GetAttribute(param)); + m_idBadPic[n] = pPictureService->BGFXGetImageNum(m_sGroupName[m_idBadTexture[n]], + pAttribute->GetAttribute(param)); } else m_idBadPic[n] = -1; @@ -628,7 +703,7 @@ void CXI_SCROLLIMAGE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, c m_Image[i].tex[n] = pListEntity->GetAttributeAsDword(param, -1); sprintf_s(param, "img%d", n + 1); if (m_Image[i].tex[n] != -1) - m_Image[i].img[n] = pPictureService->GetImageNum(m_sGroupName[m_Image[i].tex[n]], + m_Image[i].img[n] = pPictureService->BGFXGetImageNum(m_sGroupName[m_Image[i].tex[n]], pListEntity->GetAttribute(param)); else m_Image[i].img[n] = -1; @@ -648,8 +723,8 @@ void CXI_SCROLLIMAGE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, c if ((m_sBorderGroupName = new char[len]) == nullptr) throw std::runtime_error("allocate memory error"); memcpy(m_sBorderGroupName, param1, len); - m_texBorder = pPictureService->GetTextureID(m_sBorderGroupName); - m_nBorderPicture = pPictureService->GetImageNum(m_sBorderGroupName, tmpstr); + m_texBorder = pPictureService->BGFXGetTextureID(m_sBorderGroupName); + m_nBorderPicture = pPictureService->BGFXGetImageNum(m_sBorderGroupName, tmpstr); m_bShowBorder = m_texBorder != -1; } else @@ -879,7 +954,7 @@ void CXI_SCROLLIMAGE::ReleaseAll() { if (m_idBadPic[i] == -1) { - TEXTURE_RELEASE(m_rs, m_idBadTexture[i]); + BGFX_TEXTURE_RELEASE(m_rs, m_idBadTexture[i]); } else m_idBadTexture[i] = -1; @@ -905,13 +980,13 @@ void CXI_SCROLLIMAGE::ReleaseAll() for (i = 0; i < m_nGroupQuantity; i++) { - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName[i], m_nGroupTex[i]); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName[i], m_nGroupTex[i]); STORM_DELETE(m_sGroupName[i]); } STORM_DELETE(m_sGroupName); STORM_DELETE(m_nGroupTex); - PICTURE_TEXTURE_RELEASE(pPictureService, m_sBorderGroupName, m_texBorder); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sBorderGroupName, m_texBorder); STORM_DELETE(m_sBorderGroupName); FONT_RELEASE(m_rs, m_nOneStrFont); @@ -1130,7 +1205,7 @@ void CXI_SCROLLIMAGE::ChangeScroll(int nScrollItemNum) m_Image[i].tex[n] = pAttribute->GetAttributeAsDword(param, -1); sprintf_s(param, "img%d", n + 1); if (m_Image[i].tex[n] != -1) - m_Image[i].img[n] = pPictureService->GetImageNum(m_sGroupName[m_Image[i].tex[n]], + m_Image[i].img[n] = pPictureService->BGFXGetImageNum(m_sGroupName[m_Image[i].tex[n]], pAttribute->GetAttribute(param)); else m_Image[i].img[n] = -1; @@ -1201,7 +1276,7 @@ void CXI_SCROLLIMAGE::RefreshScroll() { if (m_idBadPic[i] == -1) { - TEXTURE_RELEASE(m_rs, m_idBadTexture[i]); + BGFX_TEXTURE_RELEASE(m_rs, m_idBadTexture[i]); } else m_idBadTexture[i] = -1; @@ -1225,7 +1300,7 @@ void CXI_SCROLLIMAGE::RefreshScroll() // release old groups for (i = 0; i < m_nGroupQuantity; i++) { - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName[i], m_nGroupTex[i]); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName[i], m_nGroupTex[i]); STORM_DELETE(m_sGroupName[i]); } STORM_DELETE(m_sGroupName); @@ -1293,7 +1368,7 @@ void CXI_SCROLLIMAGE::RefreshScroll() throw std::runtime_error("allocate memory error"); } memcpy(m_sGroupName[i], stmp, len); - m_nGroupTex[i] = pPictureService->GetTextureID(m_sGroupName[i]); + m_nGroupTex[i] = pPictureService->BGFXGetTextureID(m_sGroupName[i]); } } } @@ -1305,7 +1380,7 @@ void CXI_SCROLLIMAGE::RefreshScroll() sprintf_s(param, "BadPicture%d", n + 1); if ((sBadPict = pAttribute->GetAttribute(param)) != nullptr) { - m_idBadTexture[n] = m_rs->TextureCreate(sBadPict); + m_idBadTexture[n] = m_rs->BGFXTextureCreate(sBadPict); m_idBadPic[n] = -1; } else @@ -1315,8 +1390,8 @@ void CXI_SCROLLIMAGE::RefreshScroll() if (m_idBadTexture[n] >= 0) { sprintf_s(param, "BadPic%d", n + 1); - m_idBadPic[n] = - pPictureService->GetImageNum(m_sGroupName[m_idBadTexture[n]], pAttribute->GetAttribute(param)); + m_idBadPic[n] = pPictureService->BGFXGetImageNum(m_sGroupName[m_idBadTexture[n]], + pAttribute->GetAttribute(param)); } else m_idBadPic[n] = -1; @@ -1420,7 +1495,7 @@ void CXI_SCROLLIMAGE::RefreshScroll() m_Image[i].tex[n] = pListEntity->GetAttributeAsDword(param, -1); sprintf_s(param, "img%d", n + 1); if (m_Image[i].tex[n] != -1) - m_Image[i].img[n] = pPictureService->GetImageNum(m_sGroupName[m_Image[i].tex[n]], + m_Image[i].img[n] = pPictureService->BGFXGetImageNum(m_sGroupName[m_Image[i].tex[n]], pListEntity->GetAttribute(param)); else m_Image[i].img[n] = -1; @@ -1654,14 +1729,14 @@ void CXI_SCROLLIMAGE::UpdateTexturesGroup() throw std::runtime_error("allocate memory error"); } memcpy(m_sGroupName[i], stmp, len); - m_nGroupTex[i] = pPictureService->GetTextureID(m_sGroupName[i]); + m_nGroupTex[i] = pPictureService->BGFXGetTextureID(m_sGroupName[i]); } } // delete old groups for (i = 0; i < nPrevQ; i++) { - PICTURE_TEXTURE_RELEASE(pPictureService, pPrevGroup[i], prevTex[i]); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, pPrevGroup[i], prevTex[i]); STORM_DELETE(pPrevGroup[i]); } STORM_DELETE(pPrevGroup); diff --git a/src/libs/Xinterface/Nodes/xi_slideLine.cpp b/src/libs/Xinterface/Nodes/xi_slideLine.cpp index d4ac07cdf..cbf1a4636 100644 --- a/src/libs/Xinterface/Nodes/xi_slideLine.cpp +++ b/src/libs/Xinterface/Nodes/xi_slideLine.cpp @@ -1,11 +1,13 @@ #include "xi_slideLine.h" +#include "primitive_renderer.h" + + CXI_SLIDELINE::CXI_SLIDELINE() { m_idTexLine = -1; m_idTexSelLine = -1; m_idTexPointer = -1; - m_idVBuf = -1; m_nNodeType = NODETYPE_SLIDELINE; m_bMouseSelect = true; m_bDoChangeSlider = false; @@ -20,36 +22,91 @@ void CXI_SLIDELINE::Draw(bool bSelected, uint32_t Delta_Time) { if (m_bUse) { - if (m_idVBuf >= 0) + if (m_vertices.size() != 0) { DoMouseControl(); - uint32_t dwOldTF; + /*uint32_t dwOldTF; m_rs->GetRenderState(D3DRS_TEXTUREFACTOR, &dwOldTF); if (m_bSelected) m_rs->SetRenderState(D3DRS_TEXTUREFACTOR, 0xFF808080); else - m_rs->SetRenderState(D3DRS_TEXTUREFACTOR, m_dwDisableColor); + m_rs->SetRenderState(D3DRS_TEXTUREFACTOR, m_dwDisableColor);*/ if (m_idTexLine >= 0 && m_idTexSelLine >= 0) { if (bSelected) - m_rs->TextureSet(0, m_idTexSelLine); + { + + //m_rs->TextureSet(0, m_idTexSelLine); + auto texture = m_rs->GetBGFXTextureFromID(m_idTexSelLine); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + } + else - m_rs->TextureSet(0, m_idTexLine); - m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONLYONETEX_VERTEX), 0, 2, - "iBlindPictures"); + { + + //m_rs->TextureSet(0, m_idTexLine); + auto texture = m_rs->GetBGFXTextureFromID(m_idTexLine); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + } + + for (int i = 0; i < 4; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + //m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONLYONETEX_VERTEX), 0, 2, "iBlindPictures"); } if (m_idTexPointer >= 0) { - m_rs->TextureSet(0, m_idTexPointer); - m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONLYONETEX_VERTEX), 4, 2, - "iBlindPictures"); + //m_rs->TextureSet(0, m_idTexPointer); + + auto texture = m_rs->GetBGFXTextureFromID(m_idTexPointer); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + + for (int i = 4; i < 8; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + + + //m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONLYONETEX_VERTEX), 4, 2, "iBlindPictures"); } - m_rs->SetRenderState(D3DRS_TEXTUREFACTOR, dwOldTF); + //m_rs->SetRenderState(D3DRS_TEXTUREFACTOR, dwOldTF); } } } @@ -64,10 +121,9 @@ bool CXI_SLIDELINE::Init(INIFILE *ini1, const char *name1, INIFILE *ini2, const void CXI_SLIDELINE::ReleaseAll() { - TEXTURE_RELEASE(m_rs, m_idTexLine); - TEXTURE_RELEASE(m_rs, m_idTexSelLine); - TEXTURE_RELEASE(m_rs, m_idTexPointer); - VERTEX_BUFFER_RELEASE(m_rs, m_idVBuf); + BGFX_TEXTURE_RELEASE(m_rs, m_idTexLine); + BGFX_TEXTURE_RELEASE(m_rs, m_idTexSelLine); + BGFX_TEXTURE_RELEASE(m_rs, m_idTexPointer); } int CXI_SLIDELINE::CommandExecute(int wActCode) @@ -117,7 +173,7 @@ void CXI_SLIDELINE::ChangePosition(XYRECT &rNewPos) { m_rect = rNewPos; - auto *const pv = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); + auto *const pv = m_vertices.data(); if (pv) { pv[0].pos.x = pv[1].pos.x = static_cast(m_rect.left); @@ -134,7 +190,6 @@ void CXI_SLIDELINE::ChangePosition(XYRECT &rNewPos) pv[4].pos.y = pv[6].pos.y = static_cast(m_rect.top + m_rect.bottom - m_nPointerHeight) / 2.f; pv[5].pos.y = pv[7].pos.y = static_cast(m_rect.top + m_rect.bottom + m_nPointerHeight) / 2.f; - m_rs->UnLockVertexBuffer(m_idVBuf); } } @@ -234,19 +289,18 @@ void CXI_SLIDELINE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, con m_idTexLine = -1; if (ReadIniString(ini1, name1, ini2, name2, "baseTexture", param, sizeof(param), "")) - m_idTexLine = m_rs->TextureCreate(param); + m_idTexLine = m_rs->BGFXTextureCreate(param); m_idTexSelLine = -1; if (ReadIniString(ini1, name1, ini2, name2, "selectTexture", param, sizeof(param), "")) - m_idTexSelLine = m_rs->TextureCreate(param); + m_idTexSelLine = m_rs->BGFXTextureCreate(param); m_idTexPointer = -1; if (ReadIniString(ini1, name1, ini2, name2, "pointerTexture", param, sizeof(param), "")) - m_idTexPointer = m_rs->TextureCreate(param); + m_idTexPointer = m_rs->BGFXTextureCreate(param); - m_idVBuf = m_rs->CreateVertexBuffer(XI_ONLYONETEX_FVF, 8 * sizeof(XI_ONLYONETEX_VERTEX), D3DUSAGE_WRITEONLY); - if (m_idVBuf == -1) - throw std::runtime_error("can not create the vertex buffers"); + m_vertices.reserve(8); + m_vertices.resize(8); m_nPointerWidth = GetIniLong(ini1, name1, ini2, name2, "pointerWidth", 8); m_nPointerHeight = GetIniLong(ini1, name1, ini2, name2, "pointerHeight", m_rect.bottom - m_rect.top); @@ -272,7 +326,7 @@ void CXI_SLIDELINE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, con m_dwDisableColor = GetIniARGB(ini1, name1, ini2, name2, "disablecolor", 0xA04C4C4C); - auto *pv = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); + auto *pv = m_vertices.data(); if (pv) { for (i = 0; i < 8; i++) @@ -297,7 +351,6 @@ void CXI_SLIDELINE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, con pv[4].pos.y = pv[6].pos.y = static_cast(m_rect.top + m_rect.bottom - m_nPointerHeight) / 2.f; pv[5].pos.y = pv[7].pos.y = static_cast(m_rect.top + m_rect.bottom + m_nPointerHeight) / 2.f; - m_rs->UnLockVertexBuffer(m_idVBuf); } m_bSelected = true; @@ -317,12 +370,11 @@ void CXI_SLIDELINE::SetNewValue(long newValue) const auto right = static_cast(m_rect.right - m_nBaseLeft + m_nPointerLeft - m_nPointerWidth); left = left + (right - left) / m_nGrateQuantity * m_nCurValue; - auto *pv = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); + auto *pv = m_vertices.data(); if (pv) { pv[4].pos.x = pv[5].pos.x = left; pv[6].pos.x = pv[7].pos.x = left + m_nPointerWidth; - m_rs->UnLockVertexBuffer(m_idVBuf); } ATTRIBUTES *pA = core.Entity_GetAttributeClass(g_idInterface, "nodes"); diff --git a/src/libs/Xinterface/Nodes/xi_slideLine.h b/src/libs/Xinterface/Nodes/xi_slideLine.h index 647f9d86d..ede07f648 100644 --- a/src/libs/Xinterface/Nodes/xi_slideLine.h +++ b/src/libs/Xinterface/Nodes/xi_slideLine.h @@ -29,7 +29,7 @@ class CXI_SLIDELINE : public CINODE long m_idTexLine; long m_idTexSelLine; long m_idTexPointer; - long m_idVBuf; + std::vector m_vertices; uint32_t m_dwDisableColor; diff --git a/src/libs/Xinterface/Nodes/xi_slidepicture.cpp b/src/libs/Xinterface/Nodes/xi_slidepicture.cpp index 389eb30a8..7fe9e116e 100644 --- a/src/libs/Xinterface/Nodes/xi_slidepicture.cpp +++ b/src/libs/Xinterface/Nodes/xi_slidepicture.cpp @@ -1,6 +1,9 @@ #include "xi_slidepicture.h" #include +#include "primitive_renderer.h" + + void SetTextureCoordinate(XI_ONETEX_VERTEX v[4], FXYRECT tr, float angle) { if (angle == 0) @@ -54,14 +57,58 @@ void CXI_SLIDEPICTURE::Draw(bool bSelected, uint32_t Delta_Time) if (m_bUse) { Update(Delta_Time); - m_rs->TextureSet(0, m_idTex); - m_rs->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); - m_rs->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); + //m_rs->TextureSet(0, m_idTex); + //m_rs->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); + //m_rs->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); + + auto texture = m_rs->GetBGFXTextureFromID(m_idTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + if (strTechniqueName == nullptr) - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_v, sizeof(XI_ONETEX_VERTEX), "iVideo"); + { + for (int i = 0; i < 4; i += 4) + { + std::vector vertices; + + auto pV = m_v; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + //m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_v, sizeof(XI_ONETEX_VERTEX), "iVideo"); + } else - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_v, sizeof(XI_ONETEX_VERTEX), - strTechniqueName); + { + for (int i = 0; i < 4; i += 4) + { + std::vector vertices; + + auto pV = m_v; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + //m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_v, sizeof(XI_ONETEX_VERTEX), strTechniqueName); + } + } } diff --git a/src/libs/Xinterface/Nodes/xi_statusline.cpp b/src/libs/Xinterface/Nodes/xi_statusline.cpp index e8ddee4c1..5f528c746 100644 --- a/src/libs/Xinterface/Nodes/xi_statusline.cpp +++ b/src/libs/Xinterface/Nodes/xi_statusline.cpp @@ -3,15 +3,15 @@ #include "core.h" +#include "primitive_renderer.h" + + CXI_STATUSLINE::CXI_STATUSLINE() { m_rs = nullptr; m_sGroupName = nullptr; m_idTex = -1L; - m_vBuf = -1L; - m_iBuf = -1L; m_nVert = 0; - m_nIndx = 0; m_nNodeType = NODETYPE_STATUSLINE; } @@ -24,8 +24,29 @@ void CXI_STATUSLINE::Draw(bool bSelected, uint32_t Delta_Time) { if (m_bUse) { - m_rs->TextureSet(0, m_idTex); - m_rs->DrawBuffer(m_vBuf, sizeof(XI_ONLYONETEX_VERTEX), m_iBuf, 0, m_nVert, 0, m_nIndx, "iStatusLine"); + auto texture = m_rs->GetBGFXTextureFromID(m_idTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + for (int i = 0; i < m_nVert; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv}); + vertices.push_back(VERTEX_POSITION_TEXTURE{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + //m_rs->TextureSet(0, m_idTex); + //m_rs->DrawBuffer(m_vBuf, sizeof(XI_ONLYONETEX_VERTEX), m_iBuf, 0, m_nVert, 0, m_nIndx, "iStatusLine"); } } @@ -40,9 +61,7 @@ bool CXI_STATUSLINE::Init(INIFILE *ini1, const char *name1, INIFILE *ini2, const void CXI_STATUSLINE::ReleaseAll() { - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); - VERTEX_BUFFER_RELEASE(m_rs, m_vBuf); - INDEX_BUFFER_RELEASE(m_rs, m_iBuf); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); STORM_DELETE(m_sGroupName); } @@ -61,22 +80,19 @@ void CXI_STATUSLINE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co const auto len = strlen(param) + 1; m_sGroupName = new char[len]; memcpy(m_sGroupName, param, len); - m_idTex = pPictureService->GetTextureID(m_sGroupName); + m_idTex = pPictureService->BGFXGetTextureID(m_sGroupName); } // Calculate vertex and index quantity m_nVert = 2 * 4; - m_nIndx = 2 * 6; // Create vertex and index buffers - m_vBuf = m_rs->CreateVertexBuffer(XI_ONLYONETEX_FVF, m_nVert * sizeof(XI_ONLYONETEX_VERTEX), D3DUSAGE_WRITEONLY); - m_iBuf = m_rs->CreateIndexBuffer(m_nIndx * 2); - m_nIndx /= 3; + m_vertices.reserve(m_nVert); + m_vertices.resize(m_nVert); // Lock vertex and index buffers and get pointers to this - auto *const pVBuf = static_cast(m_rs->LockVertexBuffer(m_vBuf)); - auto *pIBuf = static_cast(m_rs->LockIndexBuffer(m_iBuf)); + auto *const pVBuf = m_vertices.data(); - if (pVBuf != nullptr && pIBuf != nullptr) + if (pVBuf != nullptr) { FXYRECT scrRect1, scrRect2; @@ -107,27 +123,17 @@ void CXI_STATUSLINE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co // get texture coordinates fMediumX /= (m_rect.right - m_rect.left); if (ReadIniString(ini1, name1, ini2, name2, "filledPicture", param, sizeof(param), "")) - pPictureService->GetTexturePos(m_sGroupName, param, m_texRect1); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, m_texRect1); FXYRECT texRect1; memcpy(&texRect1, &m_texRect1, sizeof(FRECT)); texRect1.right = m_texRect1.left + (m_texRect1.right - m_texRect1.left) * fMediumX; // .. other .. if (ReadIniString(ini1, name1, ini2, name2, "emptyPicture", param, sizeof(param), "")) - pPictureService->GetTexturePos(m_sGroupName, param, m_texRect2); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, m_texRect2); FXYRECT texRect2; memcpy(&texRect2, &m_texRect2, sizeof(FRECT)); texRect2.left = m_texRect2.left + (m_texRect2.right - m_texRect2.left) * fMediumX; - // fill index buffer - pIBuf[0] = 0; - pIBuf[4] = pIBuf[1] = 1; - pIBuf[3] = pIBuf[2] = 2; - pIBuf[5] = 3; // filled rectangle - pIBuf[6] = 4; - pIBuf[10] = pIBuf[7] = 5; - pIBuf[9] = pIBuf[8] = 6; - pIBuf[11] = 7; // empty rectangle - // fill vertex buffer for (auto i = 0; i < m_nVert; i++) pVBuf[i].pos.z = 1.f; @@ -168,11 +174,6 @@ void CXI_STATUSLINE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co } else throw std::runtime_error("Can't vertex or index buffer create"); - - if (pVBuf != nullptr) - m_rs->UnLockVertexBuffer(m_vBuf); - if (pIBuf != nullptr) - m_rs->UnLockIndexBuffer(m_iBuf); } bool CXI_STATUSLINE::IsClick(int buttonID, long xPos, long yPos) @@ -213,11 +214,12 @@ uint32_t CXI_STATUSLINE::MessageProc(long msgcode, MESSAGE &message) return 0; } -void CXI_STATUSLINE::Refresh() const +void CXI_STATUSLINE::Refresh() { - if (m_vBuf == -1) + if (m_vertices.size() == 0) return; - auto *pVBuf = static_cast(m_rs->LockVertexBuffer(m_vBuf)); + + auto *pVBuf = m_vertices.data(); auto *pAttr = core.Entity_GetAttributeClass(g_idInterface, "StatusLine"); if (pAttr != nullptr) @@ -284,7 +286,4 @@ void CXI_STATUSLINE::Refresh() const pVBuf[7].tu = texRect2.right; pVBuf[7].tv = texRect2.bottom; } - - if (pVBuf != nullptr) - m_rs->UnLockVertexBuffer(m_vBuf); } diff --git a/src/libs/Xinterface/Nodes/xi_statusline.h b/src/libs/Xinterface/Nodes/xi_statusline.h index 0ad6e190e..487315d52 100644 --- a/src/libs/Xinterface/Nodes/xi_statusline.h +++ b/src/libs/Xinterface/Nodes/xi_statusline.h @@ -26,15 +26,13 @@ class CXI_STATUSLINE : public CINODE protected: void LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const char *name2) override; - void Refresh() const; + void Refresh(); protected: char *m_sGroupName; // image list name long m_idTex; // texture identificator - long m_vBuf; // vertex buffer identificator - long m_iBuf; // index buffer identificator + std::vector m_vertices; long m_nVert; // vertex quantity - long m_nIndx; // index quantity float m_fLineOffset; // edge offset for frame FXYRECT m_texRect1; FXYRECT m_texRect2; diff --git a/src/libs/Xinterface/Nodes/xi_table.cpp b/src/libs/Xinterface/Nodes/xi_table.cpp index 640867508..a0f35977a 100644 --- a/src/libs/Xinterface/Nodes/xi_table.cpp +++ b/src/libs/Xinterface/Nodes/xi_table.cpp @@ -4,6 +4,8 @@ #include "xi_util.h" #include +#include "primitive_renderer.h" + #define ALIGN_BOTTOM 16 #define ALIGN_TOP 17 #define NOTUSE_OFFSET -1000.f @@ -56,7 +58,22 @@ void XI_TableLineDescribe::DrawSpecColor(float fTop) const v[2].pos.y = fTop; v[3].pos.x = fRight; v[3].pos.y = fBottom; - m_pTable->m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_NOTEX_FVF, 2, v, sizeof(XI_NOTEX_VERTEX), "iRectangle"); + + for (int i = 0; i < 4; i += 4) + { + std::vector vertices; + + auto pVertices = v; + + vertices.push_back(VERTEX_POSITION_COLOR{pVertices[i + 0].pos.x, pVertices[i + 0].pos.y, pVertices[i + 0].pos.z, pVertices[i + 0].color}); + vertices.push_back(VERTEX_POSITION_COLOR{pVertices[i + 2].pos.x, pVertices[i + 2].pos.y, pVertices[i + 2].pos.z, pVertices[i + 1].color}); + vertices.push_back(VERTEX_POSITION_COLOR{pVertices[i + 1].pos.x, pVertices[i + 1].pos.y, pVertices[i + 1].pos.z, pVertices[i + 1].color}); + vertices.push_back(VERTEX_POSITION_COLOR{pVertices[i + 3].pos.x, pVertices[i + 3].pos.y, pVertices[i + 3].pos.z, pVertices[i + 3].color}); + + m_pTable->m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + //m_pTable->m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_NOTEX_FVF, 2, v, sizeof(XI_NOTEX_VERTEX), "iRectangle"); } } @@ -363,8 +380,6 @@ CXI_TABLE::CXI_TABLE() m_bBackPresent = false; m_idBorderTexture = -1; - m_idBorderVBuf = -1; - m_idBorderIBuf = -1; m_nBorderSubQ = 0; m_EditData.bAllEditable = true; @@ -417,11 +432,33 @@ void CXI_TABLE::Draw(bool bSelected, uint32_t Delta_Time) m_SelectImg.Draw(); // Drawing the frame - if (m_idBorderTexture != -1 && m_idBorderVBuf != -1 && m_idBorderIBuf != -1) + if (m_idBorderTexture != -1) { - m_rs->TextureSet(0, m_idBorderTexture); - m_rs->DrawBuffer(m_idBorderVBuf, sizeof(XI_ONETEX_VERTEX), m_idBorderIBuf, 0, m_nBorderSubQ * 4, 0, - m_nBorderSubQ * 2, "iIcon"); + //m_rs->TextureSet(0, m_idBorderTexture); + + auto texture = m_rs->GetBGFXTextureFromID(m_idBorderTexture); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + for (int i = 0; i < m_nBorderSubQ * 4; i += 4) + { + std::vector vertices; + + auto pV = pVertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + /*m_rs->DrawBuffer(m_idBorderVBuf, sizeof(XI_ONETEX_VERTEX), m_idBorderIBuf, 0, m_nBorderSubQ * 4, 0, + m_nBorderSubQ * 2, "iIcon");*/ } // Line output @@ -460,9 +497,7 @@ void CXI_TABLE::ReleaseAll() m_BackImg.Unload(); // release border data - PICTURE_TEXTURE_RELEASE(pPictureService, m_sBorderIconGroupName.c_str(), m_idBorderTexture); - VERTEX_BUFFER_RELEASE(m_rs, m_idBorderVBuf); - INDEX_BUFFER_RELEASE(m_rs, m_idBorderIBuf); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sBorderIconGroupName.c_str(), m_idBorderTexture); // release fonts from list FONT_RELEASE(m_rs, m_nFontCellID); @@ -842,48 +877,48 @@ void CXI_TABLE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const c if (ReadIniString(ini1, name1, ini2, name2, "bordericongroup", param, sizeof(param), "")) { m_sBorderIconGroupName = param; - m_idBorderTexture = pPictureService->GetTextureID(param); + m_idBorderTexture = pPictureService->BGFXGetTextureID(param); } // m_nBorderIcon_LeftTop = -1; if (ReadIniString(ini1, name1, ini2, name2, "borderlefttop", param, sizeof(param), "")) - m_nBorderIcon_LeftTop = pPictureService->GetImageNum(m_sBorderIconGroupName.c_str(), param); + m_nBorderIcon_LeftTop = pPictureService->BGFXGetImageNum(m_sBorderIconGroupName.c_str(), param); // m_nBorderIcon_LeftBottom = -1; if (ReadIniString(ini1, name1, ini2, name2, "borderleftbottom", param, sizeof(param), "")) - m_nBorderIcon_LeftBottom = pPictureService->GetImageNum(m_sBorderIconGroupName.c_str(), param); + m_nBorderIcon_LeftBottom = pPictureService->BGFXGetImageNum(m_sBorderIconGroupName.c_str(), param); // m_nBorderIcon_RightTop = -1; if (ReadIniString(ini1, name1, ini2, name2, "borderrighttop", param, sizeof(param), "")) - m_nBorderIcon_RightTop = pPictureService->GetImageNum(m_sBorderIconGroupName.c_str(), param); + m_nBorderIcon_RightTop = pPictureService->BGFXGetImageNum(m_sBorderIconGroupName.c_str(), param); // m_nBorderIcon_RightBottom = -1; if (ReadIniString(ini1, name1, ini2, name2, "borderrightbottom", param, sizeof(param), "")) - m_nBorderIcon_RightBottom = pPictureService->GetImageNum(m_sBorderIconGroupName.c_str(), param); + m_nBorderIcon_RightBottom = pPictureService->BGFXGetImageNum(m_sBorderIconGroupName.c_str(), param); // m_nBorderIcon_Left = -1; if (ReadIniString(ini1, name1, ini2, name2, "borderleft", param, sizeof(param), "")) - m_nBorderIcon_Left = pPictureService->GetImageNum(m_sBorderIconGroupName.c_str(), param); + m_nBorderIcon_Left = pPictureService->BGFXGetImageNum(m_sBorderIconGroupName.c_str(), param); // m_nBorderIcon_Right = -1; if (ReadIniString(ini1, name1, ini2, name2, "borderright", param, sizeof(param), "")) - m_nBorderIcon_Right = pPictureService->GetImageNum(m_sBorderIconGroupName.c_str(), param); + m_nBorderIcon_Right = pPictureService->BGFXGetImageNum(m_sBorderIconGroupName.c_str(), param); // m_nBorderIcon_Top = -1; if (ReadIniString(ini1, name1, ini2, name2, "bordertop", param, sizeof(param), "")) - m_nBorderIcon_Top = pPictureService->GetImageNum(m_sBorderIconGroupName.c_str(), param); + m_nBorderIcon_Top = pPictureService->BGFXGetImageNum(m_sBorderIconGroupName.c_str(), param); // m_nBorderIcon_Bottom = -1; if (ReadIniString(ini1, name1, ini2, name2, "borderbottom", param, sizeof(param), "")) - m_nBorderIcon_Bottom = pPictureService->GetImageNum(m_sBorderIconGroupName.c_str(), param); + m_nBorderIcon_Bottom = pPictureService->BGFXGetImageNum(m_sBorderIconGroupName.c_str(), param); // m_nBorderIcon_VLine = -1; if (ReadIniString(ini1, name1, ini2, name2, "bordervline", param, sizeof(param), "")) - m_nBorderIcon_VLine = pPictureService->GetImageNum(m_sBorderIconGroupName.c_str(), param); + m_nBorderIcon_VLine = pPictureService->BGFXGetImageNum(m_sBorderIconGroupName.c_str(), param); // m_nBorderIcon_HLine = -1; if (ReadIniString(ini1, name1, ini2, name2, "borderhline", param, sizeof(param), "")) - m_nBorderIcon_HLine = pPictureService->GetImageNum(m_sBorderIconGroupName.c_str(), param); + m_nBorderIcon_HLine = pPictureService->BGFXGetImageNum(m_sBorderIconGroupName.c_str(), param); m_dwBorderColor = GetIniARGB(ini1, name1, ini2, name2, "bordercolor", ARGB(255, 128, 128, 128)); m_nBorderWidth = GetIniLong(ini1, name1, ini2, name2, "borderwidth", 0); @@ -1013,32 +1048,14 @@ void CXI_TABLE::UpdateBorders() if (m_nBorderSubQ != q) // rearranging buffers { m_nBorderSubQ = q; - VERTEX_BUFFER_RELEASE(m_rs, m_idBorderVBuf); - INDEX_BUFFER_RELEASE(m_rs, m_idBorderIBuf); - - // index buffer - m_idBorderIBuf = m_rs->CreateIndexBuffer(q * 6 * sizeof(uint16_t)); - Assert(m_idBorderIBuf != -1); - // fill in - auto *pT = static_cast(m_rs->LockIndexBuffer(m_idBorderIBuf)); - for (n = 0; n < q; n++) - { - pT[n * 6 + 0] = static_cast(n * 4 + 0); - pT[n * 6 + 1] = static_cast(n * 4 + 1); - pT[n * 6 + 2] = static_cast(n * 4 + 2); - pT[n * 6 + 3] = static_cast(n * 4 + 3); - pT[n * 6 + 4] = static_cast(n * 4 + 1); - pT[n * 6 + 5] = static_cast(n * 4 + 2); - } - m_rs->UnLockIndexBuffer(m_idBorderIBuf); + pVertices.clear(); // vertex buffer - m_idBorderVBuf = m_rs->CreateVertexBuffer(XI_ONETEX_FVF, q * 4 * sizeof(XI_ONETEX_VERTEX), D3DUSAGE_WRITEONLY); - Assert(m_idBorderVBuf != -1); + pVertices.resize(q * 4); } // fill the vertex buffer - auto *pV = static_cast(m_rs->LockVertexBuffer(m_idBorderVBuf)); + auto pV = pVertices.data(); // horizontal lines nTop = m_rect.top; for (r = 0, n = 0; r < m_nRowQuantity - 1; r++) @@ -1105,8 +1122,6 @@ void CXI_TABLE::UpdateBorders() m_rect.top + m_pntBorderCornerSize.y, m_nBorderWidth, m_rect.bottom - m_rect.top - 2 * m_pntBorderCornerSize.y); n += 4; - // finish - m_rs->UnLockVertexBuffer(m_idBorderVBuf); } void CXI_TABLE::WriteSquare(XI_ONETEX_VERTEX *pV, long nImgID, uint32_t dwCol, long nX, long nY, long nW, long nH) const @@ -1115,7 +1130,7 @@ void CXI_TABLE::WriteSquare(XI_ONETEX_VERTEX *pV, long nImgID, uint32_t dwCol, l return; FXYRECT uv; - pPictureService->GetTexturePos(nImgID, uv); + pPictureService->BGFXGetTexturePos(nImgID, uv); pV[0].color = dwCol; pV[0].pos.x = static_cast(nX); diff --git a/src/libs/Xinterface/Nodes/xi_table.h b/src/libs/Xinterface/Nodes/xi_table.h index ffd79506c..3ed89a218 100644 --- a/src/libs/Xinterface/Nodes/xi_table.h +++ b/src/libs/Xinterface/Nodes/xi_table.h @@ -192,8 +192,9 @@ class CXI_TABLE : public CINODE std::string m_sBorderIconGroupName; long m_idBorderTexture; - long m_idBorderVBuf; - long m_idBorderIBuf; + + std::vector pVertices; + long m_nBorderSubQ; long m_nBorderIcon_LeftTop; diff --git a/src/libs/Xinterface/Nodes/xi_textbutton.cpp b/src/libs/Xinterface/Nodes/xi_textbutton.cpp index d0df63455..2c8a8fb53 100644 --- a/src/libs/Xinterface/Nodes/xi_textbutton.cpp +++ b/src/libs/Xinterface/Nodes/xi_textbutton.cpp @@ -1,5 +1,7 @@ #include "xi_textbutton.h" +#include "primitive_renderer.h" + CXI_TEXTBUTTON::CXI_TEXTBUTTON() { m_rs = nullptr; @@ -8,9 +10,6 @@ CXI_TEXTBUTTON::CXI_TEXTBUTTON() m_idTex = -1; m_idShadowTex = -1; - m_idVBuf = -1; - m_idIBuf = -1; - m_fXShadow = 0.f; m_fYShadow = 0.f; @@ -46,18 +45,18 @@ void CXI_TEXTBUTTON::Draw(bool bSelected, uint32_t Delta_Time) { if (bSelected ^ m_bCurrentSelected) { - auto *pVert = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); - if (pVert != nullptr) + if (m_vertices.data() != nullptr) { + auto pVert = m_vertices.data(); FXYRECT texRect; m_bCurrentSelected = bSelected; if (m_idSelectMiddle != -1) { if (bSelected) - pPictureService->GetTexturePos(m_idSelectMiddle, texRect); + pPictureService->BGFXGetTexturePos(m_idSelectMiddle, texRect); else - pPictureService->GetTexturePos(m_idUnSelectMiddle, texRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectMiddle, texRect); pVert[4].tu = pVert[16].tu = pVert[28].tu = pVert[40].tu = texRect.left; pVert[4].tv = pVert[16].tv = pVert[28].tv = pVert[40].tv = texRect.top; @@ -72,9 +71,9 @@ void CXI_TEXTBUTTON::Draw(bool bSelected, uint32_t Delta_Time) if (m_idSelectLeft != -1) { if (bSelected) - pPictureService->GetTexturePos(m_idSelectLeft, texRect); + pPictureService->BGFXGetTexturePos(m_idSelectLeft, texRect); else - pPictureService->GetTexturePos(m_idUnSelectLeft, texRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectLeft, texRect); pVert[0].tu = pVert[12].tu = pVert[24].tu = pVert[36].tu = texRect.left; pVert[0].tv = pVert[12].tv = pVert[24].tv = pVert[36].tv = texRect.top; @@ -91,16 +90,16 @@ void CXI_TEXTBUTTON::Draw(bool bSelected, uint32_t Delta_Time) if (bSelected) { if (m_idSelectRight != -1) - pPictureService->GetTexturePos(m_idSelectRight, texRect); + pPictureService->BGFXGetTexturePos(m_idSelectRight, texRect); else - pPictureService->GetTexturePos(TEXTURE_MODIFY_HORZFLIP, m_idSelectLeft, texRect); + pPictureService->BGFXGetTexturePos(TEXTURE_MODIFY_HORZFLIP, m_idSelectLeft, texRect); } else { if (m_idUnSelectRight != -1) - pPictureService->GetTexturePos(m_idUnSelectRight, texRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectRight, texRect); else - pPictureService->GetTexturePos(TEXTURE_MODIFY_HORZFLIP, m_idUnSelectLeft, texRect); + pPictureService->BGFXGetTexturePos(TEXTURE_MODIFY_HORZFLIP, m_idUnSelectLeft, texRect); } pVert[8].tu = pVert[20].tu = pVert[32].tu = pVert[44].tu = texRect.left; @@ -113,26 +112,117 @@ void CXI_TEXTBUTTON::Draw(bool bSelected, uint32_t Delta_Time) pVert[11].tv = pVert[23].tv = pVert[35].tv = pVert[47].tv = texRect.bottom; } - m_rs->UnLockVertexBuffer(m_idVBuf); } } // show shadow if (m_idShadowTex >= 0) { - m_rs->TextureSet(0, m_idShadowTex); + //m_rs->TextureSet(0, m_idShadowTex); + + auto texture = m_rs->GetBGFXTextureFromID(m_idShadowTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + if (m_nPressedDelay > 0) - m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONETEX_VERTEX), 4 * 3 * 4, 2, "iIcon"); + { + for (int i = 0; i < 4 * 3 * 4; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, + pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, + pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + + } + //m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONETEX_VERTEX), 4 * 3 * 4, 2, "iIcon"); + } + else - m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONETEX_VERTEX), 4 * 3 * 4 + 4, 2, "iIcon"); + { + for (int i = 0; i < 4 * 3 * 4 + 4; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + //m_rs->DrawPrimitive(D3DPT_TRIANGLESTRIP, m_idVBuf, sizeof(XI_ONETEX_VERTEX), 4 * 3 * 4 + 4, 2, "iIcon"); + } + } else { - m_rs->TextureSet(0, m_idTex); + //m_rs->TextureSet(0, m_idTex); + + auto texture = m_rs->GetBGFXTextureFromID(m_idTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + if (m_nPressedDelay > 0) - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 4 * 3 * 3, 4 * 3, 0, m_nIndx, "iShadow"); + { + for (int i = 4 * 3 * 3; i < 4 * 3 * 3 + 4 * 3; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + //m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 4 * 3 * 3, 4 * 3, 0, m_nIndx, "iShadow"); + } + else - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 4 * 3, 4 * 3, 0, m_nIndx, "iShadow"); + { + for (int i = 4 * 3; i < 4 * 3 + 4 * 3; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + //m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 4 * 3, 4 * 3, 0, m_nIndx, "iShadow"); + } + } if (m_bVideoToBack) @@ -140,45 +230,142 @@ void CXI_TEXTBUTTON::Draw(bool bSelected, uint32_t Delta_Time) // show midle video fragment if (bSelected && m_pTex != nullptr) { - m_rs->SetTexture(0, m_pTex->m_pTexture); + /*m_rs->SetTexture(0, m_pTex->m_pTexture); if (m_nPressedDelay > 0) m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, &m_v[4], sizeof(XI_ONETEX_VERTEX), "iVideo"); else m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_v, sizeof(XI_ONETEX_VERTEX), - "iVideo"); + "iVideo");*/ } else if (m_dwBackColor != 0) { - m_rs->SetRenderState(D3DRS_TEXTUREFACTOR, m_dwBackColor); + //m_rs->SetRenderState(D3DRS_TEXTUREFACTOR, m_dwBackColor); if (m_nPressedDelay > 0) - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, &m_v[4], sizeof(XI_ONETEX_VERTEX), - "iTFRectangle"); + { + for (int i = 4; i < 8; i += 4) + { + std::vector vertices; + + auto pV = m_v; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, + pV[i + 0].pos.z, pV[i + 0].tu, pV[i + 0].tv, + pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, + pV[i + 2].pos.z, pV[i + 2].tu, pV[i + 2].tv, + pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, + pV[i + 1].pos.z, pV[i + 1].tu, pV[i + 1].tv, + pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, + pV[i + 3].pos.z, pV[i + 3].tu, pV[i + 3].tv, + pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + /*m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, &m_v[4], sizeof(XI_ONETEX_VERTEX), + "iTFRectangle");*/ + } else - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_v, sizeof(XI_ONETEX_VERTEX), - "iTFRectangle"); + { + for (int i = 0; i < 8; i += 4) + { + std::vector vertices; + + auto pV = m_v; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, + pV[i + 0].pos.z, pV[i + 0].tu, pV[i + 0].tv, + pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, + pV[i + 2].pos.z, pV[i + 2].tu, pV[i + 2].tv, + pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, + pV[i + 1].pos.z, pV[i + 1].tu, pV[i + 1].tv, + pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, + pV[i + 3].pos.z, pV[i + 3].tu, pV[i + 3].tv, + pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + /*m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_v, sizeof(XI_ONETEX_VERTEX), + "iTFRectangle");*/ + } + } } // show button - m_rs->TextureSet(0, m_idTex); + //m_rs->TextureSet(0, m_idTex); + + auto texture = m_rs->GetBGFXTextureFromID(m_idTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + if (m_nPressedDelay > 0) - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 4 * 3 * 2, 4 * 3, 0, m_nIndx, "iTextButton"); + { + + for (int i = 4 * 3; i < 4 * 3 * 2 + 4 * 3; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + //m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 4 * 3 * 2, 4 * 3, 0, m_nIndx, "iTextButton"); + } + else - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 0, 4 * 3, 0, m_nIndx, "iTextButton"); + { + for (int i = 0; i < 4 * 3; i += 4) + { + std::vector vertices; + + auto pV = m_vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } + + + //m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 0, 4 * 3, 0, m_nIndx, "iTextButton"); + } + if (!m_bVideoToBack) { // show midle video fragment if (bSelected && m_pTex != nullptr) { - m_rs->SetTexture(0, m_pTex->m_pTexture); + /*m_rs->SetTexture(0, m_pTex->m_pTexture); if (m_nPressedDelay > 0) m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, &m_v[4], sizeof(XI_ONETEX_VERTEX), "iVideo"); else m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, m_v, sizeof(XI_ONETEX_VERTEX), - "iVideo"); + "iVideo");*/ } } @@ -246,7 +433,7 @@ void CXI_TEXTBUTTON::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co m_idTex = -1; if (ReadIniString(ini1, name1, ini2, name2, "group", param, sizeof(param), "")) { - m_idTex = pPictureService->GetTextureID(param); + m_idTex = pPictureService->BGFXGetTextureID(param); const auto len = strlen(param) + 1; m_sGroupName = new char[len]; if (m_sGroupName == nullptr) @@ -256,7 +443,7 @@ void CXI_TEXTBUTTON::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co m_idShadowTex = -1; if (ReadIniString(ini1, name1, ini2, name2, "ShadowTexture", param, sizeof(param), "")) - m_idShadowTex = m_rs->TextureCreate(param); + m_idShadowTex = m_rs->BGFXTextureCreate(param); FXYRECT frectShadowUV; if (m_idShadowTex >= 0) @@ -298,31 +485,17 @@ void CXI_TEXTBUTTON::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co m_pTex = m_rs->GetVideoTexture(param); // do vertex and index buffer - m_nIndx = 3 * 2 * 3; // 3 rectangle * 2 triangle to rectangle * 3 vertex to triangle m_nVert = 4 * 3 * 2 * 2; // 4 vertex * 3 rectangle * (2=face&shadow) * (2=press¬press) if (m_idShadowTex >= 0) m_nVert += 8; - m_idIBuf = m_rs->CreateIndexBuffer(m_nIndx * 2); - m_idVBuf = m_rs->CreateVertexBuffer(XI_ONETEX_FVF, m_nVert * sizeof(XI_ONETEX_VERTEX), D3DUSAGE_WRITEONLY); - // Lock buffers for write - auto *pVert = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); - auto *pIndx = static_cast(m_rs->LockIndexBuffer(m_idIBuf)); - if (pVert == nullptr || pIndx == nullptr) - throw std::runtime_error("can not create the index&vertex buffers"); + m_vertices.reserve(m_nVert); + m_vertices.resize(m_nVert); // fill triangles buffer auto i = 0; - for (auto tidx = 0; tidx < 3; tidx++) - { - pIndx[i + 0] = tidx * 4; - pIndx[i + 1] = tidx * 4 + 1; - pIndx[i + 2] = tidx * 4 + 2; - pIndx[i + 3] = tidx * 4 + 2; - pIndx[i + 4] = tidx * 4 + 1; - pIndx[i + 5] = tidx * 4 + 3; - i += 6; - } + + auto pVert = m_vertices.data(); // fill vertex buffer for (i = 0; i < m_nVert; i++) @@ -380,11 +553,11 @@ void CXI_TEXTBUTTON::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co // fill left side of button m_idUnSelectLeft = m_idSelectLeft = -1; if (ReadIniString(ini1, name1, ini2, name2, "selectButtonLeft", param, sizeof(param), "")) - m_idSelectLeft = pPictureService->GetImageNum(m_sGroupName, param); + m_idSelectLeft = pPictureService->BGFXGetImageNum(m_sGroupName, param); if (ReadIniString(ini1, name1, ini2, name2, "buttonLeft", param, sizeof(param), "")) - m_idUnSelectLeft = pPictureService->GetImageNum(m_sGroupName, param); - pPictureService->GetTexturePos(m_idUnSelectLeft, texRect); - pPictureService->GetTexturePos(m_idUnSelectLeft, natureRect); + m_idUnSelectLeft = pPictureService->BGFXGetImageNum(m_sGroupName, param); + pPictureService->BGFXGetTexturePos(m_idUnSelectLeft, texRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectLeft, natureRect); auto fLeftMiddle = static_cast(m_rect.left + natureRect.right - natureRect.left); pVert[0].tu = pVert[12].tu = pVert[24].tu = pVert[36].tu = texRect.left; pVert[0].tv = pVert[12].tv = pVert[24].tv = pVert[36].tv = texRect.top; @@ -414,19 +587,19 @@ void CXI_TEXTBUTTON::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co auto fRightMiddle = m_rect.right - (fLeftMiddle - m_rect.left); m_idUnSelectRight = m_idSelectRight = -1; if (ReadIniString(ini1, name1, ini2, name2, "buttonRight", param, sizeof(param), "")) - m_idUnSelectRight = pPictureService->GetImageNum(m_sGroupName, param); + m_idUnSelectRight = pPictureService->BGFXGetImageNum(m_sGroupName, param); if (ReadIniString(ini1, name1, ini2, name2, "selectButtonRight", param, sizeof(param), "")) - m_idSelectRight = pPictureService->GetImageNum(m_sGroupName, param); + m_idSelectRight = pPictureService->BGFXGetImageNum(m_sGroupName, param); if (m_idUnSelectRight != -1) { - pPictureService->GetTexturePos(m_idUnSelectRight, texRect); - pPictureService->GetTexturePos(m_idUnSelectRight, natureRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectRight, texRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectRight, natureRect); } else { - pPictureService->GetTexturePos(TEXTURE_MODIFY_HORZFLIP, m_idUnSelectLeft, texRect); - pPictureService->GetTexturePos(m_idUnSelectLeft, natureRect); + pPictureService->BGFXGetTexturePos(TEXTURE_MODIFY_HORZFLIP, m_idUnSelectLeft, texRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectLeft, natureRect); } fRightMiddle = m_rect.right - static_cast(natureRect.right - natureRect.left); pVert[8].tu = pVert[20].tu = pVert[32].tu = pVert[44].tu = texRect.left; @@ -455,10 +628,10 @@ void CXI_TEXTBUTTON::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co pVert[46].pos.y = pVert[47].pos.y = m_rect.bottom + m_fYDeltaPress + m_fYShadowPress; // fill middle of button if (ReadIniString(ini1, name1, ini2, name2, "buttonMiddle", param, sizeof(param), "")) - m_idUnSelectMiddle = pPictureService->GetImageNum(m_sGroupName, param); + m_idUnSelectMiddle = pPictureService->BGFXGetImageNum(m_sGroupName, param); if (ReadIniString(ini1, name1, ini2, name2, "selectButtonMiddle", param, sizeof(param), "")) - m_idSelectMiddle = pPictureService->GetImageNum(m_sGroupName, param); - pPictureService->GetTexturePos(m_idUnSelectMiddle, texRect); + m_idSelectMiddle = pPictureService->BGFXGetImageNum(m_sGroupName, param); + pPictureService->BGFXGetTexturePos(m_idUnSelectMiddle, texRect); m_bCurrentSelected = false; pVert[4].tu = pVert[16].tu = pVert[28].tu = pVert[40].tu = texRect.left; pVert[4].tv = pVert[16].tv = pVert[28].tv = pVert[40].tv = texRect.top; @@ -485,11 +658,6 @@ void CXI_TEXTBUTTON::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co pVert[30].pos.y = pVert[31].pos.y = m_rect.bottom + m_fYDeltaPress; pVert[42].pos.y = pVert[43].pos.y = m_rect.bottom + m_fYDeltaPress + m_fYShadowPress; - m_nIndx /= 3; - - m_rs->UnLockVertexBuffer(m_idVBuf); - m_rs->UnLockIndexBuffer(m_idIBuf); - // fill video fragment for (i = 0; i < 8; i++) { @@ -526,12 +694,10 @@ void CXI_TEXTBUTTON::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, co void CXI_TEXTBUTTON::ReleaseAll() { - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); - TEXTURE_RELEASE(m_rs, m_idShadowTex); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); + BGFX_TEXTURE_RELEASE(m_rs, m_idShadowTex); STORM_DELETE(m_sGroupName); STORM_DELETE(m_sString); - VERTEX_BUFFER_RELEASE(m_rs, m_idVBuf); - INDEX_BUFFER_RELEASE(m_rs, m_idIBuf); VIDEOTEXTURE_RELEASE(m_rs, m_pTex); FONT_RELEASE(m_rs, m_nFontNum); } @@ -648,7 +814,7 @@ void CXI_TEXTBUTTON::FillPositionIntoVertices() FXYRECT texRect; XYRECT natureRect; - auto *pVert = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); + auto pVert = m_vertices.data(); if (m_idShadowTex >= 0) { @@ -676,8 +842,8 @@ void CXI_TEXTBUTTON::FillPositionIntoVertices() } // fill left side of button - pPictureService->GetTexturePos(m_idUnSelectLeft, texRect); - pPictureService->GetTexturePos(m_idUnSelectLeft, natureRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectLeft, texRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectLeft, natureRect); float fLeftMiddle = static_cast(m_rect.left + natureRect.right - natureRect.left); pVert[0].pos.x = pVert[2].pos.x = static_cast(m_rect.left); pVert[12].pos.x = pVert[14].pos.x = m_rect.left + m_fXShadow; @@ -699,13 +865,13 @@ void CXI_TEXTBUTTON::FillPositionIntoVertices() // fill right side of button if (m_idUnSelectRight != -1) { - pPictureService->GetTexturePos(m_idUnSelectRight, texRect); - pPictureService->GetTexturePos(m_idUnSelectRight, natureRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectRight, texRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectRight, natureRect); } else { - pPictureService->GetTexturePos(TEXTURE_MODIFY_HORZFLIP, m_idUnSelectLeft, texRect); - pPictureService->GetTexturePos(m_idUnSelectLeft, natureRect); + pPictureService->BGFXGetTexturePos(TEXTURE_MODIFY_HORZFLIP, m_idUnSelectLeft, texRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectLeft, natureRect); } float fRightMiddle = m_rect.right - static_cast(natureRect.right - natureRect.left); pVert[8].pos.x = pVert[10].pos.x = fRightMiddle; // left top X @@ -726,7 +892,7 @@ void CXI_TEXTBUTTON::FillPositionIntoVertices() pVert[46].pos.y = pVert[47].pos.y = m_rect.bottom + m_fYDeltaPress + m_fYShadowPress; // fill middle of button - pPictureService->GetTexturePos(m_idUnSelectMiddle, texRect); + pPictureService->BGFXGetTexturePos(m_idUnSelectMiddle, texRect); pVert[4].pos.x = pVert[6].pos.x = fLeftMiddle; // left pVert[16].pos.x = pVert[18].pos.x = fLeftMiddle + m_fXShadow; pVert[28].pos.x = pVert[30].pos.x = fLeftMiddle + m_fXDeltaPress; @@ -744,8 +910,6 @@ void CXI_TEXTBUTTON::FillPositionIntoVertices() pVert[30].pos.y = pVert[31].pos.y = m_rect.bottom + m_fYDeltaPress; pVert[42].pos.y = pVert[43].pos.y = m_rect.bottom + m_fYDeltaPress + m_fYShadowPress; - m_rs->UnLockVertexBuffer(m_idVBuf); - if (m_bVideoToBack) { fLeftMiddle = static_cast(m_rect.left); diff --git a/src/libs/Xinterface/Nodes/xi_textbutton.h b/src/libs/Xinterface/Nodes/xi_textbutton.h index a99c58ad8..188333a4e 100644 --- a/src/libs/Xinterface/Nodes/xi_textbutton.h +++ b/src/libs/Xinterface/Nodes/xi_textbutton.h @@ -75,10 +75,9 @@ class CXI_TEXTBUTTON : public CINODE char *m_sString; // picture index & vertex buffers - long m_idVBuf; // identificator of the vertex buffer - long m_idIBuf; // identificator of the index buffer long m_nVert; // vertex quantity - long m_nIndx; // index quantity + std::vector m_vertices; + bool m_bVideoToBack; uint32_t m_dwBackColor; diff --git a/src/libs/Xinterface/Nodes/xi_title.cpp b/src/libs/Xinterface/Nodes/xi_title.cpp index 5f9635ce1..09b2f1fab 100644 --- a/src/libs/Xinterface/Nodes/xi_title.cpp +++ b/src/libs/Xinterface/Nodes/xi_title.cpp @@ -1,5 +1,8 @@ #include "xi_title.h" +#include "primitive_renderer.h" + + CXI_TITLE::CXI_TITLE() { m_sGroupName = nullptr; @@ -7,10 +10,9 @@ CXI_TITLE::CXI_TITLE() m_idString = -1L; - m_idVBuf = -1L; - m_idIBuf = -1L; m_nVert = 0; - m_nIndx = 0; + + pVert = nullptr; m_fontID = -1L; m_nNodeType = NODETYPE_TITLE; @@ -25,8 +27,31 @@ void CXI_TITLE::Draw(bool bSelected, uint32_t Delta_Time) { if (m_bUse) { - m_rs->TextureSet(0, m_idTex); - m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 0, m_nVert, 0, m_nIndx, "iTitle"); + /*m_rs->TextureSet(0, m_idTex); + m_rs->DrawBuffer(m_idVBuf, sizeof(XI_ONETEX_VERTEX), m_idIBuf, 0, m_nVert, 0, m_nIndx, "iTitle");*/ + + auto texture = m_rs->GetBGFXTextureFromID(m_idTex); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + for (long n = 0; n < m_nVert; n += 4) + { + auto pVertices = pVert; + std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[n + 0].pos.x, pVertices[n + 0].pos.y, + pVertices[n + 0].pos.z, pVertices[n + 0].tu, + pVertices[n + 0].tv, pVertices[n + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[n + 2].pos.x, pVertices[n + 2].pos.y, + pVertices[n + 2].pos.z, pVertices[n + 2].tu, + pVertices[n + 2].tv, pVertices[n + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[n + 1].pos.x, pVertices[n + 1].pos.y, + pVertices[n + 1].pos.z, pVertices[n + 1].tu, + pVertices[n + 1].tv, pVertices[n + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pVertices[n + 3].pos.x, pVertices[n + 3].pos.y, + pVertices[n + 3].pos.z, pVertices[n + 3].tu, + pVertices[n + 3].tv, pVertices[n + 3].color}); + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + } // show title text if (m_idString != -1L) @@ -47,16 +72,21 @@ bool CXI_TITLE::Init(INIFILE *ini1, const char *name1, INIFILE *ini2, const char void CXI_TITLE::ReleaseAll() { - PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); + BGFX_PICTURE_TEXTURE_RELEASE(pPictureService, m_sGroupName, m_idTex); STORM_DELETE(m_sGroupName); m_idString = -1L; - VERTEX_BUFFER_RELEASE(m_rs, m_idVBuf); - INDEX_BUFFER_RELEASE(m_rs, m_idIBuf); m_nVert = 0; - m_nIndx = 0; - FONT_RELEASE(m_rs, m_fontID); + delete[] pVert; + + //FONT_RELEASE(m_rs, m_fontID); + + if (m_rs != NULL && m_fontID != -1) + { + m_rs->UnloadFont(m_fontID); + m_fontID = -1; + } } int CXI_TITLE::CommandExecute(int wActCode) @@ -142,7 +172,7 @@ void CXI_TITLE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const c if (m_sGroupName == nullptr) throw std::runtime_error("allocate memory error"); memcpy(m_sGroupName, param, len); - m_idTex = pPictureService->GetTextureID(m_sGroupName); + m_idTex = pPictureService->BGFXGetTextureID(m_sGroupName); } else { @@ -153,13 +183,13 @@ void CXI_TITLE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const c FXYRECT centerRect, tiledRect, mediumRect; // get title images if (ReadIniString(ini1, name1, ini2, name2, "titleCenter", param, sizeof(param), "")) - pPictureService->GetTexturePos(m_sGroupName, param, centerRect); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, centerRect); else PZERO(¢erRect, sizeof(centerRect)); if (ReadIniString(ini1, name1, ini2, name2, "titleMedium", param, sizeof(param), "")) { - pPictureService->GetTexturePos(m_sGroupName, param, mediumRect); - pPictureService->GetTexturePos(m_sGroupName, param, m_mRect); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, mediumRect); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, m_mRect); } else { @@ -168,8 +198,8 @@ void CXI_TITLE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const c } if (ReadIniString(ini1, name1, ini2, name2, "titleTiled", param, sizeof(param), "")) { - pPictureService->GetTexturePos(m_sGroupName, param, tiledRect); - pPictureService->GetTexturePos(m_sGroupName, param, m_tRect); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, tiledRect); + pPictureService->BGFXGetTexturePos(m_sGroupName, param, m_tRect); } else { @@ -187,30 +217,9 @@ void CXI_TITLE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const c m_nTiledQuantity = 0; const int rectangleQuantity = 1 + 2 + 2 * m_nTiledQuantity; m_nVert = 4 * rectangleQuantity; - m_nIndx = 6 * rectangleQuantity; - m_idVBuf = m_rs->CreateVertexBuffer(XI_ONETEX_FVF, m_nVert * sizeof(XI_ONETEX_VERTEX), D3DUSAGE_WRITEONLY); - m_idIBuf = m_rs->CreateIndexBuffer(m_nIndx * 2); - m_nIndx /= 3; - - // fill index buffer - auto *const pIndex = static_cast(m_rs->LockIndexBuffer(m_idIBuf)); - if (pIndex == nullptr) - throw std::runtime_error("index buffer not create"); - for (i = 0; i < rectangleQuantity; i++) - { - pIndex[i * 6 + 0] = i * 4; - pIndex[i * 6 + 1] = i * 4 + 1; - pIndex[i * 6 + 2] = i * 4 + 2; - pIndex[i * 6 + 3] = i * 4 + 2; - pIndex[i * 6 + 4] = i * 4 + 1; - pIndex[i * 6 + 5] = i * 4 + 3; - } - m_rs->UnLockIndexBuffer(m_idIBuf); - // fill vertex buffer - auto *const pVert = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); - if (pVert == nullptr) - throw std::runtime_error("vertex buffer not create"); + pVert = new XI_ONETEX_VERTEX[m_nVert]; + for (i = 0; i < m_nVert; i++) { pVert[i].color = imgColor; @@ -275,13 +284,11 @@ void CXI_TITLE::LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const c idx += 4; xpos += xposDelta; } - m_rs->UnLockVertexBuffer(m_idVBuf); } -void CXI_TITLE::FillVertexBuffer() const +void CXI_TITLE::FillVertexBuffer() { long i; - auto *pVert = static_cast(m_rs->LockVertexBuffer(m_idVBuf)); if (pVert != nullptr) { // fill center rectangle (were string is showing) @@ -330,7 +337,5 @@ void CXI_TITLE::FillVertexBuffer() const idx += 4; xpos += xposDelta; } - - m_rs->UnLockVertexBuffer(m_idVBuf); } } diff --git a/src/libs/Xinterface/Nodes/xi_title.h b/src/libs/Xinterface/Nodes/xi_title.h index 14ee9abc0..ba4b7e616 100644 --- a/src/libs/Xinterface/Nodes/xi_title.h +++ b/src/libs/Xinterface/Nodes/xi_title.h @@ -25,7 +25,7 @@ class CXI_TITLE : public CINODE protected: void LoadIni(INIFILE *ini1, const char *name1, INIFILE *ini2, const char *name2) override; - void FillVertexBuffer() const; + void FillVertexBuffer(); protected: char *m_sGroupName; @@ -38,10 +38,9 @@ class CXI_TITLE : public CINODE float m_fontScale; long m_fontID; - long m_idVBuf; // vertex buffer identificator - long m_idIBuf; // index buffer identificator long m_nVert; // vertex quantity - long m_nIndx; // index quantity + XI_ONETEX_VERTEX* pVert; + long m_nStringWidth; XYRECT m_mRect, m_tRect; diff --git a/src/libs/Xinterface/Nodes/xi_tooltips.cpp b/src/libs/Xinterface/Nodes/xi_tooltips.cpp index 7bedfd743..7d4945770 100644 --- a/src/libs/Xinterface/Nodes/xi_tooltips.cpp +++ b/src/libs/Xinterface/Nodes/xi_tooltips.cpp @@ -2,6 +2,9 @@ #include "../xinterface.h" #include "strutils.h" +#include "primitive_renderer.h" + + CXI_ToolTip::CXI_ToolTip(VXSERVICE *pPicService, VSTRSERVICE *pStrService, XYPOINT &pntScrSize) : m_pntScreenSize(pntScrSize) { @@ -43,9 +46,35 @@ void CXI_ToolTip::Draw() if (m_nSquareQ > 0) { - m_rs->TextureSet(0, m_nTextureID); + /*m_rs->TextureSet(0, m_nTextureID); m_rs->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, m_nSquareQ * 4, m_nSquareQ * 2, m_pI, D3DFMT_INDEX16, m_pV, - sizeof(XI_ONETEX_VERTEX), "iVideo"); + sizeof(XI_ONETEX_VERTEX), "iVideo");*/ + + auto texture = m_rs->GetBGFXTextureFromID(m_nTextureID); + m_rs->GetPrimitiveRenderer()->Texture = texture; + + for (int i = 0; i < m_nSquareQ * 2; i += 4) + { + std::vector vertices; + + auto pV = m_pV; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 0].pos.x, pV[i + 0].pos.y, pV[i + 0].pos.z, + pV[i + 0].tu, pV[i + 0].tv, pV[i + 0].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 2].pos.x, pV[i + 2].pos.y, pV[i + 2].pos.z, + pV[i + 2].tu, + pV[i + 2].tv, pV[i + 2].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 1].pos.x, pV[i + 1].pos.y, pV[i + 1].pos.z, + pV[i + 1].tu, pV[i + 1].tv, pV[i + 1].color}); + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{pV[i + 3].pos.x, pV[i + 3].pos.y, pV[i + 3].pos.z, + pV[i + 3].tu, + pV[i + 3].tv, pV[i + 3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices); + + } + + } const auto nX = (m_rPos.left + m_rPos.right) / 2; @@ -131,21 +160,21 @@ void CXI_ToolTip::SetByFormatString(XYRECT &rectOwner, INIFILE *pDefIni, const c if (pDefIni->ReadString(pcToolTipType, "back_imagegroup", param, sizeof(param), "")) { m_sGroupName = param; - m_nTextureID = m_pPicService->GetTextureID(m_sGroupName.c_str()); + m_nTextureID = m_pPicService->BGFXGetTextureID(m_sGroupName.c_str()); if (pDefIni->ReadString(pcToolTipType, "back_imageleft", param, sizeof(param), "")) { - m_nPicIndex_Left = m_pPicService->GetImageNum(m_sGroupName.c_str(), param); + m_nPicIndex_Left = m_pPicService->BGFXGetImageNum(m_sGroupName.c_str(), param); m_pPicService->GetTexturePos(m_nPicIndex_Left, m_uvBackLeft); } if (pDefIni->ReadString(pcToolTipType, "back_imageright", param, sizeof(param), "")) { - m_nPicIndex_Right = m_pPicService->GetImageNum(m_sGroupName.c_str(), param); - m_pPicService->GetTexturePos(m_nPicIndex_Right, m_uvBackRight); + m_nPicIndex_Right = m_pPicService->BGFXGetImageNum(m_sGroupName.c_str(), param); + m_pPicService->BGFXGetTexturePos(m_nPicIndex_Right, m_uvBackRight); } if (pDefIni->ReadString(pcToolTipType, "back_imagemiddle", param, sizeof(param), "")) { - m_nPicIndex_Middle = m_pPicService->GetImageNum(m_sGroupName.c_str(), param); - m_pPicService->GetTexturePos(m_nPicIndex_Middle, m_uvBackMiddle); + m_nPicIndex_Middle = m_pPicService->BGFXGetImageNum(m_sGroupName.c_str(), param); + m_pPicService->BGFXGetTexturePos(m_nPicIndex_Middle, m_uvBackMiddle); } } m_fTurnOnDelay = pDefIni->GetFloat(pcToolTipType, "turnondelay", m_fTurnOnDelay); @@ -175,10 +204,7 @@ void CXI_ToolTip::SetByFormatString(XYRECT &rectOwner, INIFILE *pDefIni, const c m_aSubText.size() * static_cast(m_rs->CharHeight(m_nFontID) * m_fFontScale) + 2 * m_pntTextOffset.x; m_nSquareQ = 3; - CreateIndexBuffer(); CreateVertexBuffer(); - UpdateIndexBuffer(); - ReplaceRectangle(rectOwner.right, rectOwner.bottom); } @@ -207,7 +233,7 @@ void CXI_ToolTip::MousePos(float fDeltaTime, long nX, long nY) void CXI_ToolTip::ReleaseAll() { - PICTURE_TEXTURE_RELEASE(m_pPicService, m_sGroupName.c_str(), m_nTextureID); + BGFX_PICTURE_TEXTURE_RELEASE(m_pPicService, m_sGroupName.c_str(), m_nTextureID); STORM_DELETE(m_pV); STORM_DELETE(m_pI); m_nSquareQ = 0; @@ -215,15 +241,6 @@ void CXI_ToolTip::ReleaseAll() FONT_RELEASE(m_rs, m_nFontID); } -void CXI_ToolTip::CreateIndexBuffer() -{ - if (m_nSquareQ > 0) - { - m_pI = new uint16_t[m_nSquareQ * 6]; - Assert(m_pI); - } -} - void CXI_ToolTip::CreateVertexBuffer() { if (m_nSquareQ > 0) @@ -233,22 +250,6 @@ void CXI_ToolTip::CreateVertexBuffer() } } -void CXI_ToolTip::UpdateIndexBuffer() const -{ - if (!m_pI) - return; - for (long n = 0; n < m_nSquareQ; n++) - { - m_pI[n * 6 + 0] = static_cast(n * 4 + 0); - m_pI[n * 6 + 1] = static_cast(n * 4 + 1); - m_pI[n * 6 + 2] = static_cast(n * 4 + 2); - - m_pI[n * 6 + 3] = static_cast(n * 4 + 1); - m_pI[n * 6 + 4] = static_cast(n * 4 + 3); - m_pI[n * 6 + 5] = static_cast(n * 4 + 2); - } -} - void CXI_ToolTip::UpdateVertexBuffer() { if (!m_pV) diff --git a/src/libs/Xinterface/Nodes/xi_tooltips.h b/src/libs/Xinterface/Nodes/xi_tooltips.h index d1546b580..9cc18bb32 100644 --- a/src/libs/Xinterface/Nodes/xi_tooltips.h +++ b/src/libs/Xinterface/Nodes/xi_tooltips.h @@ -22,9 +22,7 @@ class CXI_ToolTip protected: void ReleaseAll(); - void CreateIndexBuffer(); void CreateVertexBuffer(); - void UpdateIndexBuffer() const; void UpdateVertexBuffer(); void ReplaceRectangle(long x, long y); diff --git a/src/libs/Xinterface/Nodes/xi_videorect.cpp b/src/libs/Xinterface/Nodes/xi_videorect.cpp index 2c047c866..cb33320bf 100644 --- a/src/libs/Xinterface/Nodes/xi_videorect.cpp +++ b/src/libs/Xinterface/Nodes/xi_videorect.cpp @@ -2,6 +2,9 @@ #include "../base_video.h" #include "Entity.h" +#include "primitive_renderer.h" + + CXI_VIDEORECT::CXI_VIDEORECT() { m_rs = nullptr; @@ -45,8 +48,21 @@ void CXI_VIDEORECT::Draw(bool bSelected, uint32_t Delta_Time) v[i].pos.z = 1.f; } - m_rs->SetTexture(0, pTex); - m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, v, sizeof(XI_ONETEX_VERTEX), "iVideo"); + //m_rs->SetTexture(0, pTex); + /*m_rs->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, XI_ONETEX_FVF, 2, v, sizeof(XI_ONETEX_VERTEX), "iVideo");*/ + + /*std::vector vertices; + + vertices.push_back(VERTEX_POSITION_TEXTURE_COLOR{v[0].pos.x, v[0].pos.y, v[0].pos.z, v[0].tu, v[0].tv, v[0].color}); + vertices.push_back( + VERTEX_POSITION_TEXTURE_COLOR{v[2].pos.x, v[2].pos.y, v[2].pos.z, v[2].tu, v[2].tv, v[1].color}); + vertices.push_back( + VERTEX_POSITION_TEXTURE_COLOR{v[1].pos.x, v[1].pos.y, v[1].pos.z, v[1].tu, v[1].tv, v[2].color}); + vertices.push_back( + VERTEX_POSITION_TEXTURE_COLOR{v[3].pos.x, v[3].pos.y, v[3].pos.z, v[3].tu, v[3].tv, v[3].color}); + + m_rs->GetPrimitiveRenderer()->PushVertices(vertices);*/ + } } } diff --git a/src/libs/Xinterface/TextureSequence/TextureSequence.h b/src/libs/Xinterface/TextureSequence/TextureSequence.h index 541f13ace..97f0f98ca 100644 --- a/src/libs/Xinterface/TextureSequence/TextureSequence.h +++ b/src/libs/Xinterface/TextureSequence/TextureSequence.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "VideoTexture.h" //----------------------------------------------------------------------------- @@ -24,6 +26,8 @@ class TextureSequence : public CVideoTexture long m_AllTex; + bgfx::TextureHandle m_bgfx_AllTex; + void ToTextureRender(float blendValue) const; public: diff --git a/src/libs/Xinterface/aviplayer/aviplayer.cpp b/src/libs/Xinterface/aviplayer/aviplayer.cpp index 4efa47c06..41835ee2f 100644 --- a/src/libs/Xinterface/aviplayer/aviplayer.cpp +++ b/src/libs/Xinterface/aviplayer/aviplayer.cpp @@ -325,7 +325,8 @@ bool CAviPlayer::GetInterfaces() core.Trace("Video Error!!! Can`t add video stream"); return false; } - hr = pAMStream->AddMediaStream(nullptr, &MSPID_PrimaryAudio, AMMSF_ADDDEFAULTRENDERER, nullptr); + //hr = pAMStream->AddMediaStream(nullptr, &MSPID_PrimaryAudio, AMMSF_ADDDEFAULTRENDERER, nullptr); + hr = S_FALSE; if (FAILED(hr)) { core.Trace("Video Error!!! Can`t add audio stream"); diff --git a/src/libs/Xinterface/vxservice.h b/src/libs/Xinterface/vxservice.h index ad205a648..7885c5f14 100644 --- a/src/libs/Xinterface/vxservice.h +++ b/src/libs/Xinterface/vxservice.h @@ -34,5 +34,25 @@ class VXSERVICE virtual long GetImageNum(const char *sImageListName, const char *sImageName) = 0; + // get texture identificator for image group + virtual long BGFXGetTextureID(const char *sImageListName) = 0; + virtual bool BGFXReleaseTextureID(const char *sImageListName) = 0; + + // get texture positon for select picture + virtual bool BGFXGetTexturePos(long pictureNum, FXYRECT &texRect) = 0; + virtual bool BGFXGetTexturePos(long pictureNum, XYRECT &texRect) = 0; + virtual bool BGFXGetTexturePos(const char *sImageListName, const char *sImageName, FXYRECT &texRect) = 0; + virtual bool BGFXGetTexturePos(const char *sImageListName, const char *sImageName, XYRECT &texRect) = 0; + virtual bool BGFXGetTexturePos(int nTextureModify, long pictureNum, FXYRECT &texRect) = 0; + virtual bool BGFXGetTexturePos(int nTextureModify, const char *sImageListName, const char *sImageName, + FXYRECT &texRect) = 0; + + virtual void BGFXGetTextureCutForSize(const char *pcImageListName, const FXYPOINT &pntLeftTopUV, + const XYPOINT &pntSize, + long nSrcWidth, long nSrcHeight, FXYRECT &outUV) = 0; + + virtual long BGFXGetImageNum(const char *sImageListName, const char *sImageName) = 0; + + virtual void ReleaseAll() = 0; }; diff --git a/src/libs/Xinterface/xdefines.h b/src/libs/Xinterface/xdefines.h index 848cf17c2..b9310f163 100644 --- a/src/libs/Xinterface/xdefines.h +++ b/src/libs/Xinterface/xdefines.h @@ -220,6 +220,16 @@ struct XI_THREETEX_VERTEX float tu3, tv3; }; +inline void BGFX_PICTURE_TEXTURE_RELEASE(VXSERVICE *ps, const char *gn, long &tex) +{ + if (tex != -1 && ps != nullptr) + { + ps->BGFXReleaseTextureID(gn); + tex = -1; + } +} + + inline void PICTURE_TEXTURE_RELEASE(VXSERVICE *ps, const char *gn, long &tex) { if (tex != -1 && ps != nullptr) diff --git a/src/libs/Xinterface/xinterface.cpp b/src/libs/Xinterface/xinterface.cpp index 3f45daa82..80a3037bf 100644 --- a/src/libs/Xinterface/xinterface.cpp +++ b/src/libs/Xinterface/xinterface.cpp @@ -10,6 +10,15 @@ #include "xservice.h" #include +#include "bgfx_utils.h" +#include "glm.hpp" +#include "gtx/matrix_transform_2d.hpp" + +#include "gtc/matrix_transform.hpp" +#include "gtc/type_ptr.hpp" +#include "matrix.hpp" + + #include #include "aviplayer/aviplayer.h" @@ -343,11 +352,17 @@ void XINTERFACE::Realize(uint32_t Delta_Time) // Get old transformation pRenderService->GetTransform(D3DTS_VIEW, moldv); pRenderService->GetTransform(D3DTS_PROJECTION, moldp); + + // Set new transformation pRenderService->SetTransform(D3DTS_WORLD, matw); pRenderService->SetTransform(D3DTS_VIEW, matv); pRenderService->SetTransform(D3DTS_PROJECTION, matp); + pRenderService->BGFXSetTransformUpdateViews(D3DTS_WORLD, matw); + pRenderService->BGFXSetTransformUpdateViews(D3DTS_VIEW, matv); + pRenderService->BGFXSetTransformUpdateViews(D3DTS_PROJECTION, bgfxProj); + DrawNode(m_pNodes, Delta_Time, 0, 80); // Do mouse move @@ -449,6 +464,9 @@ void XINTERFACE::Realize(uint32_t Delta_Time) // Restore old transformation pRenderService->SetTransform(D3DTS_VIEW, moldv); pRenderService->SetTransform(D3DTS_PROJECTION, moldp); + + pRenderService->BGFXSetTransform(D3DTS_VIEW, moldv); + pRenderService->BGFXSetTransform(D3DTS_PROJECTION, moldp); } long oldCurNum = -1L; @@ -1165,6 +1183,20 @@ void XINTERFACE::LoadIni() matp.m[3][3] = 0.f; matp.m[3][2] = -1.f; + + glm::mat4 proj(1); + bx::mtxOrtho(glm::value_ptr(proj), 0.0f, float(dwScreenWidth), float(dwScreenHeight), 0.0f, 1.0f, 100.0f, 0.0f, + bgfx::getCaps()->homogeneousDepth); + + proj = glm::transpose(proj); + + float *glmprojection = glm::value_ptr(proj); + + for (int i = 0; i < 16; ++i) + bgfxProj.matrix[i] = glmprojection[i]; + + bgfxProj.m[2][2] = 1.0003f; + matv.m[3][0] = -(GlobalScreenRect.left + GlobalScreenRect.right) / 2.f; matv.m[3][1] = -(GlobalScreenRect.top + GlobalScreenRect.bottom) / 2.f; diff --git a/src/libs/Xinterface/xinterface.h b/src/libs/Xinterface/xinterface.h index 9690a5984..abcbabe3e 100644 --- a/src/libs/Xinterface/xinterface.h +++ b/src/libs/Xinterface/xinterface.h @@ -205,7 +205,8 @@ class XINTERFACE : public XINTERFACE_BASE protected: // WORLD,VIEW and PROJECTION matrixes CMatrix matw, matv, matp; - + CMatrix bgfxProj; + CINODE *m_pCurNode; CINODE *m_pNodes; CINODE *m_pContHelp; diff --git a/src/libs/Xinterface/xservice.cpp b/src/libs/Xinterface/xservice.cpp index 82a33d489..c28199f43 100644 --- a/src/libs/Xinterface/xservice.cpp +++ b/src/libs/Xinterface/xservice.cpp @@ -14,6 +14,11 @@ XSERVICE::XSERVICE() m_pList = nullptr; m_pImage = nullptr; + m_BGFXdwListQuantity = 0; + m_BGFXdwImageQuantity = 0; + m_BGFXpList = nullptr; + m_BGFXpImage = nullptr; + m_pRS = nullptr; } @@ -48,6 +53,30 @@ void XSERVICE::Init(VDX9RENDER *pRS, long lWidth, long lHeight) LoadAllPicturesInfo(); } + +long XSERVICE::BGFXGetTextureID(const char *sImageListName) +{ + if (sImageListName != nullptr) + { + for (auto i = 0; i < m_BGFXdwListQuantity; i++) + if (!_stricmp(m_BGFXpList[i].sImageListName, sImageListName)) + { + if (m_BGFXpList[i].textureQuantity <= 0) + { + char sTexName[256]; + sprintf_s(sTexName, "INTERFACES\\%s", m_BGFXpList[i].sTextureName); + m_BGFXpList[i].textureID = m_pRS->BGFXTextureCreate(sTexName); + m_BGFXpList[i].textureQuantity = 1; + } + else + m_BGFXpList[i].textureQuantity++; + return m_BGFXpList[i].textureID; + } + } + + return -1; +} + long XSERVICE::GetTextureID(const char *sImageListName) { if (sImageListName != nullptr) @@ -71,6 +100,16 @@ long XSERVICE::GetTextureID(const char *sImageListName) return -1; } +long XSERVICE::BGFXFindGroup(const char *sImageListName) const +{ + if (!sImageListName) + return -1; + for (auto n = 0; n < m_BGFXdwListQuantity; n++) + if (!_stricmp(m_BGFXpList[n].sImageListName, sImageListName)) + return n; + return -1; +} + long XSERVICE::FindGroup(const char *sImageListName) const { if (!sImageListName) @@ -81,6 +120,22 @@ long XSERVICE::FindGroup(const char *sImageListName) const return -1; } +bool XSERVICE::BGFXReleaseTextureID(const char *sImageListName) +{ + if (sImageListName == nullptr) + return false; + + for (auto i = 0; i < m_BGFXdwListQuantity; i++) + if (!_stricmp(m_BGFXpList[i].sImageListName, sImageListName)) + if (--m_BGFXpList[i].textureQuantity == 0) + { + m_pRS->BGFXTextureRelease(m_BGFXpList[i].textureID); + return true; + } + + return false; +} + bool XSERVICE::ReleaseTextureID(const char *sImageListName) { if (sImageListName == nullptr) @@ -97,6 +152,34 @@ bool XSERVICE::ReleaseTextureID(const char *sImageListName) return false; } +bool XSERVICE::BGFXGetTexturePos(long pictureNum, FXYRECT &texRect) +{ + if (pictureNum >= 0 && pictureNum < m_BGFXdwImageQuantity) + { + // find picture group + int gn; + for (gn = 0; gn < m_BGFXdwListQuantity; gn++) + if (pictureNum >= m_BGFXpList[gn].pictureStart && + pictureNum < m_BGFXpList[gn].pictureStart + m_BGFXpList[gn].pictureQuantity) + break; + if (gn < m_BGFXdwListQuantity) + { + texRect.left = + static_cast(m_BGFXpImage[pictureNum].pTextureRect.left + m_fWAdd) / m_BGFXpList[gn].textureWidth; + texRect.right = + static_cast(m_BGFXpImage[pictureNum].pTextureRect.right - m_fWAdd) / m_BGFXpList[gn].textureWidth; + texRect.top = + static_cast(m_BGFXpImage[pictureNum].pTextureRect.top + m_fHAdd) / m_BGFXpList[gn].textureHeight; + texRect.bottom = + static_cast(m_BGFXpImage[pictureNum].pTextureRect.bottom - m_fHAdd) / m_BGFXpList[gn].textureHeight; + return true; + } + } + + PZERO(&texRect, sizeof(texRect)); + return false; +} + bool XSERVICE::GetTexturePos(long pictureNum, FXYRECT &texRect) { if (pictureNum >= 0 && pictureNum < m_dwImageQuantity) @@ -125,6 +208,18 @@ bool XSERVICE::GetTexturePos(long pictureNum, FXYRECT &texRect) return false; } +bool XSERVICE::BGFXGetTexturePos(long pictureNum, XYRECT &texRect) +{ + if (pictureNum >= 0 && pictureNum < m_BGFXdwImageQuantity) + { + memcpy(&texRect, &m_BGFXpImage[pictureNum].pTextureRect, sizeof(XYRECT)); + return true; + } + + PZERO(&texRect, sizeof(texRect)); + return false; +} + bool XSERVICE::GetTexturePos(long pictureNum, XYRECT &texRect) { if (pictureNum >= 0 && pictureNum < m_dwImageQuantity) @@ -137,16 +232,69 @@ bool XSERVICE::GetTexturePos(long pictureNum, XYRECT &texRect) return false; } +bool XSERVICE::BGFXGetTexturePos(const char *sImageListName, const char *sImageName, FXYRECT &texRect) +{ + return GetTexturePos(GetImageNum(sImageListName, sImageName), texRect); +} + bool XSERVICE::GetTexturePos(const char *sImageListName, const char *sImageName, FXYRECT &texRect) { return GetTexturePos(GetImageNum(sImageListName, sImageName), texRect); } +bool XSERVICE::BGFXGetTexturePos(const char *sImageListName, const char *sImageName, XYRECT &texRect) +{ + return GetTexturePos(GetImageNum(sImageListName, sImageName), texRect); +} + bool XSERVICE::GetTexturePos(const char *sImageListName, const char *sImageName, XYRECT &texRect) { return GetTexturePos(GetImageNum(sImageListName, sImageName), texRect); } +bool XSERVICE::BGFXGetTexturePos(int nTextureModify, long pictureNum, FXYRECT &texRect) +{ + FXYRECT rectTmp; + + if (pictureNum >= 0 && pictureNum < m_BGFXdwImageQuantity) + { + // find picture group + int gn; + for (gn = 0; gn < m_BGFXdwListQuantity; gn++) + if ((pictureNum >= m_BGFXpList[gn].pictureStart) && + (pictureNum < m_BGFXpList[gn].pictureStart + m_BGFXpList[gn].pictureQuantity)) + break; + if (gn < m_BGFXdwListQuantity) + { + rectTmp.left = static_cast(m_BGFXpImage[pictureNum].pTextureRect.left); + rectTmp.top = static_cast(m_BGFXpImage[pictureNum].pTextureRect.top); + rectTmp.right = static_cast(m_BGFXpImage[pictureNum].pTextureRect.right); + rectTmp.bottom = static_cast(m_BGFXpImage[pictureNum].pTextureRect.bottom); + if (nTextureModify & TEXTURE_MODIFY_HORZFLIP) + { + const auto tmp = rectTmp.left + m_fWAdd * 2.f; + rectTmp.left = rectTmp.right - m_fWAdd * 2.f; + rectTmp.right = tmp; + } + if (nTextureModify & TEXTURE_MODIFY_VERTFLIP) + { + const auto tmp = rectTmp.top + m_fHAdd * 2.f; + rectTmp.top = rectTmp.bottom - m_fHAdd * 2.f; + rectTmp.bottom = tmp; + } + texRect.left = (rectTmp.left + m_fWAdd) / m_BGFXpList[gn].textureWidth; + texRect.right = static_cast(rectTmp.right - m_fWAdd) / m_BGFXpList[gn].textureWidth; + texRect.top = (rectTmp.top + m_fHAdd) / m_BGFXpList[gn].textureHeight; + texRect.bottom = static_cast(rectTmp.bottom - m_fHAdd) / m_BGFXpList[gn].textureHeight; + return true; + } + } + + PZERO(&texRect, sizeof(texRect)); + return false; +} + + bool XSERVICE::GetTexturePos(int nTextureModify, long pictureNum, FXYRECT &texRect) { FXYRECT rectTmp; @@ -189,11 +337,43 @@ bool XSERVICE::GetTexturePos(int nTextureModify, long pictureNum, FXYRECT &texRe return false; } +bool XSERVICE::BGFXGetTexturePos(int nTextureModify, const char *sImageListName, const char *sImageName, FXYRECT &texRect) +{ + return GetTexturePos(nTextureModify, GetImageNum(sImageListName, sImageName), texRect); +} + bool XSERVICE::GetTexturePos(int nTextureModify, const char *sImageListName, const char *sImageName, FXYRECT &texRect) { return GetTexturePos(nTextureModify, GetImageNum(sImageListName, sImageName), texRect); } +void XSERVICE::BGFXGetTextureCutForSize(const char *pcImageListName, const FXYPOINT &pntLeftTopUV, const XYPOINT &pntSize, + long nSrcWidth, long nSrcHeight, FXYRECT &outUV) +{ + const auto n = BGFXFindGroup(pcImageListName); + if (n >= 0) + { + if (nSrcWidth < m_BGFXpList[n].textureWidth) + nSrcWidth = m_BGFXpList[n].textureWidth; + if (nSrcHeight < m_BGFXpList[n].textureHeight) + nSrcHeight = m_BGFXpList[n].textureHeight; + } + auto fW = 1.f; + if (nSrcWidth > 0) + fW = static_cast(pntSize.x) / nSrcWidth + pntLeftTopUV.x; + auto fH = 1.f; + if (nSrcHeight > 0) + fH = static_cast(pntSize.y) / nSrcHeight + pntLeftTopUV.y; + if (fW > 1.f) + fW = 1.f; + if (fH > 1.f) + fH = 1.f; + outUV.left = pntLeftTopUV.x; + outUV.top = pntLeftTopUV.y; + outUV.right = fW; + outUV.bottom = fH; +} + void XSERVICE::GetTextureCutForSize(const char *pcImageListName, const FXYPOINT &pntLeftTopUV, const XYPOINT &pntSize, long nSrcWidth, long nSrcHeight, FXYRECT &outUV) { @@ -318,7 +498,100 @@ void XSERVICE::LoadAllPicturesInfo() if (!ini->GetSectionNameNext(section, sizeof(section) - 1)) break; } + + ini.reset(); + + // BGFX + memset(section, 0, sizeof(section)); + memset(param, 0, sizeof(param)); + + ini = fio->OpenIniFile((char *)LISTS_INIFILE); + if (!ini) + throw std::exception("ini file not found!"); + + m_BGFXdwListQuantity = 0; + m_BGFXdwImageQuantity = 0; + + // calculate lists quantity + if (ini->GetSectionName(section, sizeof(section) - 1)) + do + m_BGFXdwListQuantity++; + while (ini->GetSectionNameNext(section, sizeof(section) - 1)); + // create list pointers array + if (m_BGFXdwListQuantity > 0) + { + m_BGFXpList = new IMAGELISTDESCR[m_BGFXdwListQuantity]; + if (m_BGFXpList == nullptr) + throw std::exception("memory allocate error"); + } + + // fill lists + if (ini->GetSectionName(section, sizeof(section) - 1)) + for (auto i = 0; true; i++) + { + m_BGFXpList[i].textureQuantity = 0; + m_BGFXpList[i].textureID = -1L; + + // get list name + m_BGFXpList[i].sImageListName = new char[sizeof section]; + strcpy_s(m_BGFXpList[i].sImageListName, sizeof section, section); + // get texture name + ini->ReadString(section, "sTextureName", param, sizeof(param) - 1, ""); + m_BGFXpList[i].sTextureName = new char[sizeof param]; + strcpy_s(m_BGFXpList[i].sTextureName, sizeof param, param); + + // get texture width & height + m_BGFXpList[i].textureWidth = ini->GetLong(section, "wTextureWidth", 1024); + m_BGFXpList[i].textureHeight = ini->GetLong(section, "wTextureHeight", 1024); + + m_BGFXpList[i].pictureStart = m_BGFXdwImageQuantity; + // get pictures quantity + m_BGFXpList[i].pictureQuantity = 0; + if (ini->ReadString(section, "picture", param, sizeof(param) - 1, "")) + do + m_BGFXpList[i].pictureQuantity++; + while (ini->ReadStringNext(section, "picture", param, sizeof(param) - 1)); + + // resize image list + auto *const oldpImage = m_BGFXpImage; + m_BGFXpImage = new PICTUREDESCR[m_BGFXdwImageQuantity + m_BGFXpList[i].pictureQuantity]; + if (m_BGFXpImage == nullptr) + throw std::exception("allocate memory error"); + if (oldpImage != nullptr) + { + memcpy(m_BGFXpImage, oldpImage, m_BGFXdwImageQuantity * sizeof(PICTUREDESCR)); + delete oldpImage; + } + m_BGFXdwImageQuantity += m_BGFXpList[i].pictureQuantity; + + // set pictures + char picName[sizeof(param)]; + ini->ReadString(section, "picture", param, sizeof(param) - 1, ""); + for (int j = m_BGFXpList[i].pictureStart; j < m_BGFXdwImageQuantity; j++) + { + // get texture coordinates + int nLeft, nTop, nRight, nBottom; + + sscanf(param, "%[^,],%d,%d,%d,%d", picName, &nLeft, &nTop, &nRight, &nBottom); + m_BGFXpImage[j].pTextureRect.left = nLeft; + m_BGFXpImage[j].pTextureRect.top = nTop; + m_BGFXpImage[j].pTextureRect.right = nRight; + m_BGFXpImage[j].pTextureRect.bottom = nBottom; + + const auto len = strlen(picName) + 1; + m_BGFXpImage[j].sPictureName = new char[len]; + memcpy(m_BGFXpImage[j].sPictureName, picName, len); + + ini->ReadStringNext(section, "picture", param, sizeof(param) - 1); + } + + if (!ini->GetSectionNameNext(section, sizeof(section) - 1)) + break; + } + + ini.reset(); } + } void XSERVICE::ReleaseAll() @@ -350,8 +623,73 @@ void XSERVICE::ReleaseAll() m_dwListQuantity = 0; m_dwImageQuantity = 0; + + if (m_BGFXpList != nullptr) + { + for (auto i = 0; i < m_BGFXdwListQuantity; i++) + { + if (m_BGFXpList[i].textureQuantity != 0) + m_pRS->BGFXTextureRelease(m_BGFXpList[i].textureID); + + delete m_BGFXpList[i].sImageListName; + + delete m_BGFXpList[i].sTextureName; + } + + delete m_BGFXpList; + } + + // BGFX + + if (m_BGFXpImage != nullptr) + { + for (auto i = 0; i < m_BGFXdwImageQuantity; i++) + { + delete m_BGFXpImage[i].sPictureName; + } + + delete m_BGFXpImage; + } + + m_BGFXdwListQuantity = 0; + m_BGFXdwImageQuantity = 0; } +long XSERVICE::BGFXGetImageNum(const char *sImageListName, const char *sImageName) +{ + long retVal = -1; + + if (sImageName != nullptr) + if (sImageListName != nullptr) + { + for (int i = 0; i < m_BGFXdwListQuantity; i++) + if (!_stricmp(m_BGFXpList[i].sImageListName, sImageListName)) + { + for (int j = m_BGFXpList[i].pictureStart; + j < m_BGFXpList[i].pictureStart + m_BGFXpList[i].pictureQuantity; + j++) + if (!_stricmp(m_BGFXpImage[j].sPictureName, sImageName)) + { + retVal = j; + break; + } + break; + } + } + else + { + for (int i = 0; i < m_BGFXdwImageQuantity; i++) + if (!_stricmp(m_BGFXpImage[i].sPictureName, sImageName)) + { + retVal = i; + break; + } + } + + return retVal; +} + + long XSERVICE::GetImageNum(const char *sImageListName, const char *sImageName) { long retVal = -1; @@ -383,7 +721,6 @@ long XSERVICE::GetImageNum(const char *sImageListName, const char *sImageName) return retVal; } - //============================================================================ // COMBINE STRINGS CLASS //============================================================================ diff --git a/src/libs/Xinterface/xservice.h b/src/libs/Xinterface/xservice.h index 9f7ee9038..4043bc49a 100644 --- a/src/libs/Xinterface/xservice.h +++ b/src/libs/Xinterface/xservice.h @@ -37,6 +37,7 @@ class XSERVICE : public VXSERVICE bool ReleaseTextureID(const char *sImageListName) override; // get texture positon for select picture + bool GetTexturePos(long pictureNum, FXYRECT &texRect) override; bool GetTexturePos(long pictureNum, XYRECT &texRect) override; bool GetTexturePos(const char *sImageListName, const char *sImageName, FXYRECT &texRect) override; @@ -47,8 +48,24 @@ class XSERVICE : public VXSERVICE void GetTextureCutForSize(const char *pcImageListName, const FXYPOINT &pntLeftTopUV, const XYPOINT &pntSize, long nSrcWidth, long nSrcHeight, FXYRECT &outUV) override; - long GetImageNum(const char *sImageListName, const char *sImageName) override; + + long BGFXGetTextureID(const char *sImageListName) override; + long BGFXFindGroup(const char *sImageListName) const; + bool BGFXReleaseTextureID(const char *sImageListName) override; + + bool BGFXGetTexturePos(long pictureNum, FXYRECT &texRect) override; + bool BGFXGetTexturePos(long pictureNum, XYRECT &texRect) override; + bool BGFXGetTexturePos(const char *sImageListName, const char *sImageName, FXYRECT &texRect) override; + bool BGFXGetTexturePos(const char *sImageListName, const char *sImageName, XYRECT &texRect) override; + bool BGFXGetTexturePos(int nTextureModify, long pictureNum, FXYRECT &texRect) override; + bool BGFXGetTexturePos(int nTextureModify, const char *sImageListName, const char *sImageName, + FXYRECT &texRect) override; + + void BGFXGetTextureCutForSize(const char *pcImageListName, const FXYPOINT &pntLeftTopUV, const XYPOINT &pntSize, + long nSrcWidth, long nSrcHeight, FXYRECT &outUV) override; + + long BGFXGetImageNum(const char *sImageListName, const char *sImageName) override; void ReleaseAll() override; @@ -63,6 +80,12 @@ class XSERVICE : public VXSERVICE IMAGELISTDESCR *m_pList; PICTUREDESCR *m_pImage; + + long m_BGFXdwListQuantity; + long m_BGFXdwImageQuantity; + IMAGELISTDESCR *m_BGFXpList; + PICTUREDESCR *m_BGFXpImage; + // Scale factors float m_fWScale; float m_fHScale; diff --git a/src/libs/renderer/CMakeLists.txt b/src/libs/renderer/CMakeLists.txt index d3960f420..66c45a38d 100644 --- a/src/libs/renderer/CMakeLists.txt +++ b/src/libs/renderer/CMakeLists.txt @@ -10,6 +10,8 @@ add_library(${PROJECT_NAME} STATIC sdevice.h texture.h IVBufferManager.cpp + sprite_renderer.cpp + primitive_renderer.cpp ) target_include_directories(${PROJECT_NAME} PUBLIC ${DirectX_INCLUDE_DIR}) diff --git a/src/libs/renderer/Font.cpp b/src/libs/renderer/Font.cpp index bbe269917..0de27e51c 100644 --- a/src/libs/renderer/Font.cpp +++ b/src/libs/renderer/Font.cpp @@ -3,8 +3,11 @@ #include "defines.h" #include "utf8.h" +#include "sprite_renderer.h" + static char Buffer1024[1024]; + FONT::FONT() { RenderService = nullptr; @@ -17,7 +20,7 @@ FONT::FONT() bInverse = bOldInverse = false; techniqueName = nullptr; textureName = nullptr; -} +} FONT::~FONT() { @@ -36,7 +39,7 @@ FONT::~FONT() if (RenderService) { if (TextureID >= 0) - RenderService->TextureRelease(TextureID); + RenderService->BGFXTextureRelease(TextureID); // core.FreeService("dx9render"); } } @@ -190,7 +193,8 @@ bool FONT::Init(const char *font_name, const char *iniName, IDirect3DDevice9 *_d fScale = fOldScale = 1.f; Color = oldColor = 0xFFFFFFFF; - TextureID = RenderService->TextureCreate(textureName); + //TextureID = RenderService->TextureCreate(textureName); + TextureID = RenderService->BGFXTextureCreate(textureName); if (TextureID < 0) { core.Trace("Not Found Texture: %s", textureName); @@ -262,11 +266,14 @@ long FONT::UpdateVertexBuffer(long x, long y, char *data_PTR, int utf8length) long n; float xoffset; uint8_t sym; + IMAGE_VERTEX *pVertex; s_num = strlen(data_PTR); - VBuffer->Lock(0, sizeof(IMAGE_VERTEX) * utf8length * SYM_VERTEXS, (VOID **)&pVertex, 0); + + + //VBuffer->Lock(0, sizeof(IMAGE_VERTEX) * utf8length * SYM_VERTEXS, (VOID **)&pVertex, 0); xoffset = 0; @@ -277,7 +284,7 @@ long FONT::UpdateVertexBuffer(long x, long y, char *data_PTR, int utf8length) int Codepoint = utf8::Utf8ToCodepoint(data_PTR + i); Assert(Codepoint < USED_CODES); - n = curLetter * 6; + n = curLetter * 4; FLOAT_RECT pos = CharT[Codepoint].Pos; if (fScale != 1.f) { @@ -295,7 +302,7 @@ long FONT::UpdateVertexBuffer(long x, long y, char *data_PTR, int utf8length) xoffset += Spacebar * fScale; } - pVertex[n + 0].pos.x = pos.x1; + /*pVertex[n + 0].pos.x = pos.x1; pVertex[n + 1].pos.x = pos.x1; pVertex[n + 2].pos.x = pos.x2; @@ -309,52 +316,81 @@ long FONT::UpdateVertexBuffer(long x, long y, char *data_PTR, int utf8length) pVertex[n + 3].pos.y = pos.y2; pVertex[n + 4].pos.y = pos.y2; - pVertex[n + 5].pos.y = pos.y1; + pVertex[n + 5].pos.y = pos.y1;*/ FLOAT_RECT tuv = CharT[Codepoint].Tuv; - pVertex[n + 0].tu = tuv.x1; + /*pVertex[n + 0].tu = tuv.x1; + pVertex[n + 0].tv = tuv.y1; + pVertex[n + 1].tu = tuv.x1; + pVertex[n + 1].tv = tuv.y2; + pVertex[n + 2].tu = tuv.x2; + pVertex[n + 2].tv = tuv.y1; pVertex[n + 3].tu = tuv.x1; + pVertex[n + 3].tv = tuv.y2; + pVertex[n + 4].tu = tuv.x2; + pVertex[n + 4].tv = tuv.y2; + pVertex[n + 5].tu = tuv.x2; + pVertex[n + 5].tv = tuv.y1;*/ - pVertex[n + 0].tv = tuv.y1; - pVertex[n + 1].tv = tuv.y2; - pVertex[n + 2].tv = tuv.y1; + /*pVertex[n + 0].color = pVertex[n + 1].color = pVertex[n + 2].color = pVertex[n + 3].color = + pVertex[n + 4].color = pVertex[n + 5].color = Color;*/ - pVertex[n + 3].tv = tuv.y2; - pVertex[n + 4].tv = tuv.y2; - pVertex[n + 5].tv = tuv.y1; + std::vector vertices; + + vertices.resize(4); + + vertices[0] = glm::vec3(pos.x1, pos.y1, 1); + vertices[1] = glm::vec3(pos.x2, pos.y1, 1); + vertices[2] = glm::vec3(pos.x1, pos.y2, 1); + vertices[3] = glm::vec3(pos.x2, pos.y2, 1); + + auto uCoordinates = glm::vec2(tuv.x1, tuv.x2); + + auto vCoordinates = glm::vec2(tuv.y1, tuv.y2); - pVertex[n + 0].color = pVertex[n + 1].color = pVertex[n + 2].color = pVertex[n + 3].color = - pVertex[n + 4].color = pVertex[n + 5].color = Color; + //RenderService->GetSpriteRenderer()->SetViewProjection(); + //RenderService->GetSpriteRenderer()->UpdateVertexBuffer(vertices, uCoordinates, vCoordinates, Color); - pVertex[n + 0].rhw = pVertex[n + 1].rhw = pVertex[n + 2].rhw = pVertex[n + 3].rhw = pVertex[n + 4].rhw = - pVertex[n + 5].rhw = fScale; + /*pVertex[n + 0].rhw = pVertex[n + 1].rhw = pVertex[n + 2].rhw = pVertex[n + 3].rhw = pVertex[n + 4].rhw = + pVertex[n + 5].rhw = fScale;*/ } - VBuffer->Unlock(); + + + + + //VBuffer->Unlock(); return static_cast(xoffset); } long FONT::Print(long x, long y, char *data_PTR) { - if (data_PTR == nullptr || techniqueName == nullptr) + //if (data_PTR == nullptr || techniqueName == nullptr) + if (data_PTR == nullptr) return 0; + auto xoffset = 0L; long s_num = utf8::Utf8StringLength(data_PTR); if (s_num == 0) return 0; - const auto bDraw = RenderService->TechniqueExecuteStart(techniqueName); + /*const auto bDraw = RenderService->TechniqueExecuteStart(techniqueName); if (!bDraw) - return xoffset; + return xoffset;*/ + + RenderService->GetSpriteRenderer()->Texture = RenderService->GetBGFXTextureFromID(TextureID); + + //RenderService->GetSpriteRenderer()->SetViewProjection(); - RenderService->TextureSet(0, TextureID); +/* RenderService->TextureSet(0, TextureID); Device->SetFVF(IMAGE_FVF); Device->SetStreamSource(0, VBuffer, 0, sizeof(IMAGE_VERTEX)); + */ // Device->SetIndices(0); if (bInverse) @@ -363,18 +399,18 @@ long FONT::Print(long x, long y, char *data_PTR) { UpdateVertexBuffer(x + Shadow_offsetx, y + Shadow_offsety, data_PTR, s_num); - Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); - Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); + //Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); + //Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); - Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, s_num * 2); + //Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, s_num * 2); } xoffset = UpdateVertexBuffer(x, y, data_PTR, s_num); - Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); - Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); + //Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); + //Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); - Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, s_num * 2); + //Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, s_num * 2); } else { @@ -382,21 +418,21 @@ long FONT::Print(long x, long y, char *data_PTR) { UpdateVertexBuffer(x + Shadow_offsetx, y + Shadow_offsety, data_PTR, s_num); - Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); - Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); + //Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); + //Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); - Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, s_num * 2); + //Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, s_num * 2); } xoffset = UpdateVertexBuffer(x, y, data_PTR, s_num); - Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); - Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); + //Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); + //Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); - Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, s_num * 2); + //Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, s_num * 2); } - while (RenderService->TechniqueExecuteNext()) - ; + /*while (RenderService->TechniqueExecuteNext()) + ;*/ return xoffset; } @@ -428,7 +464,8 @@ void FONT::TempUnload() if (RenderService != nullptr) { if (TextureID != -1L) - RenderService->TextureRelease(TextureID); + // RenderService->TextureRelease(TextureID); + RenderService->BGFXTextureRelease(TextureID); TextureID = -1L; } } @@ -436,5 +473,5 @@ void FONT::TempUnload() void FONT::RepeatInit() { if (TextureID == -1L) - TextureID = RenderService->TextureCreate(textureName); + TextureID = RenderService->BGFXTextureCreate(textureName); } diff --git a/src/libs/renderer/Font.h b/src/libs/renderer/Font.h index 1902798a4..4e8c565ca 100644 --- a/src/libs/renderer/Font.h +++ b/src/libs/renderer/Font.h @@ -1,5 +1,10 @@ #pragma once +#include "bgfx/bgfx.h" +#include "bgfx_utils.h" + +#include "bx/timer.h" + #include "dx9render.h" #include "matrix.h" #include "vmodule_api.h" @@ -18,8 +23,10 @@ struct IMAGE_VERTEX uint32_t color; float tu, tv; }; + #endif + #define IMAGE_FVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1 | D3DFVF_TEXTUREFORMAT2) #ifndef FLOAT_RECT_DEF diff --git a/src/libs/renderer/primitive_renderer.cpp b/src/libs/renderer/primitive_renderer.cpp new file mode 100644 index 000000000..113d96173 --- /dev/null +++ b/src/libs/renderer/primitive_renderer.cpp @@ -0,0 +1,304 @@ +#include "primitive_renderer.h" +#include "brtshaderc.h" + +#include "matrix.hpp" +#include "gtc/matrix_transform.hpp" +#include "gtc/type_ptr.hpp" + +#include +#include + +bgfx::VertexLayout VERTEX_POSITION_COLOR::pc_layout; +bgfx::VertexLayout VERTEX_POSITION_TEXTURE::pt_layout; +bgfx::VertexLayout VERTEX_POSITION_TEXTURE_COLOR::ptc_layout; + + +PrimitiveRenderer::PrimitiveRenderer(long m_fbWidth, long m_fbHeight) +//SpriteRenderer::SpriteRenderer() +{ + + VERTEX_POSITION_COLOR::Init(); + VERTEX_POSITION_TEXTURE::Init(); + VERTEX_POSITION_TEXTURE_COLOR::Init(); + + + + m_width = m_fbWidth; + m_height = m_fbHeight; + + + const bgfx::Memory *memPrimitiveVsh = shaderc::compileShader(shaderc::ST_VERTEX, "shaders/dx9/vs_primitive.sc"); + const bgfx::Memory *memPrimitiveFsh = shaderc::compileShader(shaderc::ST_FRAGMENT, "shaders/dx9/fs_primitive.sc"); + + bgfx::ShaderHandle primitiveVsh = bgfx::createShader(memPrimitiveVsh); + bgfx::ShaderHandle primitiveFsh = bgfx::createShader(memPrimitiveFsh); + + m_primitiveProg = bgfx::createProgram(primitiveVsh, primitiveFsh, true); + + const bgfx::Memory *memTexturedVsh = shaderc::compileShader(shaderc::ST_VERTEX, "shaders/dx9/vs_textured.sc"); + const bgfx::Memory *memTexturedFsh = shaderc::compileShader(shaderc::ST_FRAGMENT, "shaders/dx9/fs_textured.sc"); + + bgfx::ShaderHandle texturedVsh = bgfx::createShader(memTexturedVsh); + bgfx::ShaderHandle texturedFsh = bgfx::createShader(memTexturedFsh); + + m_texturedProg = bgfx::createProgram(texturedVsh, texturedFsh, true); + + const bgfx::Memory *memTexturedColorVsh = shaderc::compileShader(shaderc::ST_VERTEX, "shaders/dx9/vs_textured_color.sc"); + const bgfx::Memory *memTexturedColorFsh = shaderc::compileShader(shaderc::ST_FRAGMENT, "shaders/dx9/fs_textured_color.sc"); + + bgfx::ShaderHandle texturedColorVsh = bgfx::createShader(memTexturedColorVsh); + bgfx::ShaderHandle texturedColorFsh = bgfx::createShader(memTexturedColorFsh); + + m_texturedColorProg = bgfx::createProgram(texturedColorVsh, texturedColorFsh, true); + + + s_texture = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler); + // u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4); +} + +PrimitiveRenderer::~PrimitiveRenderer() +{ + bgfx::destroy(m_primitiveProg); + bgfx::destroy(m_texturedProg); + bgfx::destroy(m_texturedColorProg); +} + + +void PrimitiveRenderer::ReleaseTexture(std::shared_ptr texture) +{ + bgfx::destroy(*texture->textureHandle); +} + +void PrimitiveRenderer::PushVertices(std::vector &vertices) +{ + Submit(vertices); +} + +void PrimitiveRenderer::PushVertices(std::vector &vertices) +{ + Submit(vertices); +} + +void PrimitiveRenderer::PushVertices(std::vector &vertices) +{ + Submit(vertices); +} + +void PrimitiveRenderer::TestSubmit(std::vector &vertices) +{ + const bgfx::Memory *mem = bgfx::alloc(vertices.size() * sizeof(VERTEX_POSITION_TEXTURE_COLOR)); + + VERTEX_POSITION_TEXTURE_COLOR *vertex = (VERTEX_POSITION_TEXTURE_COLOR *)mem->data; + + int index = 0; + + vertex[index + 0] = VERTEX_POSITION_TEXTURE_COLOR{vertices[index + 0].x, vertices[index + 0].y, + vertices[index + 0].z, vertices[index + 0].tu, + vertices[index + 0].tv, vertices[index + 0].color}; // top left + + vertex[index + 1] = VERTEX_POSITION_TEXTURE_COLOR{vertices[index + 1].x, vertices[index + 1].y, + vertices[index + 0].z, vertices[index + 1].tu, + vertices[index + 1].tv, vertices[index + 1].color}; // top left + + vertex[index + 2] = VERTEX_POSITION_TEXTURE_COLOR{vertices[index + 2].x, vertices[index + 2].y, + vertices[index + 0].z, vertices[index + 2].tu, + vertices[index + 2].tv, vertices[index + 2].color}; // top left + + vertex[index + 3] = VERTEX_POSITION_TEXTURE_COLOR{vertices[index + 3].x, vertices[index + 3].y, + vertices[index + 0].z, vertices[index + 3].tu, + vertices[index + 3].tv, vertices[index + 3].color}; // top left + + std::vector storageVertices = std::vector{ + vertex[index + 0], vertex[index + 1], vertex[index + 2], vertex[index + 3]}; + + Submit(storageVertices); +} + +void PrimitiveRenderer::Submit(std::vector &vertices, std::vector> &uv, + std::vector &colors) +{ + const bgfx::Memory *mem = bgfx::alloc(vertices.size() * sizeof(VERTEX_POSITION_TEXTURE_COLOR)); + + VERTEX_POSITION_TEXTURE_COLOR *vertex = (VERTEX_POSITION_TEXTURE_COLOR *)mem->data; + + int index = 0; + + vertex[index + 0] = VERTEX_POSITION_TEXTURE_COLOR{vertices[index + 0].x, vertices[index + 0].y, 0, uv[index + 0].first, + uv[index + 0].second, colors[index + 0]}; // top left + vertex[index + 1] = VERTEX_POSITION_TEXTURE_COLOR{vertices[index + 1].x, vertices[index + 1].y, 0, uv[index + 1].first, + uv[index + 1].second, colors[index + 1]}; // top right + vertex[index + 2] = VERTEX_POSITION_TEXTURE_COLOR{vertices[index + 2].x, vertices[index + 2].y, 0, uv[index + 2].first, + uv[index + 2].second, colors[index + 2]}; // bottom left + vertex[index + 3] = VERTEX_POSITION_TEXTURE_COLOR{vertices[index + 3].x, vertices[index + 3].y, 0, uv[index + 3].first, + uv[index + 3].second, colors[index + 3]}; // bottom right + + std::vector storageVertices = std::vector{ + vertex[index + 0], vertex[index + 1], vertex[index + 2], vertex[index + 3]}; + + Submit(storageVertices); +} + + + +void PrimitiveRenderer::Submit(std::vector &vertices) +{ + + bgfx::TransientVertexBuffer vb; + bgfx::allocTransientVertexBuffer(&vb, vertices.size(), VERTEX_POSITION_COLOR::pc_layout); + VERTEX_POSITION_COLOR *vertex = (VERTEX_POSITION_COLOR *)vb.data; + + for (uint16_t i = 0, v = 0; i < (vertices.size() / 4); i += 4, v += 4) + { + vertex[v + 0] = vertices[i + 0]; + vertex[v + 1] = vertices[i + 1]; + vertex[v + 2] = vertices[i + 2]; + vertex[v + 3] = vertices[i + 3]; + } + + const uint16_t indicesCount = (vertices.size() / 4) * 6; + + std::vector indices; + indices.reserve(indicesCount); + + for (uint16_t i = 0; i < indicesCount; i += 4) + { + indices.push_back(i); + indices.push_back(i + 1); + indices.push_back(i + 2); + + indices.push_back(i + 1); + indices.push_back(i + 3); + indices.push_back(i + 2); + } + + bgfx::TransientIndexBuffer ib; + + bgfx::allocTransientIndexBuffer(&ib, indicesCount); + + auto ibPtr = reinterpret_cast(ib.data); + + for (long i = 0; i < indices.size(); ++i) + { + ibPtr[i] = indices[i]; + } + + uint64_t state = BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_BLEND_ALPHA; + + bgfx::setViewFrameBuffer(1, BGFX_INVALID_HANDLE); + + bgfx::setState(state); + + bgfx::setVertexBuffer(0, &vb); + bgfx::setIndexBuffer(&ib, 0, (vertices.size() / 4) * 6); + bgfx::submit(1, m_primitiveProg); +} + + +void PrimitiveRenderer::Submit(std::vector &vertices) +{ + + bgfx::TransientVertexBuffer vb; + bgfx::allocTransientVertexBuffer(&vb, vertices.size(), VERTEX_POSITION_TEXTURE::pt_layout); + VERTEX_POSITION_TEXTURE *vertex = (VERTEX_POSITION_TEXTURE *)vb.data; + + for (uint16_t i = 0, v = 0; i < (vertices.size() / 4); i += 4, v += 4) + { + vertex[v + 0] = vertices[i + 0]; + vertex[v + 1] = vertices[i + 1]; + vertex[v + 2] = vertices[i + 2]; + vertex[v + 3] = vertices[i + 3]; + } + + const uint16_t indicesCount = (vertices.size() / 4) * 6; + + std::vector indices; + indices.reserve(indicesCount); + + for (uint16_t i = 0; i < indicesCount; i += 4) + { + indices.push_back(i); + indices.push_back(i + 1); + indices.push_back(i + 2); + + indices.push_back(i + 1); + indices.push_back(i + 3); + indices.push_back(i + 2); + } + + bgfx::TransientIndexBuffer ib; + + bgfx::allocTransientIndexBuffer(&ib, indicesCount); + + auto ibPtr = reinterpret_cast(ib.data); + + for (long i = 0; i < indices.size(); ++i) + { + ibPtr[i] = indices[i]; + } + + uint64_t state = BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_BLEND_ALPHA; + + bgfx::setViewFrameBuffer(1, BGFX_INVALID_HANDLE); + + bgfx::setState(state); + + bgfx::setTexture(0, s_texture, *Texture->textureHandle); + bgfx::setVertexBuffer(0, &vb); + bgfx::setIndexBuffer(&ib, 0, (vertices.size() / 4) * 6); + bgfx::submit(1, m_texturedProg); +} + +void PrimitiveRenderer::Submit(std::vector &vertices) +{ + + bgfx::TransientVertexBuffer vb; + bgfx::allocTransientVertexBuffer(&vb, vertices.size(), VERTEX_POSITION_TEXTURE_COLOR::ptc_layout); + VERTEX_POSITION_TEXTURE_COLOR *vertex = (VERTEX_POSITION_TEXTURE_COLOR *)vb.data; + + for (uint16_t i = 0, v = 0; i < (vertices.size() / 4); i += 4, v += 4) + { + vertex[v + 0] = vertices[i + 0]; + vertex[v + 1] = vertices[i + 1]; + vertex[v + 2] = vertices[i + 2]; + vertex[v + 3] = vertices[i + 3]; + } + + const uint16_t indicesCount = (vertices.size() / 4) * 6; + + std::vector indices; + indices.reserve(indicesCount); + + for (uint16_t i = 0; i < indicesCount; i += 4) + { + indices.push_back(i); + indices.push_back(i + 1); + indices.push_back(i + 2); + + indices.push_back(i + 1); + indices.push_back(i + 3); + indices.push_back(i + 2); + } + + bgfx::TransientIndexBuffer ib; + + bgfx::allocTransientIndexBuffer(&ib, indicesCount); + + auto ibPtr = reinterpret_cast(ib.data); + + for (long i = 0; i < indices.size(); ++i) + { + ibPtr[i] = indices[i]; + } + + uint64_t state = BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_BLEND_ALPHA; + + bgfx::setViewFrameBuffer(1, BGFX_INVALID_HANDLE); + + bgfx::setState(state); + + bgfx::setTexture(0, s_texture, *Texture->textureHandle); + bgfx::setVertexBuffer(0, &vb); + bgfx::setIndexBuffer(&ib, 0, (vertices.size() / 4) * 6); + bgfx::submit(1, m_texturedColorProg); + +} \ No newline at end of file diff --git a/src/libs/renderer/sdevice.cpp b/src/libs/renderer/sdevice.cpp index b2bacd851..ab970774d 100644 --- a/src/libs/renderer/sdevice.cpp +++ b/src/libs/renderer/sdevice.cpp @@ -1,3 +1,12 @@ +#include "bgfx_utils.h" + +#include "glm.hpp" +#include "gtx/matrix_transform_2d.hpp" + +#include "gtc/matrix_transform.hpp" +#include "gtc/type_ptr.hpp" +#include "matrix.hpp" + #include "sdevice.h" #include "core.h" @@ -13,6 +22,12 @@ #include #include +#include +#include + +#include + + #define POST_PROCESS_FVF (D3DFVF_XYZRHW | D3DFVF_TEX4) #define S_RELEASE(a, b) \ @@ -22,8 +37,11 @@ a = NULL; \ } +static bool s_isDXEndScene = false; + DX9RENDER *DX9RENDER::pRS = nullptr; +bgfx::VertexLayout DX9RENDER::PosColorTexVertex::ms_layout; uint32_t DX9SetTexturePath(VS_STACK *pS) { auto *pString = (VDATA *)pS->Pop(); @@ -285,7 +303,7 @@ inline bool DX9RENDER::ErrorHandler(HRESULT hr, const char *file, unsigned line, const uint32_t DX9RENDER::rectsVBuffer_SizeInRects = 512; //################################################################################ -DX9RENDER::DX9RENDER() +DX9RENDER::DX9RENDER() : m_spriteRenderer(nullptr), m_primitiveRenderer(nullptr) { rectsVBuffer = nullptr; @@ -368,6 +386,9 @@ DX9RENDER::DX9RENDER() vViewRelationPos = CVECTOR(0.f, 0.f, 0.f); vWordRelationPos = -vViewRelationPos; + + bgfxvViewRelationPos = CVECTOR(0.f, 0.f, 0.f); + bgfxvWordRelationPos = -vViewRelationPos; bUseLargeBackBuffer = false; } @@ -385,6 +406,7 @@ bool DX9RENDER::Init() d3d = nullptr; d3d9 = nullptr; + auto ini = fio->OpenIniFile(core.EngineIniFileName()); if (ini) { @@ -425,6 +447,9 @@ bool DX9RENDER::Init() stencil_format = D3DFMT_D16; } + m_spriteRenderer = std::make_shared(screen_size.x, screen_size.y); + m_primitiveRenderer = std::make_shared(screen_size.x, screen_size.y); + // stencil_format = D3DFMT_D24S8; if (!InitDevice(bWindow, core.GetAppHWND(), screen_size.x, screen_size.y)) return false; @@ -496,7 +521,8 @@ bool DX9RENDER::Init() rectsVBuffer = nullptr; d3d9->CreateVertexBuffer(2 * sizeof(CVECTOR), D3DUSAGE_WRITEONLY, D3DFVF_XYZ, D3DPOOL_MANAGED, &pDropConveyorVBuffer, nullptr); - d3d9->CreateVertexBuffer(rectsVBuffer_SizeInRects * 6 * sizeof(RECT_VERTEX), D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, + d3d9->CreateVertexBuffer(rectsVBuffer_SizeInRects * 6 * sizeof(PosColorTexVertex), + D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, RS_RECT_VERTEX_FORMAT, D3DPOOL_DEFAULT, &rectsVBuffer, nullptr); // D3DPOOL_MANAGED if (!pDropConveyorVBuffer || !rectsVBuffer) { @@ -653,7 +679,7 @@ bool DX9RENDER::InitDevice(bool windowed, HWND _hwnd, long width, long height) if (!windowed) { d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; - // d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; + //d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; } if (d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &d3d9) != @@ -804,13 +830,25 @@ bool DX9RENDER::ReleaseDevice() bool res = true; for (long t = 0; t < MAX_STEXTURES; t++) + { if (Textures[t].ref && Textures[t].loaded && Textures[t].d3dtex) { if (CHECKD3DERR(Textures[t].d3dtex->Release()) == false) res = false; Textures[t].ref = NULL; delete Textures[t].name; + } + } + + for (long t = 0; t < BGFXTextures.size(); t++) + { + if (BGFXTextures[t].ref && BGFXTextures[t].loaded && BGFXTextures[t].textureHandle->idx) + { + bgfx::destroy(*BGFXTextures[t].textureHandle); + BGFXTextures[t].ref = NULL; + delete BGFXTextures[t].name; } + } if (d3d9) d3d9->SetGammaRamp(0, D3DSGR_NO_CALIBRATION, &DefaultRamp); @@ -829,6 +867,10 @@ bool DX9RENDER::DX9Clear(long type) { if (CHECKD3DERR(d3d9->Clear(0L, NULL, type, dwBackColor, 1.0f, 0L)) == true) return false; + + bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH | BGFX_CLEAR_STENCIL, dwBackColor, 1.0f, 0); + bgfx::setViewClear(1, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH | BGFX_CLEAR_STENCIL, dwBackColor, 1.0f, 0); + // if(CHECKD3DERR(d3d9->Clear(0L, NULL, type, 0x0, 1.0f, 0L))==true) return false; return true; } @@ -1122,9 +1164,12 @@ bool DX9RENDER::DX9EndScene() DrawPrimitive(D3DPT_LINELIST, 0, 1); } + s_isDXEndScene = true; if (CHECKD3DERR(EndScene())) return false; + s_isDXEndScene = false; + // boal if (bMakeShoot || core.Controls->GetDebugAsyncKeyState(VK_F8) < 0) MakeScreenShot(); if (bMakeShoot || GetAsyncKeyState(VK_F8) < 0) MakeScreenShot(); @@ -1132,8 +1177,15 @@ bool DX9RENDER::DX9EndScene() if (bVideoCapture) MakeCapture(); - const HRESULT hRes = d3d9->Present(nullptr, nullptr, nullptr, nullptr); + // if (bMakeShoot || GetAsyncKeyState(VK_F9) < 0) + BGFXRenderToBackBuffer(); + GetSpriteRenderer()->Submit(); + bgfx::frame(); + + + const HRESULT hRes = d3d9->Present(nullptr, nullptr, nullptr, nullptr); + if (hRes == D3DERR_DEVICELOST) { bDeviceLost = true; @@ -1273,6 +1325,660 @@ long DX9RENDER::TextureCreate(const char *fname) return -1; } +bool find_file(const std::filesystem::path &dir_path, // in this directory, + const std::wstring &file_name, // search for this name, + std::filesystem::path &path_found) // placing path here if found +{ + if (!exists(dir_path)) + return false; + std::filesystem::directory_iterator end_itr; // default construction yields past-the-end + for (std::filesystem::directory_iterator itr(dir_path); itr != end_itr; ++itr) + { + if (std::filesystem::is_directory(itr->status())) + { + if (find_file(itr->path(), file_name, path_found)) + return true; + } + else // see below + { + auto comparing_path = itr->path().filename(); + auto comparing_path_string = comparing_path.wstring(); + std::transform(comparing_path_string.begin(), comparing_path_string.end(), + comparing_path_string.begin(), ::toupper); + + if (comparing_path_string.compare(file_name) == 0) + { + path_found = itr->path(); + return true; + } + + } + } + return false; +} + +long DX9RENDER::BGFXTextureCreate(const char *fname) +{ + // start add texture path + if ((uintptr_t)fname == -1) + { + iSetupPath = 1; + return -1; + } + + // continue add texture path process + if (iSetupPath) + { + if (iSetupPath == 1) + { + dwSetupNumber = (uintptr_t)(fname); + iSetupPath = 2; + return -1; + } + + TexPaths[dwSetupNumber].str[0] = 0; + if (fname && fname[0]) + strcpy_s(TexPaths[dwSetupNumber].str, fname); + iSetupPath = 0; + return -1; + } + + // delete relative path "resource\textures\" + // std::string sTexName = fname; + // sTexName.GetRelativePath("resource\\textures\\"); + // sTexName = sTexName - std::string(".tx"); + // ~!~ + //__debugbreak(); + // fs::path path = fs::path() / "resource" / "textures" / fname; + + if (fname == nullptr) + { + core.Trace("Can't create texture with null name"); + return -1; + } + + fs::path path = fname; + std::string pathStr = path.extension().string(); + if (_stricmp(pathStr.c_str(), ".tx") == 0) + path.replace_extension(); + pathStr = path.string(); + fname = pathStr.c_str(); //~!~ msvc still doesn't have working c_str for path + + if (!bLoadTextureEnabled) + return -1; + + size_t fname_len = strlen(fname); + + for (long i = 3; i >= -1; i--) + { + char _fname[256]; + + if (i >= 0 && TexPaths[i].str[0] == 0) + continue; + if (i >= 0) + { + const uint32_t dwLen = strlen(fname); + + long j; + for (j = dwLen - 1; j >= 0; j--) + if (fname[j] == '\\') + break; + + _fname[0] = 0; + strncpy_s(_fname, fname, j + 1); + _fname[j + 1] = 0; + strcat_s(_fname, TexPaths[i].str); + strcat_s(_fname, &fname[j + 1]); + bTrace = false; + } + else + { + bTrace = true; + strcpy_s(_fname, fname); + } + + if (strlen(_fname) > _countof(".tx") - 1) + { + if (_stricmp(&_fname[strlen(_fname) - 3], ".tx") == 0) + _fname[strlen(_fname) - 3] = 0; + } + + _strupr(_fname); + + const unsigned long hf = hash_string(_fname); + + long t; + for (t = 0; t < BGFXTextures.size(); t++) + if (BGFXTextures[t].ref != 0) + if (BGFXTextures[t].name) + if (BGFXTextures[t].hash == hf && _stricmp(BGFXTextures[t].name, _fname) == 0) + { + BGFXTextures[t].ref++; + return t; + } + + int textureIndex = -1; + + for (int i = 0; i_OldReadFile(origPath.wstring(), &head, sizeof(head), &readingBytes) || readingBytes != sizeof(head)) + { + if (bTrace) + { + core.Trace("Can't load texture %s", fn); + } + delete BGFXTextures[t].name; + BGFXTextures[t].name = nullptr; + return false; + } + // Analyzing the format + D3DFORMAT d3dFormat = D3DFMT_UNKNOWN; + long textureFI; + for (textureFI = 0; textureFI < sizeof(textureFormats) / sizeof(SD_TEXTURE_FORMAT); textureFI++) + if (textureFormats[textureFI].txFormat == head.format) + break; + if (textureFI == sizeof(textureFormats) / sizeof(SD_TEXTURE_FORMAT) || head.flags & TX_FLAGS_PALLETTE) + { + if (bTrace) + core.Trace("Invalidate texture format %s, not loading it.", fn); + delete Textures[t].name; + Textures[t].name = nullptr; + return false; + } + + d3dFormat = textureFormats[textureFI].d3dFormat; + bool isSwizzled = textureFormats[textureFI].isSwizzled; + const char *formatTxt = textureFormats[textureFI].format; + // Skipping mips + uint32_t seekposition = 0; + for (long nTD = nTextureDegradation; nTD > 0; nTD--) + { + if (head.nmips <= 1 || head.width <= 32 || head.height <= 32) + break; // degradation limit + seekposition += head.mip_size; + head.nmips--; + head.width /= 2; + head.height /= 2; + head.mip_size /= 4; + } + + seekposition += readingBytes; + + // Loading the texture + if (!(head.flags & TX_FLAGS_CUBEMAP)) + { + // Loading a regular texture + // Position in file + // + // create the texture + + auto bgfxFormat = bgfx::TextureFormat::Enum::Unknown; + + // TXF_A8R8G8B8 = 21, TXF_X8R8G8B8 = 22, TXF_R5G6B5 = 23, TXF_A1R5G5B5 = 25, TXF_A4R4G4B4 = 26 // reference, // basic texture formats + + switch (textureFormats[textureFI].txFormat) + { + case TXF_A8R8G8B8: + bgfxFormat = bgfx::TextureFormat::Enum::BGRA8; + break; + case TXF_R5G6B5: + bgfxFormat = bgfx::TextureFormat::Enum::R5G6B5; + break; + case TXF_A1R5G5B5: + bgfxFormat = bgfx::TextureFormat::Enum::RGB5A1; + break; + case TXF_A4R4G4B4: + bgfxFormat = bgfx::TextureFormat::Enum::RGBA4; + break; + + case TXF_X8R8G8B8: + default: + throw std::exception("Not implemented yet"); + } + // Filling the levels + for (long m = 0; m < head.nmips; m++) + { + // take into account the size of the mip + BGFXTextures[t].dwSize += head.mip_size; + // Getting the mip surface + bool isError = false; + { + std::vector byteOutput; + + byteOutput.resize(head.mip_size); + + isError = + !fio->_OldReadFile(origPath.wstring(), byteOutput.data(), head.mip_size, &readingBytes, seekposition); + + switch (textureFormats[textureFI].txFormat) + { + case TXF_A8R8G8B8: + { + + unsigned char *data = byteOutput.data(); + + for (int i = 0; i < head.mip_size; i+=4) + { + unsigned char a = data[i + 0]; + unsigned char r = data[i + 1]; + unsigned char g = data[i + 2]; + unsigned char b = data[i + 3]; + + data[i + 0] = b; + data[i + 1] = g; + data[i + 2] = r; + data[i + 3] = a; + } + + } + break; + case TXF_A1R5G5B5: + { + + + /*auto bitvalue = [](int8_t num, int bit) + { + if (bit > 0 && bit <= 8) + return ((num >> (bit - 1)) & 1); + else + return 0; + }; + + unsigned char *data = byteOutput.data(); + + for (int i = 0; i < head.mip_size; i += 2) + { + + std::deque bits; + + unsigned char low = data[i + 0]; + unsigned char high = data[i + 1]; + + bits.push_back(bitvalue(low, 1)); + bits.push_back(bitvalue(low, 2)); + bits.push_back(bitvalue(low, 3)); + bits.push_back(bitvalue(low, 4)); + bits.push_back(bitvalue(low, 5)); + bits.push_back(bitvalue(low, 6)); + bits.push_back(bitvalue(low, 7)); + bits.push_back(bitvalue(low, 8)); + + bits.push_back(bitvalue(high, 1)); + bits.push_back(bitvalue(high, 2)); + bits.push_back(bitvalue(high, 3)); + bits.push_back(bitvalue(high, 4)); + bits.push_back(bitvalue(high, 5)); + bits.push_back(bitvalue(high, 6)); + bits.push_back(bitvalue(high, 7)); + bits.push_back(bitvalue(high, 8)); + + unsigned char a = bits.front(); + + bits.pop_front(); + + bits.push_back(a); + + std::vector new_bits; + + for (auto& bit : bits) + { + new_bits.push_back(bit); + } + + memcpy(data + i, new_bits.data(), 2); + }*/ + } + break; + case TXF_A4R4G4B4: + { + /*unsigned char *data = byteOutput.data(); + + for (int i = 0; i < head.mip_size; i += 2) + { + + std::deque bits; + + unsigned char low = data[i + 0]; + unsigned char high = data[i + 1]; + + std::bitset<8> bitLow(low); + std::bitset<8> bitHigh(high); + + std::bitset<8> bitLowBackup(low); + std::bitset<8> bitHighBackup(high); + + + bitLow[0] = bitLowBackup[1]; + bitLow[1] = bitLowBackup[2]; + bitLow[2] = bitLowBackup[3]; + bitLow[3] = bitLowBackup[4]; + + bitLow[4] = bitLowBackup[5]; + bitLow[5] = bitLowBackup[6]; + bitLow[6] = bitLowBackup[7]; + bitLow[7] = bitHighBackup[0]; + + bitHigh[0] = bitHighBackup[1]; + bitHigh[1] = bitHighBackup[2]; + bitHigh[2] = bitHighBackup[3]; + bitHigh[3] = bitHighBackup[4]; + + bitHigh[4] = bitHighBackup[5]; + bitHigh[5] = bitHighBackup[6]; + bitHigh[6] = bitHighBackup[7]; + bitHigh[7] = bitLowBackup[0]; + + unsigned long bitLowLong= bitLow.to_ulong(); + unsigned long bitHighLong = bitHigh.to_ulong(); + + data[i + 0] = static_cast(bitLowLong); + + + data[i + 1] = static_cast(bitHighLong); + + }*/ + } + + + break; + + case TXF_R5G6B5: { + /*unsigned char *data = byteOutput.data(); + + for (int i = 0; i < head.mip_size; i += 2) + { + + std::deque bits; + + unsigned char low = data[i + 0]; + unsigned char high = data[i + 1]; + + std::bitset<8> bitLow(low); + std::bitset<8> bitHigh(high); + + std::bitset<8> bitLowBackup(low); + std::bitset<8> bitHighBackup(high); + + + bitLow[0] = bitHighBackup[3]; + bitLow[1] = bitHighBackup[4]; + bitLow[2] = bitHighBackup[5]; + bitLow[3] = bitHighBackup[6]; + bitLow[4] = bitHighBackup[7]; + + bitHigh[3] = bitLowBackup[0]; + bitHigh[4] = bitLowBackup[1]; + bitHigh[5] = bitLowBackup[2]; + bitHigh[6] = bitLowBackup[3]; + bitHigh[7] = bitLowBackup[4]; + + unsigned long bitLowLong= bitLow.to_ulong(); + unsigned long bitHighLong = bitHigh.to_ulong(); + + data[i + 0] = static_cast(bitLowLong); + data[i + 1] = static_cast(bitHighLong); + + }*/ + } + + + break; + + case TXF_X8R8G8B8: + default: + throw std::exception("Not implemented yet"); + } + + + auto textureResource = loadMemoryTexture(origPath.string(), bgfxFormat, head.width, head.height, 1, + false, false, 0, byteOutput.data()); + + auto textureHandle = textureResource->textureHandle; + BGFXTextures[t].textureHandle = textureHandle; + BGFXTextures[t].size.width = head.width; + BGFXTextures[t].size.height = head.height; + + } + // If there was an error, then interrupt the download + if (isError) + { + if (bTrace) + core.Trace("Can't loading mip %i, texture %s is not created (width: %i, height: %i, num mips: %i, " + "format: %s), not loading it.", + m, fn, head.width, head.height, head.nmips, formatTxt); + delete BGFXTextures[t].name; + BGFXTextures[t].name = nullptr; + return false; + } + // recalculate the dimensions for the next mip + head.width /= 2; + head.height /= 2; + head.mip_size /= 4; + } + BGFXTextures[t].isCubeMap = false; + } + else + { + /* + // Download cubemap + if (head.width != head.height) + { + if (bTrace) + core.Trace("Cube map texture can't has not squared sides %s, not loading it.", fn); + delete Textures[t].name; + Textures[t].name = nullptr; + return false; + } + // Number of mips + D3DCAPS9 devcaps; + if (CHECKD3DERR(d3d9->GetDeviceCaps(&devcaps))) + { + if (bTrace) + core.Trace("Cube map texture %s is not created (size: %i, num mips: %i, format: %s), not loading it.", + fn, head.width, head.nmips, formatTxt); + delete Textures[t].name; + Textures[t].name = nullptr; + return false; + } + if (!(devcaps.TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP)) + head.nmips = 1; + // create the texture + IDirect3DCubeTexture9 *tex = nullptr; + if (CHECKD3DERR(d3d9->CreateCubeTexture(head.width, head.nmips, 0, d3dFormat, D3DPOOL_MANAGED, &tex, NULL)) == + true || + !tex) + { + if (bTrace) + core.Trace("Cube map texture %s is not created (size: %i, num mips: %i, format: %s), not loading it.", + fn, head.width, head.nmips, formatTxt); + delete Textures[t].name; + Textures[t].name = nullptr; + return false; + } + // Loading the sides + bool isError = false; + if (seekposition) + seekposition += seekposition; + + uint32_t sz = + BGFXLoadCubmapSide(origPath.wstring(), tex, D3DCUBEMAP_FACE_POSITIVE_Z, head.nmips, head.mip_size, head.width, isSwizzled, seekposition); + if (sz) + { + Textures[t].dwSize += sz; + if (seekposition) + seekposition += seekposition; + sz = BGFXLoadCubmapSide(origPath.wstring(), tex, D3DCUBEMAP_FACE_POSITIVE_X, head.nmips, head.mip_size, + head.width, isSwizzled, seekposition); + if (sz) + { + Textures[t].dwSize += sz; + if (seekposition) + seekposition += seekposition; + sz = BGFXLoadCubmapSide(origPath.wstring(), tex, D3DCUBEMAP_FACE_NEGATIVE_Z, head.nmips, head.mip_size, + head.width, + isSwizzled, seekposition); + if (sz) + { + Textures[t].dwSize += sz; + if (seekposition) + seekposition += seekposition; + sz = BGFXLoadCubmapSide(origPath.wstring(), tex, D3DCUBEMAP_FACE_NEGATIVE_X, head.nmips, + head.mip_size, + head.width, + isSwizzled, seekposition); + if (sz) + { + Textures[t].dwSize += sz; + if (seekposition) + seekposition += seekposition; + sz = BGFXLoadCubmapSide(origPath.wstring(), tex, D3DCUBEMAP_FACE_POSITIVE_Y, head.nmips, + head.mip_size, + head.width, isSwizzled, seekposition); + if (sz) + { + Textures[t].dwSize += sz; + if (seekposition) + seekposition += seekposition; + sz = BGFXLoadCubmapSide(origPath.wstring(), tex, D3DCUBEMAP_FACE_NEGATIVE_Y, head.nmips, + head.mip_size, + head.width, isSwizzled, seekposition); + if (!sz) + isError = true; + Textures[t].dwSize += sz; + } + else + isError = true; + } + else + isError = true; + } + else + isError = true; + } + else + isError = true; + } + else + isError = true; + if (isError) + { + if (bTrace) + core.Trace("Cube map texture %s can't loading (size: %i, num mips: %i, format: %s), not loading it.", + fn, head.width, head.nmips, formatTxt); + delete Textures[t].name; + Textures[t].name = nullptr; + tex->Release(); + return false; + } + Textures[t].d3dtex = tex; + Textures[t].isCubeMap = true;*/ + + throw std::exception("Not implemented yet"); + } + + //--------------------------------------------------------------- + // print statistics + //--------------------------------------------------------------- + if (texLog) + { + char s[256]; + if (totSize == 0) + { + fio->_DeleteFile("texLoad.txt"); + } + auto fileS2 = fio->_CreateFile("texLoad.txt", std::ios::binary | std::ios::out | std::ios::app); + totSize += Textures[t].dwSize; + sprintf_s(s, "%.2f, size: %d, %d * %d, %s\n", totSize / 1024.0f / 1024.0f, Textures[t].dwSize, head.width, + head.height, Textures[t].name); + fio->_WriteFile(fileS2, s, strlen(s)); + fio->_FlushFileBuffers(fileS2); + fio->_CloseFile(fileS2); + } + dwTotalSize += BGFXTextures[t].dwSize; + //--------------------------------------------------------------- + BGFXTextures[t].loaded = true; + // Close the file + return true; +} + + bool DX9RENDER::TextureLoad(long t) { ProgressView(); @@ -1607,6 +2313,44 @@ IDirect3DBaseTexture9 *DX9RENDER::GetBaseTexture(long iTexture) return (iTexture >= 0) ? Textures[iTexture].d3dtex : nullptr; } +uint32_t DX9RENDER::BGFXLoadCubmapSide(std::wstring file, IDirect3DCubeTexture9 *tex, D3DCUBEMAP_FACES face, uint32_t numMips, + uint32_t mipSize, uint32_t size, bool isSwizzled, uint32_t seek_to) +{ + uint32_t texsize = 0; + // Filling the levels + for (uint32_t m = 0; m < numMips; m++) + { + // take into account the size of the mip + texsize += mipSize; + // Getting the mip surface + bool isError = false; + IDirect3DSurface9 *surface = nullptr; + if (CHECKD3DERR(tex->GetCubeMapSurface(face, m, &surface)) == true || !surface) + { + isError = true; + } + else + { + // read the mip + isError = !BGFXLoadTextureSurface(file, surface, mipSize, size, size, isSwizzled, seek_to); + } + // Freeing the surface + if (surface) + surface->Release(); + // If there was an error, then interrupt the download + if (isError) + { + if (bTrace) + core.Trace("Can't loading cubemap mip %i (side: %i), not loading it.", m, face); + return 0; + } + // recalculate the dimensions for the next mip + size /= 2; + mipSize /= 4; + } + return texsize; +} + uint32_t DX9RENDER::LoadCubmapSide(std::fstream &fileS, IDirect3DCubeTexture9 *tex, D3DCUBEMAP_FACES face, uint32_t numMips, uint32_t mipSize, uint32_t size, bool isSwizzled) { @@ -1646,7 +2390,32 @@ uint32_t DX9RENDER::LoadCubmapSide(std::fstream &fileS, IDirect3DCubeTexture9 *t size /= 2; mipSize /= 4; } - return texsize; + return texsize; +} + +bool DX9RENDER::BGFXLoadTextureSurface(std::filesystem::path file, IDirect3DSurface9 *suface, uint32_t mipSize, uint32_t width, + uint32_t height, bool isSwizzled, uint32_t seek_to) +{ + //------------------------------------------------------------------------------------------ + // PC version + // ------------------------------------------------------------------------------------------ + // Surface pointer + D3DLOCKED_RECT lock; + if (CHECKD3DERR(suface->LockRect(&lock, NULL, 0L)) == true) + return false; + // Reading out + uint32_t readingBytes = 0; + if (!fio->_OldReadFile(file.wstring(), lock.pBits, mipSize, &readingBytes, seek_to) || readingBytes != mipSize) + { + if (CHECKD3DERR(suface->UnlockRect()) == true) + return false; + return false; + } + // Surface release + if (CHECKD3DERR(suface->UnlockRect()) == true) + return false; + return true; + //------------------------------------------------------------------------------------------ } bool DX9RENDER::LoadTextureSurface(std::fstream &fileS, IDirect3DSurface9 *suface, uint32_t mipSize, uint32_t width, @@ -1799,6 +2568,57 @@ bool DX9RENDER::TextureRelease(long texid) return true; } +bool DX9RENDER::BGFXTextureRelease(long texid) +{ + if (texid == -1) + return true; + + BGFXTextures[texid].ref--; + if (BGFXTextures[texid].ref != 0) + return false; + + if (BGFXTextures[texid].name != nullptr) + { + if (texLog) + { + auto fh = fio->_CreateFile("texLoad.txt", std::ios::binary | std::ios::in | std::ios::out); + + totSize -= BGFXTextures[texid].dwSize; + const int bytes = fio->_GetFileSize("texLoad.txt"); + char *buf = new char[bytes + 1]; + fio->_ReadFile(fh, buf, bytes); + buf[bytes] = 0; + + char *str = strstr(buf, BGFXTextures[texid].name); + if (str != nullptr) + { + fio->_SetFilePointer(fh, str - buf, std::ios::beg); + const char *s = "*"; + fio->_WriteFile(fh, s, 1); + } + delete[] buf; + fio->_FlushFileBuffers(fh); + } + + delete BGFXTextures[texid].name; + BGFXTextures[texid].name = nullptr; + } + + if (BGFXTextures[texid].loaded == false) + return false; + + bgfx::destroy(*BGFXTextures[texid].textureHandle); + + + dwTotalSize -= BGFXTextures[texid].dwSize; + + BGFXTextures[texid].loaded = false; + + return true; +} + + + //################################################################################ bool DX9RENDER::SetCamera(const CVECTOR &pos, const CVECTOR &ang, float fov) { @@ -1844,11 +2664,13 @@ bool DX9RENDER::SetCamera(const CVECTOR &pos, const CVECTOR &ang) Pos = pos; Ang = ang; vWordRelationPos = -Pos; + bgfxvWordRelationPos = -Pos; } // if(CHECKD3DERR(SetTransform(D3DTS_VIEW, mtx))==true) return false; d3d9->SetTransform(D3DTS_VIEW, mtx); vViewRelationPos = -(mtx * vWordRelationPos); + bgfxvViewRelationPos = -(mtx * bgfxvWordRelationPos); d3d9->GetTransform(D3DTS_WORLD, mtx); mtx.Pos() += vWordRelationPos - vOldWordRelationPos; d3d9->SetTransform(D3DTS_WORLD, mtx); @@ -1914,17 +2736,36 @@ bool DX9RENDER::SetPerspective(float perspective, float fAspectRatio) const float h = 1.0f / tanf(fov_vert * 0.5f); const float Q = far_plane / (far_plane - near_plane); - D3DXMATRIX mtx; + CMatrix mtx; PZERO(&mtx, sizeof(mtx)); - mtx._11 = w; + mtx.m[0][0] = w; + mtx.m[1][1] = h; + mtx.m[2][2] = Q; + mtx.m[3][2] = -Q * near_plane; + mtx.m[2][3] = 1.0f; + + /*mtx._11 = w; mtx._22 = h; mtx._33 = Q; mtx._43 = -Q * near_plane; - mtx._34 = 1.0f; + mtx._34 = 1.0f;*/ - if (CHECKD3DERR(d3d9->SetTransform(D3DTS_PROJECTION, &mtx)) == true) + if (CHECKD3DERR(d3d9->SetTransform(D3DTS_PROJECTION, (D3DXMATRIX *)&mtx)) == true) return false; + + CMatrix viewMtx; + + d3d9->GetTransform(D3DTS_VIEW, (D3DXMATRIX*)&viewMtx); + + CMatrix transposedView = viewMtx; + transposedView.Transposition(); + + CMatrix transposedProj = mtx; + transposedProj.Transposition(); + + bgfx::setViewTransform(1, transposedView.matrix, transposedProj.matrix); + Fov = perspective; FindPlanes(d3d9); return true; @@ -2281,6 +3122,7 @@ void DX9RENDER::SetTransform(long type, D3DMATRIX *mtx) vDeltaWorld -= vWordRelationPos; CMatrix mw; d3d9->GetTransform(D3DTS_WORLD, (D3DMATRIX *)&mw); + mw.Pos() -= vDeltaWorld; d3d9->SetTransform(D3DTS_WORLD, mw); @@ -2292,6 +3134,9 @@ void DX9RENDER::SetTransform(long type, D3DMATRIX *mtx) } CHECKD3DERR(d3d9->SetTransform(static_cast(type), (D3DMATRIX *)&m)); + + + //BGFXSetTransform(type, mtx); } void DX9RENDER::GetTransform(long type, D3DMATRIX *mtx) @@ -2312,6 +3157,146 @@ void DX9RENDER::GetTransform(long type, D3DMATRIX *mtx) } } +void DX9RENDER::BGFXSetTransform(long type, D3DMATRIX *mtx) +{ + CMatrix m = *(CMatrix *)mtx; + if (type == D3DTS_VIEW) + { + //m = m.Transpose(); + + bgfxvViewRelationPos.x = -((D3DMATRIX*)&m)->_41; + bgfxvViewRelationPos.y = -((D3DMATRIX *)&m)->_42; + bgfxvViewRelationPos.z = -((D3DMATRIX *)&m)->_43; + + m.Pos() += bgfxvViewRelationPos; + } + else if (type == D3DTS_WORLD) + { + m.Pos() += bgfxvWordRelationPos; + } + + switch (type) + { + case D3DTS_VIEW: { + CMatrix mv = m; + mv = mv.Transpose(); + + bgfxView = mv; + } + break; + + case D3DTS_PROJECTION: { + CMatrix mp = m; + + mp = mp.Transpose(); + + bgfxProjection = mp; + + } + break; + + case D3DTS_WORLD: + CMatrix wMat = m; + wMat = wMat.Transpose(); + bgfxWorld = wMat; + break; + } +} + + +void DX9RENDER::BGFXSetTransformUpdateViews(long type, D3DMATRIX *mtx) +{ + CMatrix m = *(CMatrix *)mtx; + if (type == D3DTS_VIEW) + { + //m = m.Transpose(); + bgfxvViewRelationPos.x = -((D3DMATRIX *)&m)->_41; + bgfxvViewRelationPos.y = -((D3DMATRIX *)&m)->_42; + bgfxvViewRelationPos.z = -((D3DMATRIX *)&m)->_43; + + m.Pos() += bgfxvViewRelationPos; + } + else if (type == D3DTS_WORLD) + { + m.Pos() += bgfxvWordRelationPos; + } + + switch (type) + { + case D3DTS_VIEW: { + CMatrix mv = m; + + mv = mv.Transpose(); + + bgfxView = mv; + + bgfx::setViewTransform(1, bgfxView.matrix, bgfxProjection.matrix); + } + break; + + case D3DTS_PROJECTION: { + CMatrix mp = m; + + mp = mp.Transpose(); + + bgfxProjection = mp; + + bgfx::setViewTransform(1, bgfxView.matrix, bgfxProjection.matrix); + + // bgfx::setViewTransform(1, bgfxView.matrix, bgfxProjection.matrix); + } + break; + + case D3DTS_WORLD: + CMatrix wMat = m; + wMat = wMat.Transpose(); + bgfxWorld = wMat; + bgfx::setTransform(bgfxWorld.matrix); + break; + } +} + +void DX9RENDER::BGFXGetTransform(long type, D3DMATRIX *mtx) +{ + switch (type) + { + case D3DTS_VIEW: { + CMatrix mv = bgfxView; + mv.Transposition(); + *((CMatrix*)mtx) = mv; + } + break; + + case D3DTS_PROJECTION: { + CMatrix mp = bgfxProjection; + mp.Transposition(); + *((CMatrix *)mtx) = mp; + } + break; + + case D3DTS_WORLD: { + CMatrix wMat = bgfxWorld; + wMat.Transposition(); + *((CMatrix *)mtx) = wMat; + } + + break; + } + + if (type == D3DTS_VIEW) + { + mtx->_41 -= bgfxvViewRelationPos.x; + mtx->_42 -= bgfxvViewRelationPos.y; + mtx->_43 -= bgfxvViewRelationPos.z; + } + else if (type == D3DTS_WORLD) + { + mtx->_41 -= bgfxvWordRelationPos.x; + mtx->_42 -= bgfxvWordRelationPos.y; + mtx->_43 -= bgfxvWordRelationPos.z; + } +} + bool DX9RENDER::CreateState(ENTITY_STATE_GEN *state_gen) { // state_gen->SetState("vm",sizeof(screen_size),screen_size,sizeof(bool),&window); @@ -2387,7 +3372,8 @@ void DX9RENDER::RestoreRender() ClearPostProcessSurface(pSmallPostProcessSurface2); } ClearPostProcessSurface(pOriginalScreenSurface); - d3d9->CreateVertexBuffer(rectsVBuffer_SizeInRects * 6 * sizeof(RECT_VERTEX), D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, + d3d9->CreateVertexBuffer(rectsVBuffer_SizeInRects * 6 * sizeof(PosColorTexVertex), + D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, RS_RECT_VERTEX_FORMAT, D3DPOOL_DEFAULT, &rectsVBuffer, nullptr); for (long b = 0; b < MAX_BUFFERS; b++) { @@ -2485,6 +3471,7 @@ void DX9RENDER::SetGLOWParams(float _fBlurBrushSize, long _GlowIntensity, long _ iBlurPasses = _GlowPasses; } + void DX9RENDER::RunStart() { bDeviceLost = true; @@ -2551,6 +3538,8 @@ void DX9RENDER::RunStart() SetScreenAsRenderTarget(); } + //bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH | ((stencil_format == D3DFMT_D24S8) ? BGFX_CLEAR_STENCIL : 0, 0x303030ff, 1.0f, 0)); + DX9Clear(D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | ((stencil_format == D3DFMT_D24S8) ? D3DCLEAR_STENCIL : 0)); dwNumDrawPrimitive = 0; @@ -3046,6 +4035,93 @@ void DX9RENDER::SaveShoot() bMakeShoot = true; } +void DX9RENDER::BGFXRenderToBackBuffer() +{ + + uint32_t *Surface; + RECT r; + D3DLOCKED_RECT lr; + IDirect3DSurface9 *renderTarget, *surface; + + bMakeShoot = false; + + if (!(screen_bpp == D3DFMT_X8R8G8B8 || screen_bpp == D3DFMT_A8R8G8B8)) + { + core.Trace("Can't make screenshots in non 32bit video modes"); + return; + } + + r.left = 0; + r.right = screen_size.x; + r.top = 0; + r.bottom = screen_size.y; + renderTarget = nullptr; + surface = nullptr; + + // get the picture + if (FAILED(GetRenderTarget(&renderTarget))) + { + core.Trace("Falure get render target for make screenshot"); + return; + } + if (FAILED(CreateOffscreenPlainSurface(screen_size.x, screen_size.y, D3DFMT_A8R8G8B8, &surface))) + { + renderTarget->Release(); + core.Trace("Falure create buffer for make screenshot"); + return; + } + + // if (FAILED(d3d9->UpdateSurface(renderTarget, null, surface, null)) || + if (FAILED(D3DXLoadSurfaceFromSurface(surface, NULL, NULL, renderTarget, NULL, NULL, D3DX_DEFAULT, 0)) || + FAILED(surface->LockRect(&lr, &r, 0))) + { + surface->Release(); + renderTarget->Release(); + core.Trace("Falure make screenshot"); + return; + } + renderTarget->Release(); + renderTarget = nullptr; + + + + LONG windowWidth = screen_size.x; + LONG windowHeight = screen_size.y; + + std::vector v; + + v.resize(screen_size.x * screen_size.y * 4); + + memcpy(v.data(), reinterpret_cast(lr.pBits), v.size()); + + /*unsigned char *bits = reinterpret_cast(lr.pBits); + + for (int i = 0; i < v.size(); ++i) + { + v[i] = bits[i]; + }*/ + + size_t imgdata_size = screen_size.x * screen_size.y * 4; + + /* for (int i = 0; i < v.size(); i += 4) + { + v[i + 0] = 255; + v[i + 1] = 0; + v[i + 2] = 0; + v[i + 3] = 255; + }*/ + + auto backgroundTexture = m_spriteRenderer->GetBackBufferTexture(); + + m_spriteRenderer->BlitBackBuffer(v.data()); + + DrawSprite(backgroundTexture, 0, glm::vec2(0, 0), -2.0f); + + surface->UnlockRect(); + surface->Release(); + +} + void DX9RENDER::MakeScreenShot() { // GUARD(DX9RENDER::MakeScreenShot) @@ -3243,6 +4319,12 @@ void DX9RENDER::DrawRects(RS_RECT *pRSR, uint32_t dwRectsNum, const char *cBlock dv = 1.0f; } + float view[16]; + float proj[16]; + + //bgfx::setViewTransform(0, view, proj); + + d3d9->SetTransform(D3DTS_VIEW, IMatrix); d3d9->SetTransform(D3DTS_WORLD, IMatrix); @@ -3253,8 +4335,8 @@ void DX9RENDER::DrawRects(RS_RECT *pRSR, uint32_t dwRectsNum, const char *cBlock if (drawCount > rectsVBuffer_SizeInRects) drawCount = rectsVBuffer_SizeInRects; // Buffer - RECT_VERTEX *data = nullptr; - if (rectsVBuffer->Lock(0, drawCount * 6 * sizeof(RECT_VERTEX), (VOID **)&data, D3DLOCK_DISCARD) != D3D_OK) + PosColorTexVertex *data = nullptr; + if (rectsVBuffer->Lock(0, drawCount * 6 * sizeof(PosColorTexVertex), (VOID **)&data, D3DLOCK_DISCARD) != D3D_OK) return; if (!data) return; @@ -3262,7 +4344,7 @@ void DX9RENDER::DrawRects(RS_RECT *pRSR, uint32_t dwRectsNum, const char *cBlock for (uint32_t i = 0; i < drawCount && cnt < dwRectsNum; i++) { // Local array of a particle - RECT_VERTEX *buffer = &data[i * 6]; + PosColorTexVertex *buffer = &data[i * 6]; RS_RECT &rect = pRSR[cnt++]; CVECTOR pos = camMtx * (rect.vPos + vWordRelationPos); const float sizex = rect.fSize * fScaleX; @@ -3317,7 +4399,7 @@ void DX9RENDER::DrawRects(RS_RECT *pRSR, uint32_t dwRectsNum, const char *cBlock if (bDraw) do { - CHECKD3DERR(SetStreamSource(0, rectsVBuffer, sizeof(RECT_VERTEX))); + CHECKD3DERR(SetStreamSource(0, rectsVBuffer, sizeof(PosColorTexVertex))); CHECKD3DERR(DrawPrimitive(D3DPT_TRIANGLELIST, 0, drawCount * 2)); } while (cBlockName && TechniqueExecuteNext()); } @@ -3325,6 +4407,112 @@ void DX9RENDER::DrawRects(RS_RECT *pRSR, uint32_t dwRectsNum, const char *cBlock d3d9->SetTransform(D3DTS_VIEW, camMtx); } +std::shared_ptr DX9RENDER::GetSpriteRenderer() +{ + return m_spriteRenderer; +} + +std::shared_ptr DX9RENDER::GetPrimitiveRenderer() +{ + return m_primitiveRenderer; +} + +void DX9RENDER::DrawSprite(std::shared_ptr texture, uint32_t color, const glm::vec2 &position, float depth) +{ + DrawSprite(texture, glm::vec4(0, 0, texture->size.width, texture->size.height), color, position, glm::vec2(0, 0), glm::vec2(1, 1), 0.f, depth, false, false); +} + +void DX9RENDER::DrawSprite(std::shared_ptr texture, const glm::vec4 &src, uint32_t color, + const glm::vec2 &position, const glm::vec2 &origin, const glm::vec2 &scale, float angle, float depth, bool flip_x, bool flip_y) +{ + float minu, maxu, minv, maxv; + float width, height; + + width = src.z; + height = src.w; + // x + minu = src.x / (float)texture->size.width; // left + maxu = (src.x + width) / (float)texture->size.width; // right + + // y + // TODO OPENGL HOWTO + minv = src.y / (float)texture->size.height; // top + maxv = (src.y + height) / (float)texture->size.height; // bottom + + + if (flip_y) + { + // bottom right = max + // top left = min + float temp = maxv; + // bottom right y = top left y + maxv = minv; + minv = temp; + } + + if (flip_x) + { + float temp = maxu; + maxu = minu; + minu = temp; + } + + glm::vec2 pos(position + origin); + glm::mat3 transform = glm::translate(glm::mat3(1.f), pos) * glm::rotate(glm::mat3(1.f), angle) * + glm::scale(glm::mat3(1.f), scale) * glm::translate(glm::mat3(1.f), -pos); + + std::vector points { + transform * glm::vec3(position, 1.f), + transform * glm::vec3(position.x + width, position.y, 1.f), + transform * glm::vec3(position.x, position.y + height, 1.f), + transform * glm::vec3(position.x + width, position.y + height, 1.f), + }; + + + /*sprite->abgr = color; + sprite->tl = glm::vec2(points[0].x, points[0].y); + sprite->tr = glm::vec2(points[1].x, points[1].y); + sprite->bl = glm::vec2(points[2].x, points[2].y); + sprite->br = glm::vec2(points[3].x, points[3].y); + sprite->u = glm::vec2(minu, maxu); + sprite->v = glm::vec2(minv, maxv); + sprite->depth = depth;*/ + + auto u = glm::vec2(minu, maxu); + auto v = glm::vec2(minv, maxv); + + auto colors = color; + + std::vector vertices; + + vertices.resize(4); + + vertices[0] = SPRITE_VERTEX{points[0].x, points[0].y, u.x, v.x /*, color*/}; // top left + vertices[1] = SPRITE_VERTEX{points[1].x, points[1].y, u.y, v.x /*, color*/}; // top right + vertices[2] = SPRITE_VERTEX{points[2].x, points[2].y, u.x, v.y /*, color*/}; // bottom left + vertices[3] = SPRITE_VERTEX{points[3].x, points[3].y, u.y, v.y /*, color*/}; // bottom right + + m_spriteRenderer->Texture = texture; + + m_spriteRenderer->SetViewProjection(); + m_spriteRenderer->PushVertices(vertices, depth); + +} + +void DX9RENDER::DrawSprites(std::shared_ptr texture, + std::vector &vertices, + glm::vec2 &u, + glm::vec2 &v, + uint32_t &color) +{ + /*m_spriteRenderer->Texture = texture; + m_spriteRenderer->SetViewProjection(); + m_spriteRenderer->UpdateVertexBuffer(vertices, u, v, color); + m_spriteRenderer->Submit();*/ +} + + + void DX9RENDER::DrawSprites(RS_SPRITE *pRSS, uint32_t dwSpritesNum, const char *cBlockName) { uint32_t i; @@ -3480,7 +4668,13 @@ HRESULT DX9RENDER::BeginScene() if (!isInScene) { isInScene = true; - return CHECKD3DERR(d3d9->BeginScene()); + + HRESULT res = CHECKD3DERR(d3d9->BeginScene()); + + //bgfx::touch(0); + //bgfx::touch(1); + + return res; } return D3D_OK; @@ -3491,7 +4685,9 @@ HRESULT DX9RENDER::EndScene() if (isInScene) { isInScene = false; - return CHECKD3DERR(d3d9->EndScene()); + + HRESULT res = CHECKD3DERR(d3d9->EndScene()); + return res; } return D3D_OK; @@ -4103,7 +5299,16 @@ void DX9RENDER::ProgressView() DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1, 2, v, sizeof(v[0]), "ProgressTech"); EndScene(); + + // if (bMakeShoot || GetAsyncKeyState(VK_F9) < 0) + BGFXRenderToBackBuffer(); + + GetSpriteRenderer()->Submit(); + bgfx::frame(); + + d3d9->Present(nullptr, nullptr, nullptr, nullptr); + BeginScene(); // Next frame loadFrame++; @@ -4332,3 +5537,17 @@ IDirect3DBaseTexture9 *DX9RENDER::GetTextureFromID(long nTextureID) return nullptr; return Textures[nTextureID].d3dtex; } + +std::shared_ptr DX9RENDER::GetBGFXTextureFromID(long nTextureID) +{ + if (nTextureID < 0) + return nullptr; + + auto texture = std::make_shared(TextureResource( + BGFXTextures[nTextureID].textureHandle, BGFXTextures[nTextureID].name, BGFXTextures[nTextureID].hash, + BGFXTextures[nTextureID].ref, BGFXTextures[nTextureID].dwSize, BGFXTextures[nTextureID].isCubeMap, + BGFXTextures[nTextureID].loaded, BGFXTextures[nTextureID].size + )); + + return texture; +} diff --git a/src/libs/renderer/sdevice.h b/src/libs/renderer/sdevice.h index f1029076a..901ea40e5 100644 --- a/src/libs/renderer/sdevice.h +++ b/src/libs/renderer/sdevice.h @@ -1,5 +1,8 @@ #pragma once +#include + + #include "Effects.h" #include "Font.h" #include "VideoTexture.h" @@ -10,8 +13,12 @@ #include "d3d9types.h" #include "script_libriary.h" +#include "sprite_renderer.h" +#include "primitive_renderer.h" + #include #include +#include #define MAX_STEXTURES 1024 #define MAX_BUFFERS 1024 @@ -94,11 +101,22 @@ class DX9RENDER : public VDX9RENDER { #define RS_RECT_VERTEX_FORMAT (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1) - struct RECT_VERTEX + struct PosColorTexVertex { CVECTOR pos; uint32_t color; float u, v; + + static void init() + { + ms_layout.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::VertexLayout ms_layout; }; struct RenderTarget @@ -108,6 +126,9 @@ class DX9RENDER : public VDX9RENDER D3DVIEWPORT9 ViewPort; }; + std::shared_ptr m_spriteRenderer; + std::shared_ptr m_primitiveRenderer; + IDirect3DDevice9 *d3d9; IDirect3D9 *d3d; HWND hwnd; @@ -138,6 +159,8 @@ class DX9RENDER : public VDX9RENDER INDEX_BUFFER IndexBuffers[MAX_BUFFERS]; VERTEX_BUFFER VertexBuffers[MAX_BUFFERS]; + std::vector BGFXTextures; + bool MakeAvi; IDirect3DSurface9 *ImageBuffer; @@ -249,6 +272,7 @@ class DX9RENDER : public VDX9RENDER std::stack stRenderTarget; + bool BGFXTextureLoad(long texid); bool TextureLoad(long texid); bool ErrorHandler(HRESULT hr, const char *file, unsigned line, const char *func, const char *expr); @@ -295,6 +319,8 @@ class DX9RENDER : public VDX9RENDER // DX9Render: Screenshot Section void SaveShoot() override; + void BGFXRenderToBackBuffer(); + // DX9Render: Clip Planes Section HRESULT SetClipPlane(uint32_t Index, CONST float *pPlane) override; PLANE *GetPlanes() override; @@ -303,6 +329,11 @@ class DX9RENDER : public VDX9RENDER void SetTransform(long type, D3DMATRIX *mtx) override; void GetTransform(long type, D3DMATRIX *mtx) override; + void BGFXSetTransform(long type, D3DMATRIX *mtx) override; + void BGFXSetTransformUpdateViews(long type, D3DMATRIX *mtx) override; + void BGFXGetTransform(long type, D3DMATRIX *mtx) override; + + bool SetCamera(const CVECTOR &pos, const CVECTOR &ang, float perspective) override; bool SetCamera(const CVECTOR &pos, const CVECTOR &ang) override; bool SetCamera(CVECTOR lookFrom, CVECTOR lookTo, CVECTOR up) override; @@ -313,9 +344,13 @@ class DX9RENDER : public VDX9RENDER // DX9Render: Textures Section long TextureCreate(const char *fname) override; + long BGFXTextureCreate(const char *fname) override; + bool TextureSet(long stage, long texid) override; bool TextureRelease(long texid) override; + bool BGFXTextureRelease(long texid) override; + // DX9Render: Fonts Section long Print(long x, long y, const char *format, ...) override; long Print(long nFontNum, uint32_t color, long x, long y, const char *format, ...) override; @@ -338,6 +373,23 @@ class DX9RENDER : public VDX9RENDER bool TechniqueExecuteStart(const char *cBlockName) override; bool TechniqueExecuteNext() override; + + std::shared_ptr GetSpriteRenderer() override; + std::shared_ptr GetPrimitiveRenderer() override; + + void DrawSprite(std::shared_ptr texture, uint32_t color, + const glm::vec2 &position, float depth) override; + + void DrawSprite(std::shared_ptr texture, const glm::vec4 &src, uint32_t color, + const glm::vec2 &position, const glm::vec2 &origin, const glm::vec2 &scale, float angle, + float depth, bool flip_x, bool flip_y) override; + + void DrawSprites(std::shared_ptr texture, + std::vector &vertices, + glm::vec2 &u, glm::vec2 &v, + uint32_t &color) override; + + // DX9Render: Draw Section void DrawRects(RS_RECT *pRSR, uint32_t dwRectsNum, const char *cBlockName = nullptr, uint32_t dwSubTexturesX = 1, uint32_t dwSubTexturesY = 1, float fScaleX = 1.0f, float fScaleY = 1.0f) override; @@ -475,8 +527,14 @@ class DX9RENDER : public VDX9RENDER HRESULT ImageBlt(long nTextureId, RECT *pDstRect, RECT *pSrcRect) override; void MakeScreenShot(); + bool BGFXLoadTextureSurface(std::filesystem::path file, IDirect3DSurface9 *suface, uint32_t mipSize, uint32_t width, + uint32_t height, bool isSwizzled, uint32_t seek_to = 0); bool LoadTextureSurface(std::fstream &fileS, IDirect3DSurface9 *suface, uint32_t mipSize, uint32_t width, uint32_t height, bool isSwizzled); + + uint32_t BGFXLoadCubmapSide(std::wstring file, IDirect3DCubeTexture9 *tex, D3DCUBEMAP_FACES face, uint32_t numMips, + uint32_t mipSize, uint32_t size, bool isSwizzled, uint32_t seek_to = 0); + uint32_t LoadCubmapSide(std::fstream &fileS, IDirect3DCubeTexture9 *tex, D3DCUBEMAP_FACES face, uint32_t numMips, uint32_t mipSize, uint32_t size, bool isSwizzled); @@ -690,8 +748,12 @@ bool SetCurFont (long fontID); // returns true if the given font is installed CMatrix mView, mWorld, mProjection; + CMatrix bgfxView, bgfxWorld, bgfxProjection; + CVECTOR vWordRelationPos; CVECTOR vViewRelationPos; + CVECTOR bgfxvWordRelationPos; + CVECTOR bgfxvViewRelationPos; bool bUseLargeBackBuffer; @@ -737,7 +799,7 @@ bool SetCurFont (long fontID); // returns true if the given font is installed void SetGLOWParams(float _fBlurBrushSize, long _GlowIntensity, long _GlowPasses) override; IDirect3DBaseTexture9 *GetTextureFromID(long nTextureID) override; - + std::shared_ptr GetBGFXTextureFromID(long nTextureID) override; void LostRender(); void RestoreRender(); diff --git a/src/libs/renderer/sprite_renderer.cpp b/src/libs/renderer/sprite_renderer.cpp new file mode 100644 index 000000000..2fe5d8bd7 --- /dev/null +++ b/src/libs/renderer/sprite_renderer.cpp @@ -0,0 +1,260 @@ +#include "sprite_renderer.h" +#include "brtshaderc.h" + +#include "matrix.hpp" +#include "gtc/matrix_transform.hpp" +#include "gtc/type_ptr.hpp" + +#include +#include + +bgfx::VertexLayout SPRITE_VERTEX::sr_layout; + + +SpriteRenderer::SpriteRenderer(long m_fbWidth, long m_fbHeight) + : m_spriteQueueSize(0), m_spriteQueueCount(0), m_backbufferTexture(nullptr) +//SpriteRenderer::SpriteRenderer() +{ + SPRITE_VERTEX::Init(); + + m_width = m_fbWidth; + m_height = m_fbHeight; + + const bgfx::Memory *memVsh = shaderc::compileShader(shaderc::ST_VERTEX, "shaders/dx9/vs_font.sc"); + const bgfx::Memory *memFsh = shaderc::compileShader(shaderc::ST_FRAGMENT, "shaders/dx9/fs_font.sc"); + + bgfx::ShaderHandle vsh = bgfx::createShader(memVsh); + bgfx::ShaderHandle fsh = bgfx::createShader(memFsh); + + m_prog = bgfx::createProgram(vsh, fsh, true); + + s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler); + // u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4); + + m_backbufferMemory.resize(m_width * m_height * 4); + + auto rttTex = loadMemoryTexture("Sprite backbuffer", bgfx::TextureFormat::BGRA8, m_width, m_height, 1, false, false, + 0, nullptr); + + SetBackbufferTexture(rttTex); +} + +SpriteRenderer::~SpriteRenderer() +{ + //bgfx::destroy(m_fvbh); + //bgfx::destroy(m_fibh); + ReleaseTexture(this->GetBackBufferTexture()); + bgfx::destroy(m_prog); +} + +std::shared_ptr SpriteRenderer::GetBackBufferTexture() +{ + return m_backbufferTexture; +} + +void SpriteRenderer::SetBackbufferTexture(std::shared_ptr texture) +{ + m_backbufferTexture = texture; +} + +void SpriteRenderer::BlitBackBuffer(unsigned char *data) +{ + memcpy((void *)m_backbufferMemory.data(), data, m_width * m_height * 4); + bgfx::updateTexture2D(*m_backbufferTexture->textureHandle, 0, 0, 0, 0, m_width, m_height, bgfx::makeRef(m_backbufferMemory.data(), m_backbufferMemory.size())); + +} + +void SpriteRenderer::ReleaseTexture(std::shared_ptr texture) +{ + bgfx::destroy(*texture->textureHandle); +} + +/* +#include "Matrix.h" + +class MatrixState { +public: + + MatrixState(std::vector world, + std::vector view, + std::vector projection) + { + World = world; + View = view; + Projection = projection; + } + + std::vector GetWorld() + { + return World; + } + + std::vector GetView() + { + return View; + } + + std::vector GetProjection() + { + return Projection; + } + +private: + std::vector World; + std::vector View; + std::vector Projection; +}; + +static std::vector matrixStates;*/ + +void SpriteRenderer::SetViewProjection() +{ + const bx::Vec3 at = {0.0f, 0.0f, 0.0}; + const bx::Vec3 eye = {0.0f, 0.0f, -3.0}; + + glm::mat4 model(1); + + bx::mtxIdentity(glm::value_ptr(model)); + + bgfx::setTransform(glm::value_ptr(model)); + + glm::mat4 view(1); + bx::mtxLookAt(glm::value_ptr(view), eye, at); + glm::mat4 proj(1); + bx::mtxOrtho(glm::value_ptr(proj), 0.0f, float(m_width), float(m_height), 0.0f, 0.0f, 10.0f, 0.0f, + bgfx::getCaps()->homogeneousDepth); + bgfx::setViewTransform(0, glm::value_ptr(view), glm::value_ptr(proj)); +} + +void SpriteRenderer::UpdateIndexBuffer(std::vector indices){ + /*const bgfx::Memory *mem = bgfx::alloc(indices.size() * sizeof(uint16_t)); + + memcpy(mem->data, indices.data(), indices.size() * sizeof(uint16_t)); + + bgfx::update(m_fibh, 0, mem);*/ + + throw std::exception("Does not apply for static index buffers"); +} + +void SpriteRenderer::PushVertices(std::vector vertices, float depth) +{ + if (m_spriteQueueCount >= m_spriteQueue.size()) + { + uint32_t newSize = std::max(InitialQueueSize, (uint32_t)m_spriteQueue.size() * 2); + m_spriteQueue.resize(newSize); + + m_sortedSprites.clear(); + } + + SpriteInfo *sprite = &m_spriteQueue[m_spriteQueueCount]; + + sprite->texture = Texture; + sprite->vertices = vertices; + sprite->depth = depth; + ++m_spriteQueueCount; +} + +void SpriteRenderer::Submit() +{ + + if (m_spriteQueueCount == 0) + { + return; + } + + // Set vertex and index buffer. + //bgfx::setVertexBuffer(0, m_fvbh); + //bgfx::setIndexBuffer(m_fibh); + if (m_sortedSprites.size() < m_spriteQueueCount) + { + uint32_t previousSize = (uint32_t)m_sortedSprites.size(); + m_sortedSprites.resize(m_spriteQueueCount); + for (size_t i = previousSize; i < m_spriteQueueCount; ++i) + { + m_sortedSprites[i] = &m_spriteQueue[i]; + } + + std::sort(std::begin(m_sortedSprites), std::begin(m_sortedSprites) + m_spriteQueueCount, + [](const SpriteInfo *x, const SpriteInfo *y) { return x->depth < y->depth; }); + + } + + + + bgfx::TransientVertexBuffer vb; + bgfx::allocTransientVertexBuffer(&vb, 4 * m_spriteQueueCount, SPRITE_VERTEX::sr_layout); + SPRITE_VERTEX *vertex = (SPRITE_VERTEX *)vb.data; + + for (uint32_t i = 0, v = 0; i < m_spriteQueueCount; ++i, v += 4) + { + const SpriteInfo *s = m_sortedSprites[i]; + vertex[v + 0] = s->vertices[0]; + vertex[v + 1] = s->vertices[1]; + vertex[v + 2] = s->vertices[2]; + vertex[v + 3] = s->vertices[3]; + } + + const uint32_t indicesCount = m_spriteQueueCount * 6; + + std::vector indices; + indices.reserve(indicesCount); + + for (uint16_t i = 0; i < indicesCount; i += 4) + { + indices.push_back(i); + indices.push_back(i + 1); + indices.push_back(i + 2); + + indices.push_back(i + 1); + indices.push_back(i + 3); + indices.push_back(i + 2); + } + + bgfx::TransientIndexBuffer ib; + + bgfx::allocTransientIndexBuffer(&ib, indicesCount, false); + + auto ibPtr = reinterpret_cast(ib.data); + + for (long i = 0; i < indices.size(); ++i) + { + ibPtr[i] = indices[i]; + } + + std::shared_ptr batchTexture(nullptr); + uint16_t batchStart = 0; + + uint64_t state = BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_BLEND_ALPHA; + + for (uint16_t pos = 1; pos < m_spriteQueueCount + 1; ++pos) + { + auto texture = m_sortedSprites[pos - 1]->texture; + //int32_t depth = static_cast(m_sortedSprites[pos]->depth); + if (texture != batchTexture) + { + if (pos > batchStart) + { + bgfx::setViewFrameBuffer(0, BGFX_INVALID_HANDLE); + bgfx::setState(state); + bgfx::setTexture(0, s_texColor, *texture->textureHandle); + bgfx::setVertexBuffer(0, &vb); + bgfx::setIndexBuffer(&ib, batchStart * 6, (pos - batchStart) * 6); + //bgfx::setIndexBuffer(m_ibh, batchStart * 6, (pos - batchStart) * 6); + bgfx::submit(0, m_prog); + //bgfx::submit(1, m_prog); + } + batchTexture = texture; + batchStart = pos; + } + } + + + m_spriteQueueCount = 0; + + m_sortedSprites.clear(); + + /*if (m_backbufferTexture != nullptr) + { + ReleaseTexture(m_backbufferTexture); + }*/ +} \ No newline at end of file