Slip thin3d underneath DrawBuffer/UI

This commit is contained in:
Henrik Rydgard 2014-08-17 12:17:59 +02:00
parent c1bc4215f2
commit a9b5fe44bf
13 changed files with 122 additions and 101 deletions

View File

@ -170,7 +170,7 @@ static uint32 Hash32Len13to24(const char *s, size_t len) {
uint32 d = Fetch32(s + (len >> 1));
uint32 e = Fetch32(s);
uint32 f = Fetch32(s + len - 4);
uint32 h = len;
uint32 h = (uint32)len;
return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
}

View File

@ -21,7 +21,7 @@ enum {
// #define USE_VBO
DrawBuffer::DrawBuffer() : program_(0), count_(0), atlas(0) {
DrawBuffer::DrawBuffer() : count_(0), atlas(0) {
verts_ = new Vertex[MAX_VERTS];
fontscalex = 1.0f;
fontscaley = 1.0f;
@ -32,30 +32,36 @@ DrawBuffer::~DrawBuffer() {
delete [] verts_;
}
void DrawBuffer::Init(bool registerAsHolder) {
void DrawBuffer::Init(Thin3DContext *t3d, bool registerAsHolder) {
if (inited_)
return;
t3d_ = t3d;
vbuf_ = t3d_->CreateBuffer(MAX_VERTS * sizeof(Vertex), T3DBufferUsage::DYNAMIC | T3DBufferUsage::VERTEXDATA);
inited_ = true;
glGenBuffers(1, (GLuint *)&vbo_);
if (registerAsHolder)
vformat_ = t3d_->CreateVertexFormat(FVF_POS_UV_COLOR);
if (registerAsHolder) {
register_gl_resource_holder(this);
}
}
void DrawBuffer::Shutdown() {
glDeleteBuffers(1, (GLuint *)&vbo_);
vbo_ = 0;
vbuf_->Release();
vformat_->Release();
inited_ = false;
unregister_gl_resource_holder(this);
}
void DrawBuffer::GLLost() {
inited_ = false;
Init(false);
Init(t3d_, false);
}
void DrawBuffer::Begin(const GLSLProgram *program, DrawBufferMode dbmode) {
Init();
program_ = program;
void DrawBuffer::Begin(Thin3DShaderSet *program, DrawBufferPrimitiveMode dbmode) {
shaderSet_ = program;
count_ = 0;
mode_ = dbmode;
}
@ -65,63 +71,19 @@ void DrawBuffer::End() {
}
void DrawBuffer::Flush(bool set_blend_state) {
if (!program_) {
if (!shaderSet_) {
ELOG("No program set!");
return;
}
glsl_bind(program_);
vbuf_->SubData((const uint8_t *)verts_, 0, sizeof(Vertex) * count_);
if (count_ == 0)
return;
#ifdef USE_VBO
glBindBuffer(GL_ARRAY_BUFFER, vbo_);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * count_, verts_, GL_STREAM_DRAW);
if (set_blend_state) {
glstate.blend.enable();
glstate.blendFuncSeparate.set(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glUniform1i(program_->sampler0, 0);
glEnableVertexAttribArray(program_->a_position);
glEnableVertexAttribArray(program_->a_color);
if (program_->a_texcoord0 != -1)
glEnableVertexAttribArray(program_->a_texcoord0);
GL_CHECK();
glVertexAttribPointer(program_->a_position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, x));
glVertexAttribPointer(program_->a_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void *)offsetof(Vertex, rgba));
if (program_->a_texcoord0 != -1)
glVertexAttribPointer(program_->a_texcoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, u));
glDrawArrays(mode_ == DBMODE_LINES ? GL_LINES : GL_TRIANGLES, 0, count_);
GL_CHECK();
glDisableVertexAttribArray(program_->a_position);
glDisableVertexAttribArray(program_->a_color);
if (program_->a_texcoord0 != -1)
glDisableVertexAttribArray(program_->a_texcoord0);
GL_CHECK();
glBindBuffer(GL_ARRAY_BUFFER, 0);
#else
if (set_blend_state) {
glstate.blend.enable();
glstate.blendFuncSeparate.set(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glUniform1i(program_->sampler0, 0);
glEnableVertexAttribArray(program_->a_position);
glEnableVertexAttribArray(program_->a_color);
if (program_->a_texcoord0 != -1)
glEnableVertexAttribArray(program_->a_texcoord0);
GL_CHECK();
glVertexAttribPointer(program_->a_position, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)&verts_[0].x);
glVertexAttribPointer(program_->a_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void *)&verts_[0].rgba);
if (program_->a_texcoord0 != -1)
glVertexAttribPointer(program_->a_texcoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)&verts_[0].u);
glDrawArrays(mode_ == DBMODE_LINES ? GL_LINES : GL_TRIANGLES, 0, count_);
GL_CHECK();
glDisableVertexAttribArray(program_->a_position);
glDisableVertexAttribArray(program_->a_color);
if (program_->a_texcoord0 != -1)
glDisableVertexAttribArray(program_->a_texcoord0);
GL_CHECK();
#endif
int offset = 0;
shaderSet_->SetMatrix4x4("WorldViewProj", drawMatrix_);
t3d_->Draw(mode_ == DBMODE_NORMAL ? PRIM_TRIANGLES : PRIM_LINES, shaderSet_, vformat_, vbuf_, count_, offset);
count_ = 0;
}

View File

@ -7,6 +7,8 @@
#include "gfx/gl_lost_manager.h"
#include "gfx/texture_atlas.h"
#include "math/geom2d.h"
#include "math/lin/matrix4x4.h"
#include "thin3d/thin3d.h"
#undef DrawText
@ -39,9 +41,9 @@ enum {
FLAG_NO_PREFIX = 4096 // means to not process ampersands
};
struct GLSLProgram;
class Thin3DShaderSet;
enum DrawBufferMode {
enum DrawBufferPrimitiveMode {
DBMODE_NORMAL = 0,
DBMODE_LINES = 1
};
@ -58,18 +60,18 @@ public:
DrawBuffer();
~DrawBuffer();
void Begin(const GLSLProgram *program, DrawBufferMode mode = DBMODE_NORMAL);
void Begin(Thin3DShaderSet *shaders, DrawBufferPrimitiveMode mode = DBMODE_NORMAL);
void End();
// TODO: Enforce these. Now Init is autocalled and shutdown not called.
void Init(bool registerAsHolder = true);
void Init(Thin3DContext *t3d, bool registerAsHolder = true);
void Shutdown();
virtual void GLLost();
int Count() const { return count_; }
void Flush(bool set_blend_state=true);
void Flush(bool set_blend_state = true);
void Rect(float x, float y, float w, float h, uint32 color, int align = ALIGN_TOPLEFT);
void hLine(float x1, float y, float x2, uint32 color);
@ -148,19 +150,27 @@ public:
static void DoAlign(int flags, float *x, float *y, float *w, float *h);
void SetDrawMatrix(const Matrix4x4 &m) {
drawMatrix_ = m;
}
private:
struct Vertex {
float x, y, z;
uint32_t rgba;
float u, v;
uint32_t rgba;
};
const GLSLProgram *program_;
Matrix4x4 drawMatrix_;
Thin3DContext *t3d_;
Thin3DBuffer *vbuf_;
Thin3DVertexFormat *vformat_;
Thin3DShaderSet *shaderSet_;
int vbo_;
Vertex *verts_;
int count_;
DrawBufferMode mode_;
DrawBufferPrimitiveMode mode_;
const Atlas *atlas;
bool inited_;

View File

@ -321,6 +321,10 @@ void CheckGLExtensions() {
#endif
ProcessGPUFeatures();
int error = glGetError();
if (error)
ELOG("GL error in init: %i", error);
}
void OpenGLState::SetVSyncInterval(int interval) {

View File

@ -81,7 +81,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories);</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\dx9sdk\Include;..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories);</AdditionalIncludeDirectories>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
@ -102,7 +102,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories);</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\dx9sdk\Include;..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories);</AdditionalIncludeDirectories>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
@ -124,7 +124,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories);</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\dx9sdk\Include;..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories);</AdditionalIncludeDirectories>
<EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
<FloatingPointModel>Fast</FloatingPointModel>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
@ -149,7 +149,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories);</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\dx9sdk\Include;..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories);</AdditionalIncludeDirectories>
<EnableEnhancedInstructionSet>NotSet</EnableEnhancedInstructionSet>
<FloatingPointModel>Fast</FloatingPointModel>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
@ -272,6 +272,8 @@
<ClInclude Include="net\resolve.h" />
<ClInclude Include="net\url.h" />
<ClInclude Include="profiler\profiler.h" />
<ClInclude Include="thin3d\d3dx9_loader.h" />
<ClInclude Include="thin3d\thin3d.h" />
<ClInclude Include="thread\prioritizedworkqueue.h" />
<ClInclude Include="thread\thread.h" />
<ClInclude Include="thread\threadpool.h" />
@ -756,6 +758,10 @@
<ClCompile Include="net\resolve.cpp" />
<ClCompile Include="net\url.cpp" />
<ClCompile Include="profiler\profiler.cpp" />
<ClCompile Include="thin3d\d3dx9_loader.cpp" />
<ClCompile Include="thin3d\thin3d.cpp" />
<ClCompile Include="thin3d\thin3d_d3d9.cpp" />
<ClCompile Include="thin3d\thin3d_gl.cpp" />
<ClCompile Include="thread\prioritizedworkqueue.cpp" />
<ClCompile Include="thread\threadpool.cpp" />
<ClCompile Include="thread\threadutil.cpp" />
@ -778,4 +784,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -344,6 +344,12 @@
<ClInclude Include="util\text\shiftjis.h">
<Filter>util</Filter>
</ClInclude>
<ClInclude Include="thin3d\thin3d.h">
<Filter>thin3d</Filter>
</ClInclude>
<ClInclude Include="thin3d\d3dx9_loader.h">
<Filter>thin3d</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="gfx\gl_debug_log.cpp">
@ -802,6 +808,18 @@
<ClCompile Include="math\fast\fast_matrix_sse.c">
<Filter>math\fast</Filter>
</ClCompile>
<ClCompile Include="thin3d\thin3d_gl.cpp">
<Filter>thin3d</Filter>
</ClCompile>
<ClCompile Include="thin3d\thin3d_d3d9.cpp">
<Filter>thin3d</Filter>
</ClCompile>
<ClCompile Include="thin3d\thin3d.cpp">
<Filter>thin3d</Filter>
</ClCompile>
<ClCompile Include="thin3d\d3dx9_loader.cpp">
<Filter>thin3d</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="gfx">
@ -882,5 +900,8 @@
<Filter Include="math\fast">
<UniqueIdentifier>{5ce64c0e-98e4-4411-86cc-aacaf2f60b21}</UniqueIdentifier>
</Filter>
<Filter Include="thin3d">
<UniqueIdentifier>{06c6305a-a646-485b-85b9-645a24dd6553}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>
</Project>

