Skip to content

Commit

Permalink
add mock backend
Browse files Browse the repository at this point in the history
  • Loading branch information
nmwsharp committed Feb 11, 2020
1 parent de9ae62 commit 28e7370
Show file tree
Hide file tree
Showing 20 changed files with 1,580 additions and 94 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ cmake_policy(SET CMP0054 NEW) # don't implicitly dereference inside if()
set(POLYSCOPE_BACKEND "OPENGL3_GLFW" CACHE STRING "Which graphics and windowing backend to use")
set_property(CACHE POLYSCOPE_BACKEND PROPERTY STRINGS "OPENGL3_GLFW" "MOCK_OPENGL")
if("${POLYSCOPE_BACKEND}" STREQUAL "OPENGL3_GLFW")
message(INFO "Using polyscope backend: ${POLYSCOPE_BACKEND}")
message("Using polyscope backend: ${POLYSCOPE_BACKEND}")
elseif("${POLYSCOPE_BACKEND}" STREQUAL "MOCK_OPENGL")
message(INFO "Using polyscope backend: ${POLYSCOPE_BACKEND}")
message("Using polyscope backend: ${POLYSCOPE_BACKEND}")
else()
message(FATAL_ERROR "Unrecognized polyscope backend from POLYSCOPE_BACKEND option, value is [${POLYSCOPE_BACKEND}]" )
endif()
Expand Down
15 changes: 15 additions & 0 deletions deps/imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ if("${POLYSCOPE_BACKEND}" STREQUAL "OPENGL3_GLFW")

elseif("${POLYSCOPE_BACKEND}" STREQUAL "MOCK_OPENGL")

# Disable every platform-specific thing I can find in imgui
add_definitions(-DIMGUI_DISABLE_OSX_FUNCTIONS)
add_definitions(-DIMGUI_DISABLE_WIN32_FUNCTIONS)
add_definitions(-DIMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS)
add_definitions(-DIMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS)

set(SRCS imgui/imgui.cpp imgui/imgui_draw.cpp imgui/imgui_widgets.cpp)

add_library(
imgui
${SRCS}
)

