Skip to content

Commit

Permalink
add gleq as a thrid party event handle system
Browse files Browse the repository at this point in the history
  • Loading branch information
jintaoyugithub committed Nov 13, 2024
1 parent e492af8 commit 904f11b
Show file tree
Hide file tree
Showing 22 changed files with 606 additions and 165 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "ext/spdlog"]
path = ext/spdlog
url = https://github.com/gabime/spdlog.git
[submodule "ext/imgui"]
path = ext/imgui
url = https://github.com/ocornut/imgui.git
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ add_subdirectory(helloBgfx)
add_subdirectory(cube)
add_subdirectory(quad)
add_subdirectory(computeShader)
add_subdirectory(quadRendering)
add_subdirectory(imgui)
12 changes: 6 additions & 6 deletions examples/computeShader/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# compile all vertex shaders
bgfx_compile_shaders(
TYPE VERTEX
SHADERS "${CMAKE_SOURCE_DIR}/shaders/common/quad_vs.sc"
VARYING_DEF "${CMAKE_SOURCE_DIR}/shaders/common/quad_varying.def.sc"
SHADERS "${CMAKE_SOURCE_DIR}/shaders/examples/computeShader/quad_vs.sc"
VARYING_DEF "${CMAKE_SOURCE_DIR}/shaders/examples/computeShader/quad_varying.def.sc"
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/shaders/"
)

# compile all fragment shaders
bgfx_compile_shaders(
TYPE FRAGMENT
SHADERS "${CMAKE_SOURCE_DIR}/shaders/common/quad_fs.sc"
VARYING_DEF "${CMAKE_SOURCE_DIR}/shaders/common/quad_varying.def.sc"
SHADERS "${CMAKE_SOURCE_DIR}/shaders/examples/computeShader/quad_fs.sc"
VARYING_DEF "${CMAKE_SOURCE_DIR}/shaders/examples/computeShader/quad_varying.def.sc"
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/shaders/"
)

