Skip to content

Commit

Permalink
update mock backend to match latest
Browse files Browse the repository at this point in the history
  • Loading branch information
nmwsharp committed Mar 25, 2020
1 parent 377b6ec commit 5da48e6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 41 deletions.
48 changes: 26 additions & 22 deletions include/polyscope/render/mock_opengl/mock_gl_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class GLTextureBuffer : public TextureBuffer {
// 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(TextureFormat format, unsigned int sizeX_, unsigned int sizeY_, unsigned int nSamples);

~GLTextureBuffer() override;

Expand All @@ -42,6 +43,7 @@ class GLTextureBuffer : public TextureBuffer {
class GLRenderBuffer : public RenderBuffer {
public:
GLRenderBuffer(RenderBufferType type, unsigned int sizeX_, unsigned int sizeY_);
GLRenderBuffer(RenderBufferType type, unsigned int sizeX_, unsigned int sizeY_, unsigned int nSamples);
~GLRenderBuffer() override;

void resize(unsigned int newX, unsigned int newY) override;
Expand All @@ -55,7 +57,7 @@ class GLRenderBuffer : public RenderBuffer {
class GLFrameBuffer : public FrameBuffer {

public:
GLFrameBuffer();
GLFrameBuffer(unsigned int sizeX_, unsigned int sizeY_, bool isDefault = false);
~GLFrameBuffer() override;

// Bind to this framebuffer so subsequent draw calls will go to it
Expand All @@ -76,10 +78,11 @@ class GLFrameBuffer : public FrameBuffer {
// Query pixels
std::array<float, 4> readFloat4(int xPos, int yPos) override;

void blitTo(FrameBuffer* other) override;

// Getters

protected:

void bind();
};

Expand Down Expand Up @@ -198,35 +201,34 @@ class MockGLEngine : public Engine {
MockGLEngine();

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

void clearDisplay() override;
void bindDisplay() override;
void swapDisplayBuffers() 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
void setColorMask(std::array<bool, 4> mask = {true, true, true, true}) override;

// === Windowing and framework things
void makeContextCurrent() override;
void updateWindowSize(bool force = false) 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

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

// === Factory methods

Expand All @@ -239,22 +241,24 @@ class MockGLEngine : public Engine {
unsigned char* data = nullptr) override; // 2d
std::shared_ptr<TextureBuffer> generateTextureBuffer(TextureFormat format, unsigned int sizeX_, unsigned int sizeY_,
float* data) override; // 2d
std::shared_ptr<TextureBuffer> generateTextureBufferMultisample(TextureFormat format, unsigned int sizeX_,
unsigned int sizeY_,
unsigned int nSamples) override; // 2d

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

// create frame buffers
std::shared_ptr<FrameBuffer> generateFrameBuffer() override;
std::shared_ptr<FrameBuffer> generateFrameBuffer(unsigned int sizeX_, unsigned int sizeY_) 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
Expand Down
80 changes: 61 additions & 19 deletions src/render/mock_opengl/mock_gl_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int sizeX_, uns
setFilterMode(FilterMode::Nearest);
}

GLTextureBuffer::GLTextureBuffer(TextureFormat format_, unsigned int sizeX_, unsigned int sizeY_, unsigned int nSamples)
: TextureBuffer(2, format_, sizeX_, sizeY_) {

isMultisample = true;
multisampleCount = nSamples;

// setFilterMode(FilterMode::Nearest); // openGL rejects this?
}

GLTextureBuffer::~GLTextureBuffer() {}

void GLTextureBuffer::resize(unsigned int newLen) {
Expand Down Expand Up @@ -135,6 +144,14 @@ GLRenderBuffer::GLRenderBuffer(RenderBufferType type_, unsigned int sizeX_, unsi
checkGLError();
resize(sizeX, sizeY);
}
GLRenderBuffer::GLRenderBuffer(RenderBufferType type_, unsigned int sizeX_, unsigned int sizeY_, unsigned int nSamples_)
: RenderBuffer(type_, sizeX_, sizeY_) {
isMultisample = true;
multisampleCount = nSamples_;
checkGLError();
resize(sizeX, sizeY);
}


GLRenderBuffer::~GLRenderBuffer() {}

Expand All @@ -151,7 +168,14 @@ void GLRenderBuffer::bind() { checkGLError(); }
// ===================== Framebuffer ===========================
// =============================================================

GLFrameBuffer::GLFrameBuffer() { checkGLError(); };
GLFrameBuffer::GLFrameBuffer(unsigned int sizeX_, unsigned int sizeY_, bool isDefault) {
sizeX = sizeX_;
sizeY = sizeY_;
if (isDefault) {
} else {
}
checkGLError();
};

GLFrameBuffer::~GLFrameBuffer() {}

Expand All @@ -167,7 +191,7 @@ void GLFrameBuffer::addColorBuffer(std::shared_ptr<RenderBuffer> renderBufferIn)
bind();

checkGLError();
renderBuffers.push_back(renderBuffer);
renderBuffersColor.push_back(renderBuffer);
nColorBuffers++;
}

Expand All @@ -184,7 +208,7 @@ void GLFrameBuffer::addDepthBuffer(std::shared_ptr<RenderBuffer> renderBufferIn)
// if (depthTextureBuffer != nullptr) throw std::runtime_error("OpenGL error: already bound to texture buffer");

checkGLError();
renderBuffers.push_back(renderBuffer);
renderBuffersDepth.push_back(renderBuffer);
}

void GLFrameBuffer::addColorBuffer(std::shared_ptr<TextureBuffer> textureBufferIn) {
Expand All @@ -202,7 +226,7 @@ void GLFrameBuffer::addColorBuffer(std::shared_ptr<TextureBuffer> textureBufferI
// if (colorTextureBuffer != nullptr) throw std::runtime_error("OpenGL error: already bound to texture buffer");

checkGLError();
textureBuffers.push_back(textureBuffer);
textureBuffersColor.push_back(textureBuffer);
nColorBuffers++;
}

Expand All @@ -221,7 +245,7 @@ void GLFrameBuffer::addDepthBuffer(std::shared_ptr<TextureBuffer> textureBufferI
// if (depthTextureBuffer != nullptr) throw std::runtime_error("OpenGL error: already bound to texture buffer");

checkGLError();
textureBuffers.push_back(textureBuffer);
textureBuffersDepth.push_back(textureBuffer);
}

void GLFrameBuffer::setDrawBuffers() {
Expand All @@ -231,6 +255,7 @@ void GLFrameBuffer::setDrawBuffers() {

bool GLFrameBuffer::bindForRendering() {
bind();
render::engine->setCurrentViewport({0, 0, 400, 600});
checkGLError();
return true;
}
Expand All @@ -241,11 +266,21 @@ void GLFrameBuffer::clear() {

std::array<float, 4> GLFrameBuffer::readFloat4(int xPos, int yPos) {
// Read from the buffer
std::array<float, 4> result;
std::array<float, 4> result = {1., 2., 3., 4.};

return result;
}

void GLFrameBuffer::blitTo(FrameBuffer* targetIn) {

// it _better_ be a GL buffer
GLFrameBuffer* target = dynamic_cast<GLFrameBuffer*>(targetIn);
if (!target) throw std::runtime_error("tried to blitTo() non-GL framebuffer");

// target->bindForRendering();
bindForRendering();
checkGLError();
}

// =============================================================
// ================== Shader Program =========================
Expand Down Expand Up @@ -878,8 +913,7 @@ void GLShaderProgram::setTextureFromBuffer(std::string name, TextureBuffer* text
}

void GLShaderProgram::setTextureFromColormap(std::string name, const std::string& colormapName, bool allowUpdate) {
const ValueColorMap& colormap = getColorMap(colormapName);
// TODO switch to global shared buffers from colormap
const ValueColorMap& colormap = render::engine->getColorMap(colormapName);

// Find the right texture
for (GLShaderTexture& t : textures) {
Expand Down Expand Up @@ -1110,21 +1144,14 @@ std::vector<unsigned char> MockGLEngine::readDisplayBuffer() {

void MockGLEngine::checkError(bool fatal) { checkGLError(fatal); }

void MockGLEngine::pushActiveRenderBuffer() { activeRenderBufferStack.push_back(7); }

void MockGLEngine::popActiveRenderBuffer() {
if (activeRenderBufferStack.empty()) throw std::runtime_error("tried to pop from empty render buffer stack");
activeRenderBufferStack.pop_back();
}

void MockGLEngine::makeContextCurrent() {}

void MockGLEngine::updateWindowSize() {
void MockGLEngine::updateWindowSize(bool force) {
int newBufferWidth = 400;
int newBufferHeight = 600;
int newWindowWidth = 400;
int newWindowHeight = 600;
if (newBufferWidth != view::bufferWidth || newBufferHeight != view::bufferHeight ||
if (force || newBufferWidth != view::bufferWidth || newBufferHeight != view::bufferHeight ||
newWindowHeight != view::windowHeight || newWindowWidth != view::windowWidth) {
// Basically a resize callback
requestRedraw();
Expand Down Expand Up @@ -1163,6 +1190,8 @@ void MockGLEngine::setDepthMode(DepthMode newMode) {}

void MockGLEngine::setBlendMode(BlendMode newMode) {}

void MockGLEngine::setColorMask(std::array<bool, 4> mask) {}

std::string MockGLEngine::getClipboardText() {
std::string clipboardData = "";
return clipboardData;
Expand Down Expand Up @@ -1193,14 +1222,27 @@ std::shared_ptr<TextureBuffer> MockGLEngine::generateTextureBuffer(TextureFormat
return std::shared_ptr<TextureBuffer>(newT);
}

std::shared_ptr<TextureBuffer> MockGLEngine::generateTextureBufferMultisample(TextureFormat format, unsigned int sizeX_,
unsigned int sizeY_,
unsigned int nSamples) {
GLTextureBuffer* newT = new GLTextureBuffer(format, sizeX_, sizeY_, nSamples);
return std::shared_ptr<TextureBuffer>(newT);
}

std::shared_ptr<RenderBuffer> MockGLEngine::generateRenderBuffer(RenderBufferType type, unsigned int sizeX_,
unsigned int sizeY_) {
GLRenderBuffer* newR = new GLRenderBuffer(type, sizeX_, sizeY_);
return std::shared_ptr<RenderBuffer>(newR);
}
std::shared_ptr<RenderBuffer> MockGLEngine::generateRenderBufferMultisample(RenderBufferType type, unsigned int sizeX_,
unsigned int sizeY_,
unsigned int nSamples) {
GLRenderBuffer* newR = new GLRenderBuffer(type, sizeX_, sizeY_, nSamples);
return std::shared_ptr<RenderBuffer>(newR);
}

std::shared_ptr<FrameBuffer> MockGLEngine::generateFrameBuffer() {
GLFrameBuffer* newF = new GLFrameBuffer();
std::shared_ptr<FrameBuffer> MockGLEngine::generateFrameBuffer(unsigned int sizeX_, unsigned int sizeY_) {
GLFrameBuffer* newF = new GLFrameBuffer(sizeX_, sizeY_);
return std::shared_ptr<FrameBuffer>(newF);
}

Expand Down

0 comments on commit 5da48e6

Please sign in to comment.