0

I was trying to make custom shaders for bgfx that are already compiled. This bug allows the shaders to be read in, but then the shaders don't function properly and not draw anything to the screen.

Read and load Shader

std::string ReadFile(const char* filepath) {
    std::ifstream input(filepath);

    std::stringstream buf;
    buf << input.rdbuf();

    return buf.str();
}

bgfx::ShaderHandle loadShader(const char* _name) {
    auto data = ReadFile(_name);
    bgfx::ShaderHandle handle = bgfx::createShader(bgfx::makeRef(static_cast<void*>(&data), data.size()));
    std::cout << data << "\n";
    return handle;
}

Vertex Shader

$input a_position, a_color0
$output v_color0

#include "bgfx_shader.sh"
#include "shaderlib.sh"

void main()
{
    gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) );
    v_color0 = a_color0;
}

Fragment Shader

$input v_color0

#include "bgfx_shader.sh"
#include "shaderlib.sh"

void main()
{
    gl_FragColor = v_color0;
}

Window and shader output

Here is the whole program if you wish to try to make sense of it. [just in case]

#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include <bx/math.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

#define GLFW_EXPOSE_NATIVE_WIN32
#define GLFW_EXPOSE_NATIVE_WGL
#include <GLFW/glfw3native.h>

#include <iostream>
#include <fstream>
#include <sstream>

const int width = 1920;
const int height = 1080;

struct PosColorVertex {
    float m_x;
    float m_y;
    float m_z;
    uint32_t m_abgr;

    static void init() {
        ms_decl
            .begin()
            .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
            .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
            .end();
    };

    static bgfx::VertexLayout ms_decl;
};


bgfx::VertexLayout PosColorVertex::ms_decl;

static PosColorVertex s_cubeVertices[] =
{
 {  0.5f,  0.5f, 0.0f, 0xff0000ff },
 {  0.5f, -0.5f, 0.0f, 0xff0000ff },
 { -0.5f, -0.5f, 0.0f, 0xff00ff00 },
 { -0.5f,  0.5f, 0.0f, 0xff00ff00 }
};


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

bgfx::VertexBufferHandle m_vbh;
bgfx::IndexBufferHandle m_ibh;
bgfx::ProgramHandle m_program;

std::string ReadFile(const char* filepath) {
    std::ifstream input(filepath);

    std::stringstream buf;
    buf << input.rdbuf();

    return buf.str();
}

bgfx::ShaderHandle loadShader(const char* _name) {
    auto data = ReadFile(_name);
    bgfx::ShaderHandle handle = bgfx::createShader(bgfx::makeRef(static_cast<void*>(&data), data.size()));
    std::cout << data << "\n";
    return handle;
}

int main()
{
    glfwInit();
    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    GLFWwindow* window = glfwCreateWindow(width, height, "Hello, bgfx!", nullptr, nullptr);

    //glfwWaitEventsTimeout(0.016);

    bgfx::Init bgfxInit;
    bgfxInit.platformData.nwh = glfwGetWin32Window(window);
    bgfxInit.type = bgfx::RendererType::Count;
    bgfxInit.resolution.width = width;
    bgfxInit.resolution.height = height;
    bgfxInit.resolution.reset = BGFX_RESET_VSYNC;
    bgfx::init(bgfxInit);

    printf("bgfx renderer is %s\n", bgfx::getRendererName(bgfx::getRendererType()));

    bgfx::setDebug(BGFX_DEBUG_TEXT /*| BGFX_DEBUG_STATS*/);

    // Update the background color
    bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF, 1.0f, 0);
    bgfx::setViewRect(0, 0, 0, width, height);

    PosColorVertex::init();
    m_vbh = bgfx::createVertexBuffer(
        // Static data can be passed with bgfx::makeRef
        bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices)),
        PosColorVertex::ms_decl
    );

    m_ibh = bgfx::createIndexBuffer(
        // Static data can be passed with bgfx::makeRef
        bgfx::makeRef(s_cubeTriList, sizeof(s_cubeTriList))
    );

    bgfx::ShaderHandle vsh = loadShader("v_simple.bin");
    bgfx::ShaderHandle fsh = loadShader("f_simple.bin");

    m_program = bgfx::createProgram(vsh, fsh, true);

    unsigned int counter = 0;
    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();

        const bx::Vec3 at = { 0.0f, 0.0f,   0.0f };
        const bx::Vec3 eye = { 0.0f, 0.0f, 10.0f };

        // Set view and projection matrix for view 0.
        float view[16];
        bx::mtxLookAt(view, eye, at);

        float proj[16];
        bx::mtxProj(proj,
            60.0f,
            float(width) / float(height),
            0.1f, 100.0f,
            bgfx::getCaps()->homogeneousDepth);

        bgfx::setViewTransform(0, view, proj);

        // Set view 0 default viewport.
        bgfx::setViewRect(0, 0, 0,
            width,
            height);

        bgfx::touch(0);


        float mtx[16];
        bx::mtxRotateY(mtx, 0.0f);

        // position x,y,z
        mtx[12] = 0.0f;
        mtx[13] = 0.0f;
        mtx[14] = 0.0f;

        // Set model matrix for rendering.
        bgfx::setTransform(mtx);

        // Set vertex and index buffer.
        bgfx::setVertexBuffer(0, m_vbh);
        bgfx::setIndexBuffer(m_ibh);

        // Set render states.
        bgfx::setState(BGFX_STATE_DEFAULT);

        // Submit primitive for rendering to view 0.
        bgfx::submit(0, m_program);

        bgfx::frame();
    }

    bgfx::shutdown();
    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}

I have tried reading it in as a const char* and using data.data(), both of these cuase the app to crash. I was also not able to find anyone else having this problem.

Cake
  • 37
  • 6

0 Answers0