Expand All @@ -31,8 +31,8 @@ bgfx_compile_shaders(
add_executable(computeShader
computeShader.cpp
# include all the shader files
"${CMAKE_SOURCE_DIR}/shaders/common/quad_vs.sc"
"${CMAKE_SOURCE_DIR}/shaders/common/quad_fs.sc"
"${CMAKE_SOURCE_DIR}/shaders/examples/computeShader/quad_vs.sc"
"${CMAKE_SOURCE_DIR}/shaders/examples/computeShader/quad_fs.sc"
"${CMAKE_SOURCE_DIR}/shaders/examples/computeShader/image_cs.sc"
"${CMAKE_SOURCE_DIR}/shaders/examples/computeShader/buffer_cs.sc"
)
Expand Down
114 changes: 37 additions & 77 deletions examples/computeShader/computeShader.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
#include <utils/common.hpp>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include <iostream>
#include <appBaseGLFW.hpp>

glm::vec4 test = glm::vec4(1.0f);
#include <iostream>

#define GLEQ_IMPLEMENTATION
#define GLEQ_STATIC
#include <gleq/gleq.hpp>

/// data used for quad
struct quadPosTexCoord {
float _x;
float _y;
float _z;

float _u;
float _v;
int16_t _u;
int16_t _v;

static void init() {
_layout
.begin()
.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Int16, true, true)
.end();
};

Expand All @@ -31,44 +28,36 @@ struct quadPosTexCoord {

bgfx::VertexLayout quadPosTexCoord::_layout;

//quadPosTexCoord quadVertices[] = {
// {-1.0f, -1.0f, 0.0f, 0.0f},
// { 1.0f, -1.0f, 1.0f, 0.0f},
// {-1.0f, 1.0f, 0.0f, 1.0f},
// { 1.0f, 1.0f, 1.0f, 1.0f},
//};

quadPosTexCoord quadVertices[] = {
{-1.0f, 1.0f, 0.0f, 0.0f},
{ 1.0f, 1.0f, 1.0f, 0.0f},
{-1.0f, -1.0f, 0.0f, 1.0f},
{ 1.0f, -1.0f, 1.0f, 1.0f},
static quadPosTexCoord quadVertices[] = {
// left corner, right corner, top right, top left
{-1.0, -1.0, 0.0, 0, 0},
{ 1.0, -1.0, 0.0, 0x7fff, 0},
{ 1.0, 1.0, 0.0, 0x7fff, 0x7fff},
{-1.0, 1.0, 0.0, 0, 0x7fff},
};

//const uint16_t quadIndices[] = {
// 2, 1, 0,
// 2, 3, 1,
//};

const uint16_t quadIndices[] = {
2, 1, 0,
2, 3, 1,
static const uint16_t quadIndices[] = {
0, 1, 2,
2, 3, 0,
};

/// ERROR: uint16_t is necessary, otherwise it won't display anything
//static const unsigned int quadIndices[] = {
// 0, 1, 2,
// 2, 3, 0,
//};

const uint32_t kThreadGroupUpdateSize = 512;
const uint32_t kMaxParticleCount = 32 * 1024;

void mouseButtonCb(GLFWwindow* window, int button, int action, int modes) {
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
std::cout << "left button pressed" << std::endl;
}
}

class ExampleComputeShader : public shift::AppBaseGLFW {

void init(int _argc, const char** _argv, uint32_t width, uint32_t height) override {
shift::AppBaseGLFW::init(_argc, _argv, width, height);

// init the input event system
gleqTrackWindow(_window);

/* Check if compute shader is supported */
const bgfx::Caps* caps = bgfx::getCaps(); // this func return renderer capabilities
_computeSupported = !!(caps->supported & BGFX_CAPS_COMPUTE);
Expand All @@ -89,10 +78,7 @@ class ExampleComputeShader : public shift::AppBaseGLFW {
bgfx::makeRef(quadIndices, sizeof(quadIndices))
);

_test2 = bgfx::createUniform("test2", bgfx::UniformType::Vec4);

// create quad program
bgfx::setUniform(_test2, &test);
_quadProgram = shift::loadProgram({ "quad_vs.sc", "quad_fs.sc" });

/* Data required by Compute Shader*/
Expand All @@ -108,26 +94,14 @@ class ExampleComputeShader : public shift::AppBaseGLFW {
// note this is memory size
s_buffers = bgfx::createDynamicVertexBuffer(1<<15, dvbLayout, BGFX_BUFFER_COMPUTE_READ_WRITE);

_test = bgfx::createUniform("test", bgfx::UniformType::Vec4);

// textures/images

// create compute shaders
bgfx::setUniform(_test, &test);
_csProgramWithBuffer = shift::loadProgram({ "buffer_cs.sc" });

// why dispatch at init?
// because in the compute shader example, it try to init the data with compute shader
// check example Nbody
//bgfx::dispatch(0, _csProgramWithBuffer, SHIFT_DEFAULT_WIDTH * SHIFT_DEFAULT_WIDTH, 1, 1);

//_csProgramWithImage = shift::loadProgram({ "image_cs.sc" });
}

}

bool update() override {

if (!glfwWindowShouldClose(_window)) {
glfwSwapBuffers(_window);
glfwPollEvents();
Expand All @@ -136,37 +110,26 @@ class ExampleComputeShader : public shift::AppBaseGLFW {
/* Compute shader */
bgfx::setBuffer(0, s_buffers, bgfx::Access::Write);
// specified the work group count
bgfx::setUniform(_test, &test, 1);
bgfx::dispatch(0, _csProgramWithBuffer, kMaxParticleCount / kThreadGroupUpdateSize, 1, 1);


/* Quad rendering */
glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, glm::radians(70.0f), glm::vec3(1.0f, 0.0f, 0.0f));
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, -5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 proj = glm::perspective(glm::radians(60.0f), float(getWidth()) / getHeight(), 0.1f, 100.0f);
bgfx::setViewTransform(0, &view[0][0], &proj[0][0]);
bgfx::setViewRect(0, 0, 0, uint16_t(getWidth()), uint16_t(getHeight()));
bgfx::touch(0);

bgfx::setUniform(_test2, &test, 1);
bgfx::setTransform(&model[0][0]);
bgfx::setVertexBuffer(0, _vbhQuad);
bgfx::setIndexBuffer(_ibhQuad);
bgfx::setBuffer(0, s_buffers, bgfx::Access::Read);
// chech https://bkaradzic.github.io/bgfx/bgfx.html#_CPPv4N4bgfx7Encoder8setStateE8uint64_t8uint32_t
bgfx::setState(BGFX_STATE_DEFAULT);
//bgfx::setState(0
// //| BGFX_STATE_PT_LINESTRIP
// | BGFX_STATE_WRITE_RGB
// | BGFX_STATE_BLEND_ADD
// | BGFX_STATE_DEPTH_TEST_ALWAYS
//);
//bgfx::setState(BGFX_STATE_CULL_CW);
bgfx::submit(0, _quadProgram);

bgfx::frame();

// check the input events
while(gleqNextEvent(&_event)) {
switch(_event.type) {
case GLEQ_BUTTON_PRESSED:
std::cout << _event.pos.x << _event.pos.y << std::endl;
}
}

return true;
}
Expand All @@ -181,17 +144,14 @@ class ExampleComputeShader : public shift::AppBaseGLFW {
if (_computeSupported) {
bgfx::destroy(_csProgramWithBuffer);
//bgfx::destroy(_csProgramWithImage);
bgfx::destroy(_quadProgram);
bgfx::destroy(_vbhQuad);
bgfx::destroy(_ibhQuad);
bgfx::destroy(s_buffers);
bgfx::destroy(_test);
bgfx::destroy(_test2);
//bgfx::destroy(_imgCS);
}
bgfx::destroy(_quadProgram);
}


public:
ExampleComputeShader(const char* name, const char* description, const char* url)
: shift::AppBaseGLFW(name, description, url) {}
Expand All @@ -207,15 +167,15 @@ class ExampleComputeShader : public shift::AppBaseGLFW {
bool _computeSupported;
bool _indirectSupported;

GLEQevent _event;

bgfx::ProgramHandle _csProgramWithBuffer;
bgfx::ProgramHandle _csProgramWithImage;
bgfx::ProgramHandle _quadProgram;
bgfx::VertexBufferHandle _vbhQuad;
bgfx::IndexBufferHandle _ibhQuad;
bgfx::DynamicVertexBufferHandle s_buffers;
bgfx::TextureHandle _imgCS;
bgfx::UniformHandle _test;
bgfx::UniformHandle _test2;
};

int main(int _argc, const char** _argv) {
Expand Down
18 changes: 18 additions & 0 deletions examples/imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
add_executable(imguiExample
imgui.cpp
# include all the shader files
)

target_include_directories(
imguiExample
PUBLIC
"${CMAKE_SOURCE_DIR}/include/"
"${CMAKE_SOURCE_DIR}/ext/bgfx.cmake/bgfx/3rdparty/"
"${CMAKE_SOURCE_DIR}/ext/bgfx.cmake/bgfx/include/"
)

target_link_libraries(
imguiExample
PUBLIC
shift_lib
)
Empty file added examples/imgui/imgui.cpp
Empty file.
12 changes: 12 additions & 0 deletions ext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ set( BUILD_SHARED_LIBS OFF CACHE INTERNAL "" )
set( BUILD_STATIC_LIBS OFF CACHE INTERNAL "" )
add_subdirectory( glm )

# imgui
add_library(
imgui
STATIC
${CMAKE_CURRENT_LIST_DIR}/imgui/imgui.cpp
${CMAKE_CURRENT_LIST_DIR}/imgui/imgui_demo.cpp
${CMAKE_CURRENT_LIST_DIR}/imgui/imgui_draw.cpp
${CMAKE_CURRENT_LIST_DIR}/imgui/imgui_widgets.cpp
)
target_include_directories( imgui PUBLIC ${CMAKE_CURRENT_LIST_DIR}/imgui )
set_target_properties( imgui PROPERTIES FOLDER "imgui" )

# spdlog
add_subdirectory( spdlog )

Loading

0 comments on commit 904f11b

Please sign in to comment.