target_include_directories(imgui PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/imgui/")

endif()


Expand Down
12 changes: 7 additions & 5 deletions include/polyscope/render/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,18 @@ class Engine {
// === Windowing and framework things
virtual void makeContextCurrent() = 0;
virtual void updateWindowSize() = 0;
virtual std::tuple<int, int> getWindowPos() = 0;
virtual bool windowRequestsClose() = 0;
virtual void pollEvents() = 0;
virtual bool isKeyPressed(char c) = 0; // for lowercase a-z and 0-9 only
virtual std::tuple<int, int> getWindowPos() = 0;
virtual std::string getClipboardText() = 0;
virtual void setClipboardText(std::string text) = 0;

// ImGui
virtual void initializeImGui() = 0;
virtual void shutdownImGui() = 0;
void setImGuiStyle();
ImFontAtlas* getImGuiGlobalFontAtlas();
ImFontAtlas* getImGuiGlobalFontAtlas();
virtual void ImGuiNewFrame() = 0;
virtual void ImGuiRender() = 0;

Expand Down Expand Up @@ -366,9 +368,9 @@ class Engine {

// The cache of materials
std::map<Material, BasisMaterial> materialCache;
// Internal windowing and engine details
ImFontAtlas* globalFontAtlas = nullptr;

// Internal windowing and engine details
ImFontAtlas* globalFontAtlas = nullptr;
};


Expand Down
261 changes: 261 additions & 0 deletions include/polyscope/render/mock_opengl/mock_gl_engine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
// Copyright 2017-2019, Nicholas Sharp and the Polyscope contributors. http://polyscope.run.
#pragma once

#include "polyscope/options.h"
#include "polyscope/render/engine.h"
#include "polyscope/utilities.h"

// A fake version of the opengl engine, with all of the actual gl calls stubbed out. Useful for testing.

// Macro to construct shader strings. Unforunately, it eats line numbers.
#define POLYSCOPE_GLSL(version, shader) "#version " #version "\n" #shader

namespace polyscope {
namespace render {

class GLTextureBuffer : public TextureBuffer {
public:
// create a 1D texture from data
GLTextureBuffer(TextureFormat format, unsigned int size1D, unsigned char* data = nullptr);
GLTextureBuffer(TextureFormat format, unsigned int size1D, float* data);

// create a 2D texture from data
GLTextureBuffer(TextureFormat format, unsigned int sizeX_, unsigned int sizeY_, unsigned char* data = nullptr);
GLTextureBuffer(TextureFormat format, unsigned int sizeX_, unsigned int sizeY_, float* data);

~GLTextureBuffer() override;


// Resize the underlying buffer (contents are lost)
void resize(unsigned int newLen) override;
void resize(unsigned int newX, unsigned int newY) override;

void setFilterMode(FilterMode newMode) override;
void* getNativeHandle() override;


void bind();

protected:
};

class GLRenderBuffer : public RenderBuffer {
public:
GLRenderBuffer(RenderBufferType type, unsigned int sizeX_, unsigned int sizeY_);
~GLRenderBuffer() override;

void resize(unsigned int newX, unsigned int newY) override;

void bind();

protected:
};


class GLFrameBuffer : public FrameBuffer {

public:
GLFrameBuffer();
~GLFrameBuffer() override;

// Bind to this framebuffer so subsequent draw calls will go to it
// If return is false, binding failed and the framebuffer should not be used.
bool bindForRendering() override;

// Clear to redraw
void clear() override;

// Bind to textures/renderbuffers for output
void addColorBuffer(std::shared_ptr<RenderBuffer> renderBuffer) override;
void addColorBuffer(std::shared_ptr<TextureBuffer> textureBuffer) override;
void addDepthBuffer(std::shared_ptr<RenderBuffer> renderBuffer) override;
void addDepthBuffer(std::shared_ptr<TextureBuffer> textureBuffer) override;

void setDrawBuffers() override;

// Query pixels
std::array<float, 4> readFloat4(int xPos, int yPos) override;

// Getters

protected:

void bind();
};


class GLShaderProgram : public ShaderProgram {

public:
GLShaderProgram(const std::vector<ShaderStageSpecification>& stages, DrawMode dm, unsigned int nPatchVertices = 0);
~GLShaderProgram() override;

// === Store data
// If update is set to "true", data is updated rather than allocated (must be allocated first)

// Uniforms
bool hasUniform(std::string name) override;
void setUniform(std::string name, int val) override;
void setUniform(std::string name, unsigned int val) override;
void setUniform(std::string name, float val) override;
void setUniform(std::string name, double val) override; // WARNING casts down to float
void setUniform(std::string name, float* val) override;
void setUniform(std::string name, glm::vec2 val) override;
void setUniform(std::string name, glm::vec3 val) override;
void setUniform(std::string name, glm::vec4 val) override;
void setUniform(std::string name, std::array<float, 3> val) override;
void setUniform(std::string name, float x, float y, float z, float w) override;

// = Attributes
// clang-format off
bool hasAttribute(std::string name) override;
void setAttribute(std::string name, const std::vector<glm::vec2>& data, bool update = false, int offset = 0, int size = -1) override;
void setAttribute(std::string name, const std::vector<glm::vec3>& data, bool update = false, int offset = 0, int size = -1) override;
void setAttribute(std::string name, const std::vector<glm::vec4>& data, bool update = false, int offset = 0, int size = -1) override;
void setAttribute(std::string name, const std::vector<double>& data, bool update = false, int offset = 0, int size = -1) override;
void setAttribute(std::string name, const std::vector<int>& data, bool update = false, int offset = 0, int size = -1) override;
void setAttribute(std::string name, const std::vector<uint32_t>& data, bool update = false, int offset = 0, int size = -1) override;
// clang-format on

// Convenience method to set an array-valued attrbute, such as 'in vec3 vertexVal[3]'. Applies interleaving then
// forwards to the usual setAttribute
template <typename T, unsigned int C>
void setAttribute(std::string name, const std::vector<std::array<T, C>>& data, bool update = false, int offset = 0,
int size = -1);


// Indices
void setIndex(std::vector<std::array<unsigned int, 3>>& indices) override;
void setIndex(std::vector<unsigned int>& indices) override;
void setPrimitiveRestartIndex(unsigned int restartIndex) override;

// Textures
bool hasTexture(std::string name) override;
void setTexture1D(std::string name, unsigned char* texData, unsigned int length) override;
void setTexture2D(std::string name, unsigned char* texData, unsigned int width, unsigned int height,
bool withAlpha = true, bool useMipMap = false, bool repeat = false) override;
void setTextureFromColormap(std::string name, const ValueColorMap& colormap, bool allowUpdate = false) override;
void setTextureFromBuffer(std::string name, TextureBuffer* textureBuffer) override;

// Draw!
void draw() override;
void validateData() override;

protected:
// Classes to keep track of attributes and uniforms
struct GLShaderUniform {
std::string name;
DataType type;
bool isSet; // has a value been assigned to this uniform?
int location;
};

struct GLShaderAttribute {
std::string name;
DataType type;
int arrayCount;
long int dataSize; // the size of the data currently stored in this attribute (-1 if nothing)
int location;
int VBOLoc;
};

struct GLShaderTexture {
std::string name;
int dim;
unsigned int index;
bool isSet;
GLTextureBuffer* textureBuffer;
std::shared_ptr<GLTextureBuffer> textureBufferOwned; // might be empty, if texture isn't owned
int location;
};

// Lists of attributes and uniforms that need to be set
std::vector<GLShaderUniform> uniforms;
std::vector<GLShaderAttribute> attributes;
std::vector<GLShaderTexture> textures;

void addUniqueAttribute(ShaderSpecAttribute attribute);
void addUniqueUniform(ShaderSpecUniform uniform);
void addUniqueTexture(ShaderSpecTexture texture);

private:
// Setup routines
void compileGLProgram(const std::vector<ShaderStageSpecification>& stages);
void setDataLocations();
void createBuffers();

void deleteAttributeBuffer(GLShaderAttribute& attribute);

// Drawing related
void activateTextures();

int nPatchVertices;
};


class MockGLEngine : public Engine {
public:
MockGLEngine();

// High-level control
void initialize();
void checkError(bool fatal = false) override;

void clearDisplay() override;
void bindDisplay() override;
void swapDisplayBuffers() override;
std::vector<unsigned char> readDisplayBuffer() override;

// Manage render state
void pushActiveRenderBuffer() override;
void popActiveRenderBuffer() override;
void setDepthMode(DepthMode newMode = DepthMode::Less) override;
void setBlendMode(BlendMode newMode = BlendMode::Over) override;

// === Windowing and framework things
void makeContextCurrent() override;
void updateWindowSize() override;
std::tuple<int, int> getWindowPos() override;
bool windowRequestsClose() override;
void pollEvents() override;
bool isKeyPressed(char c) override; // for lowercase a-z and 0-9 only
std::string getClipboardText() override;
void setClipboardText(std::string text) override;

// ImGui
void initializeImGui() override;
void shutdownImGui() override;
void ImGuiNewFrame() override;
void ImGuiRender() override;

// === Factory methods

// create textures
std::shared_ptr<TextureBuffer> generateTextureBuffer(TextureFormat format, unsigned int size1D,
unsigned char* data = nullptr) override; // 1d
std::shared_ptr<TextureBuffer> generateTextureBuffer(TextureFormat format, unsigned int size1D,
float* data) override; // 1d
std::shared_ptr<TextureBuffer> generateTextureBuffer(TextureFormat format, unsigned int sizeX_, unsigned int sizeY_,
unsigned char* data = nullptr) override; // 2d
std::shared_ptr<TextureBuffer> generateTextureBuffer(TextureFormat format, unsigned int sizeX_, unsigned int sizeY_,
float* data) override; // 2d

// create render buffers
std::shared_ptr<RenderBuffer> generateRenderBuffer(RenderBufferType type, unsigned int sizeX_,
unsigned int sizeY_) override;

// create frame buffers
std::shared_ptr<FrameBuffer> generateFrameBuffer() override;

// create shader programs
std::shared_ptr<ShaderProgram> generateShaderProgram(const std::vector<ShaderStageSpecification>& stages, DrawMode dm,
unsigned int nPatchVertices = 0) override;

protected:

std::vector<unsigned int> activeRenderBufferStack;

};

} // namespace render
} // namespace polyscope
Loading

0 comments on commit 28e7370

Please sign in to comment.