2010-12-26 MORITA Hajime <morrita@google.com>
[WebKit.git] / Sources / ThirdParty / ANGLE / src / libGLESv2 / geometry / dx9.h
1 //
2 // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // geometry/dx9.h: Direct3D 9-based implementation of BufferBackEnd, TranslatedVertexBuffer and TranslatedIndexBuffer.
8
9 #ifndef LIBGLESV2_GEOMETRY_DX9_H_
10 #define LIBGLESV2_GEOMETRY_DX9_H_
11
12 #include <d3d9.h>
13
14 #include "libGLESv2/Buffer.h"
15 #include "libGLESv2/geometry/backend.h"
16
17 namespace gl
18 {
19
20 class Dx9BackEnd : public BufferBackEnd
21 {
22   public:
23     explicit Dx9BackEnd(Context *context, IDirect3DDevice9 *d3ddevice);
24     ~Dx9BackEnd();
25
26     virtual bool supportIntIndices();
27
28     virtual TranslatedVertexBuffer *createVertexBuffer(std::size_t size);
29     virtual TranslatedVertexBuffer *createVertexBufferForStrideZero(std::size_t size);
30     virtual TranslatedIndexBuffer *createIndexBuffer(std::size_t size, GLenum type);
31     virtual FormatConverter getFormatConverter(GLenum type, std::size_t size, bool normalize);
32
33     virtual bool validateStream(GLenum type, std::size_t size, std::size_t stride, std::size_t offset) const;
34
35     virtual GLenum setupIndicesPreDraw(const TranslatedIndexData &indexInfo);
36     virtual GLenum setupAttributesPreDraw(const TranslatedAttribute *attributes);
37
38   private:
39     IDirect3DDevice9 *mDevice;
40
41     bool mUseInstancingForStrideZero;
42     bool mSupportIntIndices;
43
44     bool mAppliedAttribEnabled[MAX_VERTEX_ATTRIBS];
45
46     enum StreamFrequency
47     {
48         STREAM_FREQUENCY_UNINSTANCED = 0,
49         STREAM_FREQUENCY_INDEXED,
50         STREAM_FREQUENCY_INSTANCED
51     };
52
53     StreamFrequency mStreamFrequency[MAX_VERTEX_ATTRIBS+1];
54
55     struct TranslationInfo
56     {
57         FormatConverter formatConverter;
58         D3DDECLTYPE d3dDeclType;
59     };
60
61     enum { NUM_GL_VERTEX_ATTRIB_TYPES = 6 };
62
63     TranslationInfo mAttributeTypes[NUM_GL_VERTEX_ATTRIB_TYPES][2][4]; // [GL types as enumerated by typeIndex()][normalized][size-1]
64
65     struct TranslationDescription
66     {
67         DWORD capsFlag;
68         TranslationInfo preferredConversion;
69         TranslationInfo fallbackConversion;
70     };
71
72     // This table is used to generate mAttributeTypes.
73     static const TranslationDescription mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4]; // [GL types as enumerated by typeIndex()][normalized][size-1]
74
75     void checkVertexCaps(DWORD declTypes);
76
77     unsigned int typeIndex(GLenum type) const;
78
79     class Dx9VertexBuffer : public TranslatedVertexBuffer
80     {
81       public:
82         Dx9VertexBuffer(IDirect3DDevice9 *device, std::size_t size);
83         virtual ~Dx9VertexBuffer();
84
85         IDirect3DVertexBuffer9 *getBuffer() const;
86
87       protected:
88         Dx9VertexBuffer(IDirect3DDevice9 *device, std::size_t size, DWORD usageFlags);
89
90         virtual void *map();
91         virtual void unmap();
92
93         virtual void recycle();
94         virtual void *streamingMap(std::size_t offset, std::size_t size);
95
96       private:
97         IDirect3DVertexBuffer9 *mVertexBuffer;
98     };
99
100     class Dx9VertexBufferZeroStrideWorkaround : public Dx9VertexBuffer
101     {
102       public:
103         Dx9VertexBufferZeroStrideWorkaround(IDirect3DDevice9 *device, std::size_t size);
104
105       protected:
106         virtual void recycle();
107         virtual void *streamingMap(std::size_t offset, std::size_t size);
108     };
109
110     class Dx9IndexBuffer : public TranslatedIndexBuffer
111     {
112       public:
113         Dx9IndexBuffer(IDirect3DDevice9 *device, std::size_t size, GLenum type);
114         virtual ~Dx9IndexBuffer();
115
116         IDirect3DIndexBuffer9 *getBuffer() const;
117
118       protected:
119         virtual void *map();
120         virtual void unmap();
121
122         virtual void recycle();
123         virtual void *streamingMap(std::size_t offset, std::size_t size);
124
125       private:
126         IDirect3DIndexBuffer9 *mIndexBuffer;
127     };
128
129     IDirect3DVertexBuffer9 *getDxBuffer(TranslatedVertexBuffer *vb) const;
130     IDirect3DIndexBuffer9 *getDxBuffer(TranslatedIndexBuffer *ib) const;
131
132     D3DDECLTYPE mapAttributeType(GLenum type, std::size_t size, bool normalized) const;
133 };
134
135 }
136
137 #endif   // LIBGLESV2_GEOMETRY_DX9_H_