View File

@ -35,6 +35,7 @@ enum DialogResult {
class ScreenManager;
class UIContext;
class Thin3DContext;
class Screen {
public:
@ -94,6 +95,9 @@ public:
void setUIContext(UIContext *context) { uiContext_ = context; }
UIContext *getUIContext() { return uiContext_; }
void setThin3DContext(Thin3DContext *context) { thin3DContext_ = context; }
Thin3DContext *getThin3DContext() { return thin3DContext_; }
void render();
void resized();
void deviceLost();
@ -125,6 +129,7 @@ private:
Screen *nextScreen_;
UIContext *uiContext_;
Thin3DContext *thin3DContext_;
const Screen *dialogFinished_;
DialogResult dialogResult_;

View File

@ -77,11 +77,11 @@ bool UIRegionHit(int i, int x, int y, int w, int h, int margin) {
}
}
void UIBegin(const GLSLProgram *shader) {
void UIBegin(Thin3DShaderSet *shaderSet) {
for (int i = 0; i < MAX_POINTERS; i++)
uistate.hotitem[i] = 0;
ui_draw2d.Begin(shader);
ui_draw2d_front.Begin(shader);
ui_draw2d.Begin(shaderSet);
ui_draw2d_front.Begin(shaderSet);
}
void UIFlush() {
@ -89,7 +89,6 @@ void UIFlush() {
ui_draw2d_front.Flush();
}
void UIEnd() {
for (int i = 0; i < MAX_POINTERS; i++) {
if (uistate.mousedown[i] == 0) {

View File

@ -225,7 +225,7 @@ private:
bool UIRegionHit(int pointerId, int x, int y, int w, int h, int margin);
// Call at start of frame
void UIBegin(const GLSLProgram *shader);
void UIBegin(Thin3DShaderSet *shaderSet);
// Call at end of frame.

View File

@ -17,11 +17,16 @@ UIContext::UIContext()
}
UIContext::~UIContext() {
blend_->Release();
delete fontStyle_;
delete textDrawer_;
}
void UIContext::Init(const GLSLProgram *uishader, const GLSLProgram *uishadernotex, Texture *uitexture, DrawBuffer *uidrawbuffer, DrawBuffer *uidrawbufferTop) {
void UIContext::Init(Thin3DContext *thin3d, Thin3DShaderSet *uishader, Thin3DShaderSet *uishadernotex, Texture *uitexture, DrawBuffer *uidrawbuffer, DrawBuffer *uidrawbufferTop) {
thin3d_ = thin3d;
blend_ = thin3d_->GetBlendStatePreset(T3DBlendStatePreset::BS_STANDARD_ALPHA);
depth_ = thin3d_->CreateDepthStencilState(false, false, T3DComparison::LESS);
uishader_ = uishader;
uishadernotex_ = uishadernotex;
uitexture_ = uitexture;
@ -35,19 +40,15 @@ void UIContext::Init(const GLSLProgram *uishader, const GLSLProgram *uishadernot
}
void UIContext::Begin() {
glstate.blend.enable();
glstate.blendFuncSeparate.set(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glstate.blendEquationSeparate.set(GL_FUNC_ADD, GL_FUNC_ADD);
thin3d_->SetBlendState(blend_);
int error = glGetError();
glstate.cullFace.disable();
glstate.depthTest.disable();
glstate.dither.enable();
#if !defined(USING_GLES2)
glstate.colorLogicOp.disable();
#endif
if (uishader_)
glsl_bind(uishader_);
if (uitexture_)
uitexture_->Bind(0);
error = glGetError();
UIBegin(uishader_);
}
@ -62,8 +63,6 @@ void UIContext::BeginNoTex() {
#if !defined(USING_GLES2)
glstate.colorLogicOp.disable();
#endif
if (uishader_)
glsl_bind(uishader_);
if (uitexture_)
uitexture_->Bind(0);

View File

@ -9,7 +9,10 @@
// Everything you need to draw a UI collected into a single unit that can be passed around.
// Everything forward declared so this header is safe everywhere.
struct GLSLProgram;
class Thin3DContext;
class Thin3DShaderSet;
class Thin3DDepthStencilState;
class Thin3DBlendState;
class Texture;
class DrawBuffer;
class TextDrawer;
@ -27,7 +30,7 @@ public:
UIContext();
~UIContext();
void Init(const GLSLProgram *uishader, const GLSLProgram *uishadernotex, Texture *uitexture, DrawBuffer *uidrawbuffer, DrawBuffer *uidrawbufferTop);
void Init(Thin3DContext *thin3d, Thin3DShaderSet *uiShaderTex, Thin3DShaderSet *uiShaderNoTex, Texture *uitexture, DrawBuffer *uidrawbuffer, DrawBuffer *uidrawbufferTop);
void Begin();
void BeginNoTex();
@ -63,17 +66,23 @@ public:
// in dps, like dp_xres and dp_yres
void SetBounds(const Bounds &b) { bounds_ = b; }
const Bounds &GetBounds() const { return bounds_; }
Thin3DContext *GetThin3DContext() { return thin3d_; }
private:
Thin3DContext *thin3d_;
Bounds bounds_;
float fontScaleX_;
float fontScaleY_;
UI::FontStyle *fontStyle_;
TextDrawer *textDrawer_;
// TODO: Collect these into a UIContext
const GLSLProgram *uishader_;
const GLSLProgram *uishadernotex_;
Thin3DContext *thin3D_;
Thin3DDepthStencilState *depth_;
Thin3DBlendState *blend_;
Thin3DShaderSet *uishader_;
Thin3DShaderSet *uishadernotex_;
Texture *uitexture_;
DrawBuffer *uidrawbuffer_;
DrawBuffer *uidrawbufferTop_;

View File

@ -4,6 +4,7 @@
#include "ui/viewgroup.h"
class I18NCategory;
class Thin3DContext;
class UIScreen : public Screen {
public:
@ -32,6 +33,7 @@ protected:
private:
void DoRecreateViews();
bool recreateViews_;
int hatDown_;

View File

@ -41,7 +41,11 @@ public:
// Takes ownership! DO NOT add a view to multiple parents!
template <class T>
T *Add(T *view) { views_.push_back(view); return view; }
T *Add(T *view) {
lock_guard guard(modifyLock_);
views_.push_back(view);
return view;
}
virtual bool SetFocus();
virtual bool SubviewFocused(View *view);