aboutsummaryrefslogtreecommitdiff
path: root/Gem/develop/include/Gem/Gem/ContextData.h
blob: 74804daf0818dc21b71812e60eef9ce2d8c2c91b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*-----------------------------------------------------------------
  LOG
  GEM - Graphics Environment for Multimedia

  data specific to a rendering context

  Copyright (c) 2009-2011 IOhannes m zmölnig. forum::für::umläute. IEM. zmoelnig@iem.at
  For information on usage and redistribution, and for a DISCLAIMER OF ALL
  WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.

  -----------------------------------------------------------------*/

#ifndef _INCLUDE__GEM_GEM_CONTEXTDATA_H_
#define _INCLUDE__GEM_GEM_CONTEXTDATA_H_

#include "Gem/ExportDef.h"
#include <vector>

/*-----------------------------------------------------------------
  -------------------------------------------------------------------
  CLASS
  ContextData

  rendering context specific data
  this is heavily inspired by VrJuggler

  DESCRIPTION

  several things in openGL like display-lists are context dependent
  if we have multiple contexts, such values most be generated for each context
  ContextData provides a generic (templated) datatype for this

  LATER (SOONER) think about splitting the render() into a context-specific section that
  set's up display-lists,... and a draw() function that just calls the pre-generated values

  -----------------------------------------------------------------*/

namespace gem {

class GEM_EXTERN ContextDataBase {
 protected:
  static const int INVALID_CONTEXT;
  virtual int getCurContext(void);
  virtual ~ContextDataBase(void);
};


template<class ContextDataType = int>
  class GEM_EXTERN ContextData : ContextDataBase
  {
    private:
    public:

    //////////
    // Constructor
    ContextData(void) : m_haveDefaultValue(false) {;}

    ContextData(ContextDataType v) : m_haveDefaultValue(true), m_defaultValue(v) {;}

    virtual ~ContextData() {
      m_ContextDataVector.clear();
    }

    /**
     * returns the context-specific value
     *
     * @usage ContextData<GLenum>m_fun; m_fun=GL_FUNC_ADD;
     *
     * @pre We are in a draw process.
     * @note Should only be called from the draw function.
     *        Results are un-defined for other functions.
     */
    virtual operator ContextDataType()
    {
      return (*getPtrToCur());
    }

    /**
     * assigns a value to the correct context
     *
     * @pre We are in a draw process.
     * @note Should only be called from the draw function.
     *       Results are un-defined for other functions.
     */
    virtual ContextDataType&operator = (ContextDataType value)
    {
      /* simplistic approach to handle out-of-context assignments:
       *  assign the value to all context instances
       */
      if(INVALID_CONTEXT==getCurContext()) {
        doSetAll(value);
      }

      return (*getPtrToCur()=value);
    }

    private:
    bool m_haveDefaultValue;
    ContextDataType m_defaultValue;
    std::vector<ContextDataType*>  m_ContextDataVector;


    /* Makes sure that the vector is at least requiredSize large */
    void checkSize(unsigned int requiredSize)
    {
      if(requiredSize > m_ContextDataVector.size())
        {
          m_ContextDataVector.reserve(requiredSize);          // Resize smartly
          while(m_ContextDataVector.size() < requiredSize)    // Add any new items needed
            {
                if(m_haveDefaultValue) {
                    m_ContextDataVector.push_back(new ContextDataType(m_defaultValue));
                } else {
                    m_ContextDataVector.push_back(new ContextDataType);
                }
            }
        }
    }

    /**
     * Returns a pointer to the correct data element in the current context.
     *
     * @pre We are in the draw function.
     * @post Synchronized.
     * @note ASSERT: Same context is rendered by same thread each time.
     */
    ContextDataType* getPtrToCur(void)
    {
      // Get current context
      int context_id = getCurContext();
      // Cache ref for better performance
      checkSize(context_id+1);     // Make sure we are large enough (+1 since we have index)

      return m_ContextDataVector[context_id];
    }

    void doSetAll(ContextDataType v)
    {
      unsigned int i=0;
      for(i=0; i< m_ContextDataVector.size(); i++) {
        *m_ContextDataVector[i]=v;
      }
    }
  };
};



#endif	// for header file