diff --git a/ext/cityhash/city.cpp b/ext/cityhash/city.cpp index 28e766b010..4e088af1c8 100644 --- a/ext/cityhash/city.cpp +++ b/ext/cityhash/city.cpp @@ -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))))))); } diff --git a/gfx_es2/draw_buffer.cpp b/gfx_es2/draw_buffer.cpp index 4ce0b69ce9..a89f42099b 100644 --- a/gfx_es2/draw_buffer.cpp +++ b/gfx_es2/draw_buffer.cpp @@ -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; } diff --git a/gfx_es2/draw_buffer.h b/gfx_es2/draw_buffer.h index 914f7cc746..112b2a45b4 100644 --- a/gfx_es2/draw_buffer.h +++ b/gfx_es2/draw_buffer.h @@ -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_; diff --git a/gfx_es2/gl_state.cpp b/gfx_es2/gl_state.cpp index a240d0302f..b8f8c95367 100644 --- a/gfx_es2/gl_state.cpp +++ b/gfx_es2/gl_state.cpp @@ -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) { diff --git a/native.vcxproj b/native.vcxproj index 831a61317a..fc39463309 100644 --- a/native.vcxproj +++ b/native.vcxproj @@ -81,7 +81,7 @@ Level3 Disabled WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) - ..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories); + ..\dx9sdk\Include;..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories); ProgramDatabase true false @@ -102,7 +102,7 @@ Level3 Disabled WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) - ..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories); + ..\dx9sdk\Include;..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories); true false false @@ -124,7 +124,7 @@ true true WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) - ..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories); + ..\dx9sdk\Include;..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories); StreamingSIMDExtensions2 Fast true @@ -149,7 +149,7 @@ true true WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) - ..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories); + ..\dx9sdk\Include;..\zlib;..\ext\zlib;..\native;..\RollerballGL;..\native\ext\glew;..\SDL\include;..\native\ext;%(AdditionalIncludeDirectories); NotSet Fast true @@ -272,6 +272,8 @@ + + @@ -756,6 +758,10 @@ + + + + @@ -778,4 +784,4 @@ - + \ No newline at end of file diff --git a/native.vcxproj.filters b/native.vcxproj.filters index 8272c3f79d..5025807740 100644 --- a/native.vcxproj.filters +++ b/native.vcxproj.filters @@ -344,6 +344,12 @@ util + + thin3d + + + thin3d + @@ -802,6 +808,18 @@ math\fast + + thin3d + + + thin3d + + + thin3d + + + thin3d + @@ -882,5 +900,8 @@ {5ce64c0e-98e4-4411-86cc-aacaf2f60b21} + + {06c6305a-a646-485b-85b9-645a24dd6553} + - + \ No newline at end of file diff --git a/ui/screen.h b/ui/screen.h index 8992c235a1..1c8aa7da00 100644 --- a/ui/screen.h +++ b/ui/screen.h @@ -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_; diff --git a/ui/ui.cpp b/ui/ui.cpp index 08520698a2..e8c4a568c4 100644 --- a/ui/ui.cpp +++ b/ui/ui.cpp @@ -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) { diff --git a/ui/ui.h b/ui/ui.h index f00dd5f58b..b9f1d72e2f 100644 --- a/ui/ui.h +++ b/ui/ui.h @@ -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. diff --git a/ui/ui_context.cpp b/ui/ui_context.cpp index 5bac398568..2378e5d7f9 100644 --- a/ui/ui_context.cpp +++ b/ui/ui_context.cpp @@ -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); diff --git a/ui/ui_context.h b/ui/ui_context.h index 5fff30ec35..e0be7df327 100644 --- a/ui/ui_context.h +++ b/ui/ui_context.h @@ -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_; diff --git a/ui/ui_screen.h b/ui/ui_screen.h index 868de6580e..85cc5f7863 100644 --- a/ui/ui_screen.h +++ b/ui/ui_screen.h @@ -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_; diff --git a/ui/viewgroup.h b/ui/viewgroup.h index e331c63283..4406313117 100644 --- a/ui/viewgroup.h +++ b/ui/viewgroup.h @@ -41,7 +41,11 @@ public: // Takes ownership! DO NOT add a view to multiple parents! template - 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);