Initial commit. More or less the default XCode project (deleted a strange setContext...
authorchsieh <chester.developer@hotmail.com>
Fri, 17 Dec 2010 22:55:04 +0000 (14:55 -0800)
committerchsieh <chester.developer@hotmail.com>
Fri, 17 Dec 2010 22:55:04 +0000 (14:55 -0800)
Still need to make sure it compiles and whatnot on OSX, and also add the proper proprocessor defines.

45 files changed:
.gitignore [new file with mode: 0644]
Classes/EAGLView.h [new file with mode: 0644]
Classes/EAGLView.m [new file with mode: 0644]
Classes/Foundation/Common/Assert.h [new file with mode: 0755]
Classes/Foundation/Common/Base.h [new file with mode: 0755]
Classes/Foundation/Common/GlobalDefines.h [new file with mode: 0755]
Classes/Foundation/Common/GlobalInclude.h [new file with mode: 0755]
Classes/Foundation/Common/GlobalTypes.h [new file with mode: 0755]
Classes/Foundation/Common/Print.h [new file with mode: 0755]
Classes/Foundation/Common/Singleton.h [new file with mode: 0755]
Classes/Foundation/Containers/BitEncoder.h [new file with mode: 0755]
Classes/Foundation/Containers/LocklessRingBuffer.cpp [new file with mode: 0755]
Classes/Foundation/Containers/LocklessRingBuffer.h [new file with mode: 0755]
Classes/Foundation/Hash/DJB2.h [new file with mode: 0755]
Classes/Foundation/Math/MathDefines.h [new file with mode: 0755]
Classes/Foundation/Math/MathInclude.h [new file with mode: 0755]
Classes/Foundation/Math/MathOperations.h [new file with mode: 0755]
Classes/Foundation/Math/MathTypes.h [new file with mode: 0755]
Classes/Foundation/Math/Matrix.h [new file with mode: 0755]
Classes/Foundation/Math/Quaternion.h [new file with mode: 0755]
Classes/Foundation/Math/Vector.h [new file with mode: 0755]
Classes/Foundation/Memory/MemoryBitset.cpp [new file with mode: 0755]
Classes/Foundation/Memory/MemoryBitset.h [new file with mode: 0755]
Classes/Foundation/Memory/MemoryHeap.cpp [new file with mode: 0755]
Classes/Foundation/Memory/MemoryHeap.h [new file with mode: 0755]
Classes/Foundation/Memory/MemoryLinear.h [new file with mode: 0755]
Classes/Foundation/Synchronization/Atomic32Bit.h [new file with mode: 0755]
Classes/Foundation/Synchronization/MemorySync.h [new file with mode: 0755]
Classes/GraphicsServices/OpenGLServices.h [new file with mode: 0644]
Classes/LittlestAppDelegate.h [new file with mode: 0644]
Classes/LittlestAppDelegate.m [new file with mode: 0644]
Classes/LittlestViewController.h [new file with mode: 0644]
Classes/LittlestViewController.m [new file with mode: 0644]
Littlest-Info.plist [new file with mode: 0644]
Littlest.xcodeproj/dorischen.mode1v3 [new file with mode: 0644]
Littlest.xcodeproj/dorischen.pbxuser [new file with mode: 0644]
Littlest.xcodeproj/project.pbxproj [new file with mode: 0755]
LittlestViewController.xib [new file with mode: 0644]
Littlest_Prefix.pch [new file with mode: 0644]
MainWindow.xib [new file with mode: 0644]
Shaders/Shader.fsh [new file with mode: 0644]
Shaders/Shader.vsh [new file with mode: 0644]
Shaders/Standard/Standard.fsh [new file with mode: 0644]
Shaders/Standard/Standard.vsh [new file with mode: 0644]
main.m [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..5430402
--- /dev/null
@@ -0,0 +1,2 @@
+# ignore built files
+build/*
diff --git a/Classes/EAGLView.h b/Classes/EAGLView.h
new file mode 100644 (file)
index 0000000..277392d
--- /dev/null
@@ -0,0 +1,37 @@
+//
+//  EAGLView.h
+//  Littlest
+//
+//  Created by Doris Chen on 12/4/10.
+//  Copyright 2010 __MyCompanyName__. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+#import <OpenGLES/ES1/gl.h>
+#import <OpenGLES/ES1/glext.h>
+#import <OpenGLES/ES2/gl.h>
+#import <OpenGLES/ES2/glext.h>
+
+// This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass.
+// The view content is basically an EAGL surface you render your OpenGL scene into.
+// Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel.
+@interface EAGLView : UIView
+{
+@private
+    EAGLContext *context;
+    
+    // The pixel dimensions of the CAEAGLLayer.
+    GLint framebufferWidth;
+    GLint framebufferHeight;
+    
+    // The OpenGL ES names for the framebuffer and renderbuffer used to render to this view.
+    GLuint defaultFramebuffer, colorRenderbuffer;
+}
+
+@property (nonatomic, retain) EAGLContext *context;
+
+- (void)setFramebuffer;
+- (BOOL)presentFramebuffer;
+
+@end
diff --git a/Classes/EAGLView.m b/Classes/EAGLView.m
new file mode 100644 (file)
index 0000000..4fb0237
--- /dev/null
@@ -0,0 +1,163 @@
+//
+//  EAGLView.m
+//  Littlest
+//
+//  Created by Doris Chen on 12/4/10.
+//  Copyright 2010 __MyCompanyName__. All rights reserved.
+//
+
+#import <QuartzCore/QuartzCore.h>
+
+#import "EAGLView.h"
+
+//-----------------------------------------------------------------------------------------------------------
+@interface EAGLView (PrivateMethods)
+- (void)createFrameBuffer;
+- (void)deleteFramebuffer;
+@end
+
+//-----------------------------------------------------------------------------------------------------------
+@implementation EAGLView
+
+@dynamic context;
+
+//-----------------------------------------------------------------------------------------------------------
+// You must implement this method
++ (Class)layerClass
+{
+    return [CAEAGLLayer class];
+}
+
+//-----------------------------------------------------------------------------------------------------------
+//The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:.
+- (id)initWithCoder:(NSCoder*)coder
+{
+    self = [super initWithCoder:coder];
+       if (self)
+    {
+        CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
+        
+        eaglLayer.opaque = TRUE;
+        eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
+                                        [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
+                                        kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
+                                        nil];
+    }
+    
+    return self;
+}
+
+//-----------------------------------------------------------------------------------------------------------
+- (void)dealloc
+{
+    [self deleteFramebuffer];    
+    [context release];
+    
+    [super dealloc];
+}
+
+//-----------------------------------------------------------------------------------------------------------
+- (EAGLContext *)context
+{
+    return context;
+}
+
+//-----------------------------------------------------------------------------------------------------------
+- (void)setContext:(EAGLContext *)newContext
+{
+    if (context != newContext)
+    {
+        [self deleteFramebuffer];
+        [context release];
+               
+        context = [newContext retain];
+    }
+}
+
+//-----------------------------------------------------------------------------------------------------------
+- (void)createFrameBuffer
+{
+    if (context && !defaultFramebuffer)
+    {
+        [EAGLContext setCurrentContext:context];
+        
+        // Create default framebuffer object.
+        glGenFramebuffers(1, &defaultFramebuffer);
+        glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
+        
+        // Create color render buffer and allocate backing store.
+        glGenRenderbuffers(1, &colorRenderbuffer);
+        glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
+        [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
+        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
+        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
+        
+        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
+        
+        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
+            NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
+    }
+}
+
+//-----------------------------------------------------------------------------------------------------------
+- (void)deleteFramebuffer
+{
+    if (context)
+    {
+        [EAGLContext setCurrentContext:context];
+        
+        if (defaultFramebuffer)
+        {
+            glDeleteFramebuffers(1, &defaultFramebuffer);
+            defaultFramebuffer = 0;
+        }
+        
+        if (colorRenderbuffer)
+        {
+            glDeleteRenderbuffers(1, &colorRenderbuffer);
+            colorRenderbuffer = 0;
+        }
+    }
+}
+
+//-----------------------------------------------------------------------------------------------------------
+- (void)setFramebuffer
+{
+    if (context)
+    {
+        [EAGLContext setCurrentContext:context];
+        
+        if (!defaultFramebuffer)
+            [self createFrameBuffer];
+        
+        glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
+        
+        glViewport(0, 0, framebufferWidth, framebufferHeight);
+    }
+}
+
+//-----------------------------------------------------------------------------------------------------------
+- (BOOL)presentFramebuffer
+{
+    BOOL success = FALSE;
+    
+    if (context)
+    {
+        [EAGLContext setCurrentContext:context];
+        
+        glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
+        
+        success = [context presentRenderbuffer:GL_RENDERBUFFER];
+    }
+    
+    return success;
+}
+
+//-----------------------------------------------------------------------------------------------------------
+- (void)layoutSubviews
+{
+    // The framebuffer will be re-created at the beginning of the next setFramebuffer method call.
+    [self deleteFramebuffer];
+}
+
+@end
diff --git a/Classes/Foundation/Common/Assert.h b/Classes/Foundation/Common/Assert.h
new file mode 100755 (executable)
index 0000000..123e36a
--- /dev/null
@@ -0,0 +1,17 @@
+#pragma once\r
+\r
+#ifdef DEBUG\r
+\r
+#include <assert.h>\r
+\r
+#define Assert(expression) if (!(expression)) __debugbreak();\r
+\r
+#define AssertMessage(expression, message, ...) \\r
+  if (!(expression)) Printf(message, ##__VA_ARGS__)\r
+\r
+#else\r
+\r
+slInline void Assert(bool expression) {}\r
+slInline void AssertMsg(bool expression, char* msg, ...) {}\r
+\r
+#endif
\ No newline at end of file
diff --git a/Classes/Foundation/Common/Base.h b/Classes/Foundation/Common/Base.h
new file mode 100755 (executable)
index 0000000..4cf66ae
--- /dev/null
@@ -0,0 +1,11 @@
+#pragma once\r
+\r
+namespace Foundation\r
+{\r
+  class CBase\r
+  {\r
+  public:\r
+    CBase()  {}\r
+    ~CBase() {}\r
+  };\r
+}\r
diff --git a/Classes/Foundation/Common/GlobalDefines.h b/Classes/Foundation/Common/GlobalDefines.h
new file mode 100755 (executable)
index 0000000..58323d7
--- /dev/null
@@ -0,0 +1,120 @@
+#pragma once\r
+\r
+#include "Foundation/Common/GlobalTypes.h"\r
+\r
+#ifdef _WINDOWS\r
+#define __WINDOWS__\r
+#endif\r
+\r
+#ifdef _DEBUG\r
+#define DEBUG\r
+#endif\r
+\r
+#ifdef _NDEBUG\r
+#define RELEASE\r
+#endif\r
+\r
+#ifndef NULL\r
+#define NULL 0\r
+#endif\r
+\r
+#ifndef FALSE\r
+#define FALSE 0\r
+#endif\r
+\r
+#ifndef TRUE\r
+#define TRUE 1\r
+#endif\r
+\r
+//----------------------------------------------------------------------------------------\r
+// cache information\r
+#ifndef CACHE_LINE_SIZE\r
+#define CACHE_LINE_SIZE         128 //equal to 2 line for intel normally\r
+#endif\r
+\r
+//----------------------------------------------------------------------------------------\r
+//force inline on non-debug, might make code explode\r
+#ifdef _DEBUG\r
+  #define slInline                inline\r
+#else\r
+  #define slInline                __forceinline\r
+#endif\r
+\r
+//compiler determined inline\r
+#define clInline                  inline\r
+\r
+#define slRestrict                __restrict\r
+\r
+//----------------------------------------------------------------------------------------\r
+// alignment macros\r
+#if defined(__WINDOWS__)\r
+#define ALIGN(N)                  __declspec(align(N))\r
+#endif\r
+\r
+#define IS_POWER_OF_TWO(x)        ( ((x) & -(x)) == (x) )\r
+\r
+#pragma warning(disable:4146) // =(\r
+#define ALIGN_UP(x, ALIGNMENT)    ( ((x) + (ALIGNMENT) - 1) & -(ALIGNMENT) )\r
+#define ALIGN_DOWN(x, ALIGNMENT)  ( (x) & -(ALIGNMENT) )\r
+\r
+#define POWER_OF_TWO_MOD(x, N)    ( (x) & ((N) - 1) )\r
+\r
+#define ALIGN_CACHE               ( ALIGN( CACHE_LINE_SIZE ) )\r
+\r
+//----------------------------------------------------------------------------------------\r
+// cache macros\r
+#if defined(__WINDOWS__)\r
+// uhh, nothing?\r
+#elif defined(__ARM__)\r
+#define DCBT(x) __asm__("pld" #(x))\r
+#else\r
+#error Not implemented yet!\r
+#endif\r
+\r
+//----------------------------------------------------------------------------------------\r
+// string and memory functions\r
+#if defined(__WINDOWS__)\r
+  #include <string.h>\r
+\r
+  #pragma intrinsic(memcpy)\r
+  #pragma intrinsic(memset)\r
+  #pragma intrinsic(strcmp)\r
+  #pragma intrinsic(strcpy)\r
+  #pragma intrinsic(strlen)\r
+  #pragma intrinsic(strcat)\r
+#endif\r
+\r
+//----------------------------------------------------------------------------------------\r
+// color constants\r
+#define NULL_COLOR  0x00000000\r
+#define BLACK_COLOR 0x000000ff\r
+#define RED_COLOR   0xff0000ff\r
+#define GREEN_COLOR 0x00ff00ff\r
+#define BLUE_COLOR  0x0000ffff\r
+#define WHITE_COLOR 0xffffffff\r
+\r
+//----------------------------------------------------------------------------------------\r
+#if defined(__WINDOWS__)\r
+slInline uint8_t LZCount(uint64_t x)\r
+{\r
+  uint8_t leading_zero_count = 0;\r
+  uint8_t next_shift = 32;\r
+  uint64_t copy = x;\r
+  while (next_shift != 0)\r
+  {\r
+    bool non_zero = copy >= (0x1ULL << next_shift);\r
+    uint8_t actual_shift = (uint8_t)non_zero * next_shift;\r
+    leading_zero_count += actual_shift;\r
+    copy >>= actual_shift;\r
+    next_shift >>= 1;\r
+  }\r
+  leading_zero_count += (copy == 0x1ULL);\r
+\r
+  return leading_zero_count;\r
+}\r
+#else\r
+#error // use lzcnt!\r
+#endif\r
+\r
+//----------------------------------------------------------------------------------------\r
+#define RightMostEnabledBit(x) ((x) & -(x))
\ No newline at end of file
diff --git a/Classes/Foundation/Common/GlobalInclude.h b/Classes/Foundation/Common/GlobalInclude.h
new file mode 100755 (executable)
index 0000000..5ffd39f
--- /dev/null
@@ -0,0 +1,5 @@
+#pragma once\r
+\r
+#include "GlobalDefines.h"\r
+#include "GlobalTypes.h"\r
+#include "Assert.h"\r
diff --git a/Classes/Foundation/Common/GlobalTypes.h b/Classes/Foundation/Common/GlobalTypes.h
new file mode 100755 (executable)
index 0000000..5e1d3d5
--- /dev/null
@@ -0,0 +1,40 @@
+#pragma once\r
+\r
+#ifdef uint32_t\r
+#undef uint32_t\r
+#endif\r
+\r
+typedef signed char         int8_t;\r
+typedef unsigned char       uint8_t;\r
+typedef short               int16_t;\r
+typedef unsigned short      uint16_t;\r
+typedef int                 int32_t;\r
+typedef unsigned int        uint32_t;\r
+typedef long long           int64_t;\r
+typedef unsigned long long  uint64_t;\r
+typedef uint32_t            color32_t;\r
+\r
+union IntFloat\r
+{\r
+  int8_t    m_Int8[4];\r
+  uint8_t   m_UInt8[4];\r
+  int16_t   m_Int16[2];\r
+  uint16_t  m_UInt16[2];\r
+  int32_t   m_Int32;\r
+  uint32_t  m_UInt32;\r
+  float     m_Float;\r
+};\r
+\r
+union LongDouble\r
+{\r
+  int8_t    m_Int8[8];\r
+  uint8_t   m_UInt8[8];\r
+  int16_t   m_Int16[4];\r
+  uint16_t  m_UInt16[4];\r
+  int32_t   m_Int32[2];\r
+  uint32_t  m_UInt32[2];\r
+  int64_t   m_Int64;\r
+  uint64_t  m_UInt64;\r
+  float     m_Float[2];\r
+  double    m_Double;\r
+};\r
diff --git a/Classes/Foundation/Common/Print.h b/Classes/Foundation/Common/Print.h
new file mode 100755 (executable)
index 0000000..c86bdb9
--- /dev/null
@@ -0,0 +1,5 @@
+#pragma once\r
+\r
+#include <stdio.h>\r
+\r
+#define Printf(message, ...) printf(message, ##__VA_ARGS__)\r
diff --git a/Classes/Foundation/Common/Singleton.h b/Classes/Foundation/Common/Singleton.h
new file mode 100755 (executable)
index 0000000..cdbead7
--- /dev/null
@@ -0,0 +1,50 @@
+#pragma once\r
+\r
+#include "Foundation/Common/GlobalInclude.h"\r
+\r
+/*\r
+Statically allocates for faster access\r
+*/\r
+\r
+namespace Foundation\r
+{\r
+\r
+template <typename T> \r
+class Singleton\r
+{\r
+private:\r
+  static T* m_Instance; \r
+\r
+protected:\r
+  Singleton(){m_Instance = 0;}\r
+\r
+public:\r
+  static T* Create()\r
+  {\r
+    Assert( m_Instance == 0);\r
+    if(m_Instance)\r
+    {\r
+      return m_Instance;\r
+    }\r
+    m_Instance = new(16) T();\r
+    return m_Instance;\r
+  }\r
+\r
+  slInline static T* GetInstance()\r
+  {\r
+    //Assert( m_Instance == 0);\r
+    return m_Instance;\r
+  }\r
+\r
+  static void DestroyInstance()\r
+  {\r
+    if(m_Instance)\r
+    {\r
+      delete m_Instance;\r
+    }\r
+  }\r
+};\r
+\r
+template<typename T> T* Singleton<T>::m_Instance = 0;\r
+\r
+}\r
diff --git a/Classes/Foundation/Containers/BitEncoder.h b/Classes/Foundation/Containers/BitEncoder.h
new file mode 100755 (executable)
index 0000000..102e7fc
--- /dev/null
@@ -0,0 +1,10 @@
+#pragma once\r
+\r
+#include "Foundation/Common/GlobalInclude.h"\r
+\r
+#define SET_BITS(x, new_val, shift, mask) ((x) = (((x) & ~(mask)) | (((new_val) << (shift)) & (mask))))\r
+#define ZERO_BITS(x, mask)                ((x) &= ~(mask))\r
+#define OR_BITS (x, new_val, shift, mask) ((x) |= (((new_val) << (shift)) & (mask)))\r
+#define AND_BITS(x, new_val, shift, mask) ((x) &= (((new_val) << (shift)) & (mask)))\r
+#define GET_BITS(x, mask)                 ((x) & (mask))\r
+#define GET_BITS_RIGHT(x, shift, mask)    (((x) & (mask)) >> (shift))\r
diff --git a/Classes/Foundation/Containers/LocklessRingBuffer.cpp b/Classes/Foundation/Containers/LocklessRingBuffer.cpp
new file mode 100755 (executable)
index 0000000..48d7ed7
--- /dev/null
@@ -0,0 +1,161 @@
+#pragma once\r
+\r
+#include "LocklessRingBuffer.h"\r
+#include "Foundation/Synchronization/MemorySync.h"\r
+\r
+//----------------------------------------------------------------------------------------\r
+void LocklessRingBuffer::Init(void* buffer, uint32_t buffer_size)\r
+{\r
+  m_Base = (uint8_t*)buffer;\r
+  m_BufferSize = buffer_size;\r
+  m_WriteOffset = 0;\r
+  m_ReadOffset = 0;\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+void LocklessRingBuffer::Destroy()\r
+{\r
+  m_Base = NULL;\r
+  m_BufferSize = 0;\r
+  m_WriteOffset = 0;\r
+  m_ReadOffset = 0;\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+bool LocklessRingBuffer::Write(void* entry, uint32_t entry_size)\r
+{\r
+  Assert(entry_size < m_BufferSize);\r
+\r
+  uint32_t new_write_offset = m_WriteOffset;\r
+  uint32_t remaining_space = m_WriteOffset < m_ReadOffset ? m_ReadOffset - m_WriteOffset - 1 : m_ReadOffset + m_BufferSize - m_WriteOffset;\r
+  if (remaining_space < entry_size)\r
+  {\r
+    return false;\r
+  }\r
+\r
+  bool write_will_end_lower = new_write_offset + entry_size >= m_BufferSize;\r
+\r
+  uint32_t distance_to_top = (entry_size > m_BufferSize - new_write_offset) ? (m_BufferSize - new_write_offset) : entry_size;\r
+  memcpy(m_Base + new_write_offset, entry, distance_to_top);\r
+  new_write_offset += distance_to_top;\r
+\r
+  if (write_will_end_lower)\r
+  {\r
+    uint32_t remainder = entry_size - distance_to_top;\r
+    memcpy(m_Base, (uint8_t*)entry + distance_to_top, remainder);\r
+    new_write_offset = remainder;\r
+  }\r
+\r
+  ReadWriteSync();\r
+\r
+  m_WriteOffset = new_write_offset;\r
+  return true;\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+bool LocklessRingBuffer::Write(void* entries[], uint32_t entry_sizes[], uint32_t entry_count)\r
+{\r
+  uint32_t total_size = 0;\r
+  for (uint32_t i = 0; i < entry_count; i++)\r
+  {\r
+    total_size += entry_sizes[i];\r
+  }\r
+\r
+  Assert(total_size < m_BufferSize);\r
+\r
+  uint32_t new_write_offset = m_WriteOffset;\r
+  uint32_t remaining_space = m_WriteOffset < m_ReadOffset ? m_ReadOffset - m_WriteOffset - 1 : m_ReadOffset + m_BufferSize - m_WriteOffset;\r
+  if (remaining_space < total_size)\r
+  {\r
+    return false;\r
+  }\r
+\r
+  for (uint32_t i = 0; i < entry_count; i++)\r
+  {\r
+    bool write_will_end_lower = new_write_offset + entry_sizes[i] >= m_BufferSize;\r
+    uint32_t distance_to_top = (entry_sizes[i] > m_BufferSize - new_write_offset) ? (m_BufferSize - new_write_offset) : entry_sizes[i];\r
+    memcpy(m_Base + new_write_offset, entries[i], distance_to_top);\r
+    new_write_offset += distance_to_top;\r
+\r
+    if (write_will_end_lower)\r
+    {\r
+      uint32_t remainder = entry_sizes[i] - distance_to_top;\r
+      memcpy(m_Base, (uint8_t*)entries[i] + distance_to_top, remainder);\r
+      new_write_offset = remainder;\r
+    }\r
+  }\r
+\r
+  ReadWriteSync();\r
+\r
+  m_WriteOffset = new_write_offset;\r
+  return true;\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+bool LocklessRingBuffer::Peek(void* read_buffer, uint32_t read_size)\r
+{\r
+  uint32_t read_remaining = m_WriteOffset < m_ReadOffset ? m_BufferSize - m_ReadOffset + m_WriteOffset : m_WriteOffset - m_ReadOffset;\r
+  if (read_remaining < read_size)\r
+  {\r
+    return false;\r
+  }\r
+\r
+  uint32_t read_to_top = m_BufferSize - m_ReadOffset <= read_size ? m_BufferSize - m_ReadOffset : read_size;\r
+  memcpy(read_buffer, m_Base + m_ReadOffset, read_to_top);\r
+  if (m_BufferSize - m_ReadOffset <= read_size)\r
+  {\r
+    memcpy((uint8_t*)read_buffer + read_to_top, m_Base, read_remaining - read_to_top);\r
+  }\r
+\r
+  return true;\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+bool LocklessRingBuffer::Read(void* read_buffer, uint32_t read_size)\r
+{\r
+  uint32_t read_remaining = m_WriteOffset < m_ReadOffset ? m_BufferSize - m_ReadOffset + m_WriteOffset : m_WriteOffset - m_ReadOffset;\r
+  if (read_remaining < read_size)\r
+  {\r
+    return false;\r
+  }\r
+\r
+  uint32_t read_to_top = m_BufferSize - m_ReadOffset <= read_size ? m_BufferSize - m_ReadOffset : read_size;\r
+  memcpy(read_buffer, m_Base + m_ReadOffset, read_to_top);\r
+  uint32_t new_read_offset = m_ReadOffset + read_to_top;\r
+\r
+  if (m_BufferSize - m_ReadOffset <= read_size)\r
+  {\r
+    memcpy((uint8_t*)read_buffer + read_to_top, m_Base, read_remaining - read_to_top);\r
+    new_read_offset = read_remaining - read_to_top;\r
+  }\r
+\r
+  ReadWriteSync();\r
+\r
+  m_ReadOffset = new_read_offset;\r
+\r
+  return true;\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+bool LocklessRingBuffer::ReadDiscard(uint32_t read_size)\r
+{\r
+  uint32_t read_remaining = m_WriteOffset < m_ReadOffset ? m_BufferSize - m_ReadOffset + m_WriteOffset : m_WriteOffset - m_ReadOffset;\r
+  if (read_remaining < read_size)\r
+  {\r
+    return false;\r
+  }\r
+\r
+  uint32_t read_to_top = m_BufferSize - m_ReadOffset <= read_size ? m_BufferSize - m_ReadOffset : read_size;\r
+  uint32_t new_read_offset = m_ReadOffset + read_to_top;\r
+\r
+  if (m_BufferSize - m_ReadOffset <= read_size)\r
+  {\r
+    new_read_offset = read_remaining - read_to_top;\r
+  }\r
+\r
+  ReadWriteSync();\r
+\r
+  m_ReadOffset = new_read_offset;\r
+\r
+  return true;\r
+}\r
diff --git a/Classes/Foundation/Containers/LocklessRingBuffer.h b/Classes/Foundation/Containers/LocklessRingBuffer.h
new file mode 100755 (executable)
index 0000000..e098a36
--- /dev/null
@@ -0,0 +1,23 @@
+#pragma once\r
+\r
+#include "Foundation/Common/GlobalInclude.h"\r
+\r
+class LocklessRingBuffer\r
+{\r
+private:\r
+  uint8_t*  m_Base;\r
+  uint32_t  m_BufferSize;\r
+  uint32_t  m_WriteOffset;\r
+  uint32_t  m_ReadOffset;\r
+\r
+public:\r
+  void      Init(void* buffer, uint32_t buffer_size);\r
+  void      Destroy();\r
+\r
+  bool      Write(void* entry, uint32_t entry_size);\r
+  bool      Write(void* entries[], uint32_t entry_sizes[], uint32_t entry_count);\r
+\r
+  bool      Peek(void* read_buffer, uint32_t read_size);\r
+  bool      Read(void* read_buffer, uint32_t read_size);\r
+  bool      ReadDiscard(uint32_t read_size);\r
+};\r
diff --git a/Classes/Foundation/Hash/DJB2.h b/Classes/Foundation/Hash/DJB2.h
new file mode 100755 (executable)
index 0000000..30fa608
--- /dev/null
@@ -0,0 +1,25 @@
+#pragma once\r
+\r
+#include "Foundation/Common/GlobalInclude.h"\r
+\r
+//==========================\r
+// djb2 string hash function\r
+//--------------------------\r
+static slInline uint32 StrHash(const char *str)\r
+{\r
+    uint32 hash = 5381;\r
+    int32 c = *str;\r
+\r
+       if(!str)\r
+       {\r
+               Assert(false);\r
+               return 0;\r
+       }\r
+\r
+       while ( (*str != 0) ){\r
+        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */\r
+               c = (*str++);\r
+       }\r
+\r
+    return hash;       \r
+}
\ No newline at end of file
diff --git a/Classes/Foundation/Math/MathDefines.h b/Classes/Foundation/Math/MathDefines.h
new file mode 100755 (executable)
index 0000000..1aedb6b
--- /dev/null
@@ -0,0 +1,14 @@
+#pragma once\r
+\r
+#define kEpsilon     1e-6\r
+#define kSqrtEpsilon 1e-3\r
+\r
+#define kPi       3.1415926535897932384626433832795\r
+#define kPi_2     1.5707963267948966192313216916398\r
+#define kPi_3     1.0471975511965977461542144610932\r
+#define kPi_4     0.78539816339744830961566084581988\r
+#define k2Pi_3    2.0943951023931954923084289221863\r
+#define k3Pi_4    2.3561944901923449288469825374596\r
+#define k2Pi      6.283185307179586476925286766559\r
+\r
+#define kE        2.7182818284590452353602874713527
\ No newline at end of file
diff --git a/Classes/Foundation/Math/MathInclude.h b/Classes/Foundation/Math/MathInclude.h
new file mode 100755 (executable)
index 0000000..718190a
--- /dev/null
@@ -0,0 +1,8 @@
+#pragma once\r
+\r
+#include "Foundation/Math/MathDefines.h"\r
+#include "Foundation/Math/MathOperations.h"\r
+#include "Foundation/Math/MathTypes.h"\r
+#include "Foundation/Math/Vector.h"\r
+#include "Foundation/Math/Quaternion.h"\r
+#include "Foundation/Math/Matrix.h"\r
diff --git a/Classes/Foundation/Math/MathOperations.h b/Classes/Foundation/Math/MathOperations.h
new file mode 100755 (executable)
index 0000000..af49e6f
--- /dev/null
@@ -0,0 +1,75 @@
+#pragma once\r
+\r
+#include <math.h>\r
+#include "Foundation/Common/GlobalInclude.h"\r
+\r
+#if defined(__WINDOWS__)\r
+\r
+#pragma intrinsic(sin)\r
+#pragma intrinsic(cos)\r
+#pragma intrinsic(tan)\r
+#pragma intrinsic(abs)\r
+#pragma intrinsic(fabs)\r
+#pragma intrinsic(asin)\r
+#pragma intrinsic(acos)\r
+#pragma intrinsic(atan)\r
+#pragma intrinsic(atan2)\r
+#pragma intrinsic(exp)\r
+#pragma intrinsic(sqrt)\r
+#pragma intrinsic(log)\r
+#pragma intrinsic(log10)\r
+\r
+#endif\r
+\r
+#define Sinf(x)   sin(x)\r
+#define Cosf(x)   cos(x)\r
+#define Tanf(x)   tan(x)\r
+#define Sqrtf(x)  sqrt(x)\r
+\r
+//----------------------------------------------------------------------------------------\r
+#define Min2(x, y)              ( (x) <= (y) ? (x) : (y) )\r
+#define Max2(x, y)              ( (x) >= (y) ? (x) : (y) )\r
+#define Min3(x, y, z)           ( (x) <= (y) ? ((x) <= (z) ? (x) : (z)) : ((y) <= (z) ? (y) : (z)) )\r
+#define Max3(x, y, z)           ( (x) >= (y) ? ((x) >= (z) ? (x) : (z)) : ((y) >= (z) ? (y) : (z)) )\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline float Absf(const float& f)\r
+{\r
+  IntFloat int_float;\r
+  int_float.m_Float = f;\r
+  int_float.m_Int32 &= 0x7fffffff;\r
+  return int_float.m_Float;\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline bool EpsilonEquals(const float& a, const float& b, const float& epsilon)\r
+{\r
+  return Absf(a - b) <= epsilon;\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline uint16_t ConvertFloatToHalf( unsigned int f )\r
+{\r
+  unsigned int s = f & 0x80000000;\r
+  signed int e = ((f & 0x7f800000) >> 23) - (127 - 15);\r
+  if (e < 0) return 0;\r
+  else if (e > 31)\r
+  {\r
+    e = 31;\r
+  }\r
+  unsigned int fo = f & 0x7fffff;\r
+  return (uint16_t)((s >> 16) | ((e << 10) & 0x7c00) | (fo >> 13));\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline float ConvertHalfToFloat( unsigned short h )\r
+{\r
+  unsigned int s = h & 0x8000;\r
+  unsigned int e = ((h & 0x7c00) >> 10) - 15 + 127;\r
+  unsigned int f = h & 0x3ff;\r
+\r
+  IntFloat int_float;\r
+  int_float.m_UInt32 = ((s << 16) | ((e << 23) & 0x7f800000) | (f << 13));\r
+\r
+  return int_float.m_Float;\r
+}\r
diff --git a/Classes/Foundation/Math/MathTypes.h b/Classes/Foundation/Math/MathTypes.h
new file mode 100755 (executable)
index 0000000..c16a44e
--- /dev/null
@@ -0,0 +1,363 @@
+#pragma once\r
+\r
+#include "Foundation/Common/GlobalInclude.h"\r
+#include "Foundation/Math/MathOperations.h"\r
+\r
+//----------------------------------------------------------------------------------------\r
+struct Vector2\r
+{\r
+  float x;\r
+  float y;\r
+\r
+  slInline explicit Vector2() {}\r
+  slInline explicit Vector2(const float xy) {x = xy; y = xy;}\r
+  slInline explicit Vector2(const float ix, const float iy) {x = ix; y = iy;}\r
+\r
+  slInline Vector2(const Vector2& v) {x = v.x; y = v.y;} // copy constructor\r
+\r
+  slInline float* AsFloatArray() {return &x;}\r
+  slInline const float* AsFloatArray() const {return &x;}\r
+\r
+  slInline float GetComponent(int index) const {return AsFloatArray()[index];}\r
+};\r
+\r
+\r
+//----------------------------------------------------------------------------------------\r
+struct Vector3\r
+{\r
+  float x;\r
+  float y;\r
+  float z;\r
+\r
+  slInline explicit Vector3() {}\r
+  slInline explicit Vector3(const float xyz) {x = xyz; y = xyz; z = xyz;}\r
+  slInline explicit Vector3(const float ix, const float iy, const float iz) {x = ix; y = iy; z = iz;}\r
+\r
+  slInline explicit Vector3(const Vector2& v, const float iz) {x = v.x; y = v.y; z = iz;}\r
+\r
+  slInline Vector3(const Vector3& v) {x = v.x; y = v.y; z = v.z;} // copy constructor\r
+\r
+  slInline Vector2 AsVector2() const {return Vector2(x, y);}\r
+\r
+  slInline float* AsFloatArray() {return &x;}\r
+  slInline const float* AsFloatArray() const {return &x;}\r
+\r
+  slInline float GetComponent(int index) const {return AsFloatArray()[index];}\r
+};\r
+\r
+\r
+//----------------------------------------------------------------------------------------\r
+struct Vector4\r
+{\r
+  float x;\r
+  float y;\r
+  float z;\r
+  float w;\r
+\r
+  slInline explicit Vector4() {}\r
+  slInline explicit Vector4(const float xyzw) {x = xyzw; y = xyzw; z = xyzw; w = xyzw;}\r
+  slInline explicit Vector4(const float xyz, const float iw) {x = xyz; y = xyz; z = xyz; w = iw;}\r
+  slInline explicit Vector4(const float ix, const float iy, const float iz, const float iw) {x = ix; y = iy; z = iz; w = iw;}\r
+\r
+  slInline explicit Vector4(const Vector2& u, const Vector2& v) {x = u.x; y = u.y; z = v.x; w = v.y;}\r
+  slInline explicit Vector4(const Vector2& v, const float iz, const float iw) {x = v.x; y = v.y; z = iz; w = iw;}\r
+\r
+  slInline explicit Vector4(const Vector3& v, const float iw) {x = v.x; y = v.y; z = v.z; w = iw;}\r
+\r
+  slInline Vector4(const Vector4& v) {x = v.x; y = v.y; z = v.z; w = v.w;} // copy constructor\r
+\r
+  slInline Vector2 AsVector2() const {return Vector2(x, y);}\r
+  slInline Vector3 AsVector3() const {return Vector3(x, y, z);}\r
+\r
+  slInline float* AsFloatArray() {return &x;}\r
+  slInline const float* AsFloatArray() const {return &x;}\r
+\r
+  slInline float GetComponent(int index) const {return AsFloatArray()[index];}\r
+};\r
+\r
+\r
+//----------------------------------------------------------------------------------------\r
+struct Quaternion\r
+{\r
+  float i;\r
+  float j;\r
+  float k;\r
+  float s;\r
+\r
+  slInline explicit Quaternion() {}\r
+  explicit Quaternion(const float angle, const Vector3& normalized_axis);\r
+  explicit Quaternion(const float angle, const float x, const float y, const float z);\r
+\r
+  slInline Quaternion(const Quaternion& q) {i = q.i; j = q.j; k = q.k; s = q.s;}\r
+\r
+  Vector4 AsVector4();\r
+  slInline float* AsFloatArray() {return &i;}\r
+\r
+  Vector3 GetImaginary();\r
+  slInline float GetReal() {return s;}\r
+};\r
+\r
+slInline Quaternion::Quaternion(const float angle, const Vector3& normalized_axis)\r
+{\r
+  Assert( EpsilonEquals(LengthVector3(normalized_axis), 1.0f, kEpsilon) );\r
+  float half_angle = angle * 0.5f;\r
+  float sin_half_angle = Sinf(half_angle);\r
+  i = normalized_axis.x * sin_half_angle;\r
+  j = normalized_axis.y * sin_half_angle;\r
+  k = normalized_axis.z * sin_half_angle;\r
+  s = Cosf(half_angle);\r
+}\r
+\r
+slInline Quaternion::Quaternion(const float angle, const float x, const float y, const float z)\r
+{\r
+  Assert( EpsilonEquals(x * x + y * y + z * z, 1.0f, kSqrtEpsilon) );\r
+  float half_angle = angle * 0.5f;\r
+  float sin_half_angle = Sinf(half_angle);\r
+  i = x * sin_half_angle;\r
+  j = y * sin_half_angle;\r
+  k = z * sin_half_angle;\r
+  s = Cosf(half_angle);\r
+}\r
+\r
+Vector4 Quaternion::AsVector4()\r
+{\r
+  Vector4 r;\r
+  r.x = i;\r
+  r.y = j;\r
+  r.z = k;\r
+  r.w = s;\r
+  return r;\r
+}\r
+\r
+Vector3 Quaternion::GetImaginary()\r
+{\r
+  Vector3 r;\r
+  r.x = i;\r
+  r.y = j;\r
+  r.z = k;\r
+  return r;\r
+}\r
+\r
+\r
+//----------------------------------------------------------------------------------------\r
+struct Matrix3\r
+{\r
+  Vector3 v[3];\r
+\r
+  slInline explicit Matrix3() {}\r
+  explicit Matrix3(const Vector3& v0, const Vector3& v1, const Vector3& v2);\r
+  explicit Matrix3(const float v00, const float v01, const float v02,\r
+                   const float v10, const float v11, const float v12,\r
+                   const float v20, const float v21, const float v22);\r
+  Matrix3(Matrix3& m); // copy constructor\r
+\r
+  float* AsFloatArray() {return &v[0].x;}\r
+};\r
+\r
+slInline Matrix3::Matrix3(const Vector3& v0, const Vector3& v1, const Vector3& v2)\r
+{\r
+  v[0] = v0;\r
+  v[1] = v1;\r
+  v[2] = v2;\r
+}\r
+\r
+slInline Matrix3::Matrix3(const float v00, const float v01, const float v02, const float v10, const float v11, const float v12, const float v20, const float v21, const float v22)\r
+{\r
+  v[0].x = v00; v[0].y = v01; v[0].z = v02;\r
+  v[1].x = v10; v[1].y = v11; v[1].z = v12;\r
+  v[2].x = v20; v[2].y = v21; v[2].z = v22;\r
+}\r
+\r
+slInline Matrix3::Matrix3(Matrix3& m)\r
+{\r
+  v[0] = m.v[0];\r
+  v[1] = m.v[1];\r
+  v[2] = m.v[2];\r
+}\r
+\r
+\r
+//----------------------------------------------------------------------------------------\r
+struct Matrix4\r
+{\r
+  Vector4 v[4];\r
+\r
+  slInline explicit Matrix4() {}\r
+  explicit Matrix4(const Vector4& v0, const Vector4& v1, const Vector4& v2, const Vector4& v3);\r
+  explicit Matrix4(const float v00, const float v01, const float v02, const float v03,\r
+                   const float v10, const float v11, const float v12, const float v13,\r
+                   const float v20, const float v21, const float v22, const float v23,\r
+                   const float v30, const float v31, const float v32, const float v33);\r
+  explicit Matrix4(const Matrix3& m);\r
+  Matrix4(const Matrix4& m); // copy constructor\r
+\r
+  float* AsFloatArray() {return &v[0].x;}\r
+};\r
+\r
+slInline Matrix4::Matrix4(const Vector4& v0, const Vector4& v1, const Vector4& v2, const Vector4& v3)\r
+{\r
+  v[0] = v0;\r
+  v[1] = v1;\r
+  v[2] = v2;\r
+  v[3] = v3;\r
+}\r
+\r
+slInline Matrix4::Matrix4(const float v00, const float v01, const float v02, const float v03, const float v10, const float v11, const float v12, const float v13, const float v20, const float v21, const float v22, const float v23, const float v30, const float v31, const float v32, const float v33)\r
+{\r
+  v[0].x = v00; v[0].y = v01; v[0].z = v02; v[0].w = v03;\r
+  v[1].x = v10; v[1].y = v11; v[1].z = v12; v[1].w = v13;\r
+  v[2].x = v20; v[2].y = v21; v[2].z = v22; v[2].w = v23;\r
+  v[3].x = v30; v[3].y = v31; v[3].z = v32; v[3].w = v33;\r
+}\r
+\r
+slInline Matrix4::Matrix4(const Matrix3& m)\r
+{\r
+  v[0].x = m.v[0].x; v[0].y = m.v[0].y; v[0].z = m.v[0].z; v[0].w = 0.0f;\r
+  v[1].x = m.v[1].x; v[1].y = m.v[1].y; v[1].z = m.v[1].z; v[1].w = 0.0f;\r
+  v[2].x = m.v[2].x; v[2].y = m.v[2].y; v[2].z = m.v[2].z; v[2].w = 0.0f;\r
+  v[3].x = 0.0f;     v[3].y = 0.0f;     v[3].z = 0.0f;     v[3].w = 1.0f;\r
+}\r
+\r
+slInline Matrix4::Matrix4(const Matrix4& m)\r
+{\r
+  v[0] = m.v[0];\r
+  v[1] = m.v[1];\r
+  v[2] = m.v[2];\r
+  v[3] = m.v[3];\r
+}\r
+\r
+\r
+//----------------------------------------------------------------------------------------\r
+typedef ALIGN(8)  Vector2  AlignedVector2;\r
+typedef ALIGN(16) Vector3  AlignedVector3;\r
+typedef ALIGN(16) Vector4  AlignedVector4;\r
+\r
+typedef ALIGN(64) Matrix3  AlignedMatrix3;\r
+typedef ALIGN(64) Matrix4  AlignedMatrix4;\r
+\r
+\r
+//----------------------------------------------------------------------------------------\r
+// SIMD Structures\r
+//----------------------------------------------------------------------------------------\r
+struct Vector2SOA\r
+{\r
+  uint32_t m_Count;\r
+  uint32_t m_MaxCount;\r
+\r
+  float* m_X;\r
+  float* m_Y;\r
+\r
+  slInline explicit Vector2SOA() {m_Count = 0; m_MaxCount = 0; m_X = NULL; m_Y = NULL;}\r
+  bool InitVector2SOA(float* buffer, uint32_t buffer_size);\r
+\r
+  void GetVector2AtIndex(Vector2& v, uint32_t index) {Assert(index < m_Count); v.x = m_X[index]; v.y = m_Y[index];}\r
+};\r
+\r
+slInline bool Vector2SOA::InitVector2SOA(float* buffer, uint32_t max_count)\r
+{\r
+  m_Count = 0;\r
+\r
+  uint64_t buffer_addr = (uint64_t)buffer;\r
+  uint64_t buffer_addr_aligned = ALIGN_UP(buffer_addr, 16);\r
+  uint64_t buffer_end = (uint64_t)(buffer + max_count);\r
+\r
+  if (buffer_end - buffer_addr_aligned < 20)\r
+  {\r
+    m_MaxCount = 0;\r
+    m_X = NULL;\r
+    m_Y = NULL;\r
+\r
+    return false;\r
+  }\r
+\r
+  uint32_t remainder = (uint32_t)POWER_OF_TWO_MOD(buffer_end, 16) / 4;\r
+  uint32_t slot_count = (uint32_t)(buffer_end - buffer_addr_aligned) / 32;\r
+\r
+  m_MaxCount = slot_count * 4 + (uint32_t)(remainder * (slot_count & 0x1));\r
+\r
+  m_X = (float*)buffer_addr_aligned;\r
+  m_Y = m_X + ALIGN_UP(m_MaxCount, 4);\r
+}\r
+\r
+\r
+//----------------------------------------------------------------------------------------\r
+struct Vector3SOA : public Vector2SOA\r
+{\r
+  float* m_Z;\r
+\r
+  slInline explicit Vector3SOA() {m_Count = 0; m_MaxCount = 0; m_X = NULL; m_Y = NULL; m_Z = NULL;}\r
+  bool InitVector3SOA(float* buffer, uint32_t buffer_size);\r
+\r
+  void GetVector3AtIndex(Vector3& v, uint32_t index) {Assert(index < m_Count); v.x = m_X[index]; v.y = m_Y[index]; v.z = m_Z[index];}\r
+};\r
+\r
+slInline bool Vector3SOA::InitVector3SOA(float* buffer, uint32_t max_count)\r
+{\r
+  m_Count = 0;\r
+\r
+  uint64_t buffer_addr = (uint64_t)buffer;\r
+  uint64_t buffer_addr_aligned = ALIGN_UP(buffer_addr, 16);\r
+  uint64_t buffer_end = (uint64_t)(buffer + max_count);\r
+\r
+  if (buffer_end - buffer_addr_aligned < 36)\r
+  {\r
+    m_MaxCount = 0;\r
+    m_X = NULL;\r
+    m_Y = NULL;\r
+    m_Z = NULL;\r
+\r
+    return false;\r
+  }\r
+\r
+  uint32_t remainder = (uint32_t)POWER_OF_TWO_MOD(buffer_end, 16) / 4;\r
+  uint32_t slot_count = (uint32_t)(buffer_end - buffer_addr_aligned) / 48;\r
+\r
+  m_MaxCount = slot_count * 4 + (uint32_t)(remainder * (slot_count % 3 == 2));\r
+\r
+  m_X = (float*)buffer_addr_aligned;\r
+  uint32_t aligned_count = ALIGN_UP(m_MaxCount, 4);\r
+  m_Y = m_X + aligned_count;\r
+  m_Z = m_Y + aligned_count;\r
+}\r
+\r
+\r
+//----------------------------------------------------------------------------------------\r
+struct Vector4SOA : public Vector3SOA\r
+{\r
+  float* m_W;\r
+\r
+  slInline explicit Vector4SOA() {m_Count = 0; m_MaxCount = 0; m_X = NULL; m_Y = NULL; m_Z = NULL; m_W = NULL;}\r
+  bool InitVector4SOA(float* buffer, uint32_t buffer_size);\r
+\r
+  void GetVector4AtIndex(Vector4& v, uint32_t index) {Assert(index < m_Count); v.x = m_X[index]; v.y = m_Y[index]; v.z = m_Z[index]; v.w = m_W[index];}\r
+};\r
+\r
+slInline bool Vector4SOA::InitVector4SOA(float* buffer, uint32_t max_count)\r
+{\r
+  m_Count = 0;\r
+\r
+  uint64_t buffer_addr = (uint64_t)buffer;\r
+  uint64_t buffer_addr_aligned = ALIGN_UP(buffer_addr, 16);\r
+  uint64_t buffer_end = (uint64_t)(buffer + max_count);\r
+\r
+  if (buffer_end - buffer_addr_aligned < 52)\r
+  {\r
+    m_MaxCount = 0;\r
+    m_X = NULL;\r
+    m_Y = NULL;\r
+    m_Z = NULL;\r
+    m_W = NULL;\r
+\r
+    return false;\r
+  }\r
+\r
+  uint32_t remainder = (uint32_t)POWER_OF_TWO_MOD(buffer_end, 16) / 4;\r
+  uint32_t slot_count = (uint32_t)(buffer_end - buffer_addr_aligned) / 64;\r
+\r
+  m_MaxCount = slot_count * 4 + (uint32_t)(remainder * ((slot_count & 0x3) == 4));\r
+\r
+  m_X = (float*)buffer_addr_aligned;\r
+  uint32_t aligned_count = ALIGN_UP(m_MaxCount, 4);\r
+  m_Y = m_X + aligned_count;\r
+  m_Z = m_Y + aligned_count;\r
+  m_W = m_Z + aligned_count;\r
+}\r
diff --git a/Classes/Foundation/Math/Matrix.h b/Classes/Foundation/Math/Matrix.h
new file mode 100755 (executable)
index 0000000..37e5362
--- /dev/null
@@ -0,0 +1,660 @@
+#pragma once\r
+\r
+#include "Foundation/Common/GlobalInclude.h"\r
+#include "Foundation/Math/MathOperations.h"\r
+#include "Foundation/Math/MathTypes.h"\r
+#include "Foundation/Math/Vector.h"\r
+\r
+//----------------------------------------------------------------------------------------\r
+// Matrix are expressed in post-multiply row-major format.\r
+// That is, matrix multiplication follows the form v = x * A.\r
+// A local-to-world transform with basis a, b, c, expressed in world space, will result\r
+//   in a matrix that looks like:\r
+//    _   _\r
+//   |  a  |\r
+//   |  b  |\r
+//   |  c  |\r
+//    -   -\r
+//\r
+// Therefore, translate t will naturally follow below c, in the illustration above.\r
+// With this, we can stuff the matrix directly into OpenGL and not have to do swizzling.\r
+//----------------------------------------------------------------------------------------\r
+\r
+//----------------------------------------------------------------------------------------\r
+// Matrix3\r
+//----------------------------------------------------------------------------------------\r
+void SetMatrix3(Matrix3& r, const Matrix3& a);\r
+void SetMatrix3(Matrix3& r, const Vector3& v0, const Vector3& v1, const Vector3& v2);\r
+\r
+void ScaleMatrix3(Matrix3& r, const float s, const Matrix3& a);\r
+Matrix3 ScaleMatrix3(const float s, const Matrix3& a);\r
+\r
+float DeterminantOfMatrix3(const Matrix3& a);\r
+\r
+void TransposeMatrix3(Matrix3& r, const Matrix3& a);\r
+Matrix3 TransposeMatrix3(const Matrix3& a);\r
+\r
+void InvertAffineMatrix3(Matrix3& r, const Matrix3& a);\r
+Matrix3 InvertAffineMatrix3(const Matrix3& a);\r
+\r
+void InvertGeneralMatrix3(Matrix3& r, const Matrix3& a);\r
+Matrix3 InvertGeneralMatrix3(const Matrix3& a);\r
+\r
+void MulMatrix3(Matrix3& r, const Matrix3& a, const Matrix3& b);\r
+Matrix3 MulMatrix3(const Matrix3& a, const Matrix3& b);\r
+\r
+void MulMatrix3ByTransposedMatrix3(Matrix3& r, const Matrix3& a, const Matrix3& b);\r
+Matrix3 MulMatrix3ByTransposedMatrix3(const Matrix3& a, const Matrix3& b);\r
+\r
+void MulVector3ByMatrix3(Vector3& r, const Vector3& v, const Matrix3& a);\r
+Vector3 MulVector3ByMatrix3(const Vector3& v, const Matrix3& a);\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline void SetMatrix3(Matrix3& r, const Matrix3& a)\r
+{\r
+  r.v[0] = a.v[0];\r
+  r.v[1] = a.v[1];\r
+  r.v[2] = a.v[2];\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline void SetMatrix3(Matrix3& r, const Vector3& v0, const Vector3& v1, const Vector3& v2)\r
+{\r
+  r.v[0] = v0;\r
+  r.v[1] = v1;\r
+  r.v[2] = v2;\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline void ScaleMatrix3(Matrix3& r, const float s, const Matrix3& a)\r
+{\r
+  ScaleVector3(r.v[0], s, a.v[0]);\r
+  ScaleVector3(r.v[1], s, a.v[1]);\r
+  ScaleVector3(r.v[2], s, a.v[2]);\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline Matrix3 ScaleMatrix3(const float s, const Matrix3& a)\r
+{\r
+  Matrix3 r;\r
+  ScaleVector3(r.v[0], s, a.v[0]);\r
+  ScaleVector3(r.v[1], s, a.v[1]);\r
+  ScaleVector3(r.v[2], s, a.v[2]);\r
+  return r;\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline float DeterminantOfMatrix3(const Matrix3& a)\r
+{\r
+  return a.v[0].x * (a.v[1].y * a.v[2].z - a.v[1].z * a.v[2].y)\r
+       + a.v[0].y * (a.v[1].z * a.v[2].x - a.v[1].x * a.v[2].z)\r
+       + a.v[0].z * (a.v[1].x * a.v[2].y - a.v[1].y * a.v[2].x);\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline void TransposeMatrix3(Matrix3& r, const Matrix3& a)\r
+{\r
+  // a might be the same as r\r
+  r.v[0].x = a.v[0].x;\r
+  r.v[1].y = a.v[1].y;\r
+  r.v[2].z = a.v[2].z;\r
+\r
+  float temp;\r
+  temp = r.v[0].y;\r
+  r.v[0].y = r.v[1].x;\r
+  r.v[1].x = temp;\r
+\r
+  temp = r.v[0].z;\r
+  r.v[0].z = r.v[2].x;\r
+  r.v[2].x = temp;\r
+\r
+  temp = r.v[1].z;\r
+  r.v[1].z = r.v[2].y;\r
+  r.v[2].y = temp;\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline Matrix3 TransposeMatrix3(const Matrix3& a)\r
+{\r
+  Matrix3 r;\r
+  r.v[0].x = a.v[0].x; r.v[0].y = a.v[1].x; r.v[0].z = a.v[2].x;\r
+  r.v[1].x = a.v[0].y; r.v[1].y = a.v[1].y; r.v[1].z = a.v[2].y;\r
+  r.v[2].x = a.v[0].z; r.v[2].y = a.v[1].z; r.v[2].z = a.v[2].z;\r
+  return r;\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline void InvertAffineMatrix3(Matrix3& r, const Matrix3& a)\r
+{\r
+  TransposeMatrix3(r, a);\r
+}\r
+\r
+//----------------------------------------------------------------------------------------\r
+slInline Matrix3 InvertAffineMatrix3(const Matrix3& a)\r
+{\r
+  return TransposeMatrix3(a);\r
+}\r
+