12 #ifndef hifi_GeometryCache_h
13 #define hifi_GeometryCache_h
15 #include "model-networking/ModelCache.h"
22 #include <DependencyManager.h>
24 #include <shared/Shapes.h>
26 #include <gpu/Batch.h>
27 #include <gpu/Stream.h>
29 #include <render/ShapePipeline.h>
31 #include <graphics/Material.h>
32 #include <graphics/Asset.h>
34 class SimpleProgramKey;
36 typedef QPair<glm::vec2, float> Vec2FloatPair;
37 typedef QPair<Vec2FloatPair, Vec2FloatPair> Vec2FloatPairPair;
38 typedef QPair<glm::vec2, glm::vec2> Vec2Pair;
39 typedef QPair<Vec2Pair, Vec2Pair> Vec2PairPair;
40 typedef QPair<glm::vec3, glm::vec3> Vec3Pair;
41 typedef QPair<glm::vec4, glm::vec4> Vec4Pair;
42 typedef QPair<Vec3Pair, Vec2Pair> Vec3PairVec2Pair;
43 typedef QPair<Vec3Pair, glm::vec4> Vec3PairVec4;
44 typedef QPair<Vec3Pair, Vec4Pair> Vec3PairVec4Pair;
45 typedef QPair<Vec4Pair, glm::vec4> Vec4PairVec4;
46 typedef QPair<Vec4Pair, Vec4Pair> Vec4PairVec4Pair;
48 inline uint qHash(
const Vec2FloatPairPair& v, uint seed) {
50 return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.second +
51 5021 * v.second.first.x + 5023 * v.second.first.y + 5039 * v.second.second);
54 inline uint qHash(
const Vec2Pair& v, uint seed) {
56 return qHash(v.first.x + 5009 * v.first.y + 5011 * v.second.x + 5021 * v.second.y, seed);
59 inline uint qHash(
const glm::vec4& v, uint seed) {
61 return qHash(v.x + 5009 * v.y + 5011 * v.z + 5021 * v.w, seed);
64 inline uint qHash(
const Vec2PairPair& v, uint seed) {
66 return qHash(v.first.first.x + 5009 * v.first.first.y
67 + 5011 * v.first.second.x + 5021 * v.first.second.y
68 + 5023 * v.second.first.x + 5039 * v.second.first.y
69 + 5051 * v.second.second.x + 5059 * v.second.second.y, seed);
72 inline uint qHash(
const Vec3Pair& v, uint seed) {
74 return qHash(v.first.x + 5009 * v.first.y + 5011 * v.first.z
75 + 5021 * v.second.x + 5023 * v.second.y + 5039 * v.second.z, seed);
78 inline uint qHash(
const Vec4Pair& v, uint seed) {
80 return qHash(v.first.x + 5009 * v.first.y + 5011 * v.first.z + 5021 * v.first.w
81 + 5023 * v.second.x + 5039 * v.second.y + 5051 * v.second.z + 5059 * v.second.w , seed);
84 inline uint qHash(
const Vec3PairVec2Pair& v, uint seed) {
86 return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z +
87 5021 * v.first.second.x + 5023 * v.first.second.y + 5039 * v.first.second.z +
88 5051 * v.second.first.x + 5059 * v.second.first.y +
89 5077 * v.second.second.x + 5081 * v.second.second.y, seed);
92 inline uint qHash(
const Vec3PairVec4& v, uint seed) {
94 return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z +
95 5021 * v.first.second.x + 5023 * v.first.second.y + 5039 * v.first.second.z +
96 5051 * v.second.x + 5059 * v.second.y + 5077 * v.second.z + 5081 * v.second.w, seed);
100 inline uint qHash(
const Vec3PairVec4Pair& v, uint seed) {
102 return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z
103 + 5023 * v.first.second.x + 5039 * v.first.second.y + 5051 * v.first.second.z
104 + 5077 * v.second.first.x + 5081 * v.second.first.y + 5087 * v.second.first.z + 5099 * v.second.first.w
105 + 5101 * v.second.second.x + 5107 * v.second.second.y + 5113 * v.second.second.z + 5119 * v.second.second.w,
109 inline uint qHash(
const Vec4PairVec4& v, uint seed) {
111 return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z + 5021 * v.first.first.w
112 + 5023 * v.first.second.x + 5039 * v.first.second.y + 5051 * v.first.second.z + 5059 * v.first.second.w
113 + 5077 * v.second.x + 5081 * v.second.y + 5087 * v.second.z + 5099 * v.second.w,
117 inline uint qHash(
const Vec4PairVec4Pair& v, uint seed) {
119 return qHash(v.first.first.x + 5009 * v.first.first.y + 5011 * v.first.first.z + 5021 * v.first.first.w
120 + 5023 * v.first.second.x + 5039 * v.first.second.y + 5051 * v.first.second.z + 5059 * v.first.second.w
121 + 5077 * v.second.first.x + 5081 * v.second.first.y + 5087 * v.second.first.z + 5099 * v.second.first.w
122 + 5101 * v.second.second.x + 5107 * v.second.second.y + 5113 * v.second.second.z + 5119 * v.second.second.w,
156 static QString stringFromShape(GeometryCache::Shape geoShape);
158 static void computeSimpleHullPointListForShape(
int entityShape,
const glm::vec3 &entityExtents, QVector<glm::vec3> &outPointList);
160 int allocateID() {
return _nextID++; }
161 void releaseID(
int id);
162 static const int UNKNOWN_ID;
165 void bindSimpleProgram(gpu::Batch& batch,
bool textured =
false,
bool transparent =
false,
bool unlit =
false,
bool depthBias =
false,
166 bool isAntiAliased =
true,
bool forward =
false, graphics::MaterialKey::CullFaceMode cullFaceMode = graphics::MaterialKey::CullFaceMode::CULL_BACK);
168 static gpu::PipelinePointer getSimplePipeline(
bool textured =
false,
bool transparent =
false,
bool unlit =
false,
bool depthBias =
false,
169 bool fading =
false,
bool isAntiAliased =
true,
bool forward =
false, graphics::MaterialKey::CullFaceMode cullFaceMode = graphics::MaterialKey::CullFaceMode::CULL_BACK);
171 void bindWebBrowserProgram(gpu::Batch& batch,
bool transparent,
bool forward);
172 gpu::PipelinePointer getWebBrowserProgram(
bool transparent,
bool forward);
173 static std::map<std::pair<bool, bool>, gpu::PipelinePointer> _webPipelines;
175 static void initializeShapePipelines();
176 render::ShapePipelinePointer getShapePipelinePointer(
bool transparent,
bool unlit,
bool forward,
177 graphics::MaterialKey::CullFaceMode cullFaceMode = graphics::MaterialKey::CULL_BACK) {
return _shapePipelines[std::make_tuple(transparent, unlit, forward, cullFaceMode)]; }
180 void renderShapeInstances(gpu::Batch& batch, Shape shape,
size_t count, gpu::BufferPointer& colorBuffer);
181 void renderWireShapeInstances(gpu::Batch& batch, Shape shape,
size_t count, gpu::BufferPointer& colorBuffer);
183 void renderSolidShapeInstance(RenderArgs* args, gpu::Batch& batch, Shape shape,
const glm::vec4& color,
184 const render::ShapePipelinePointer& pipeline);
185 void renderWireShapeInstance(RenderArgs* args, gpu::Batch& batch, Shape shape,
const glm::vec4& color,
186 const render::ShapePipelinePointer& pipeline);
188 void renderSolidSphereInstance(RenderArgs* args, gpu::Batch& batch,
const glm::vec4& color,
189 const render::ShapePipelinePointer& pipeline);
190 void renderSolidSphereInstance(RenderArgs* args, gpu::Batch& batch,
const glm::vec3& color,
191 const render::ShapePipelinePointer& pipeline) {
192 renderSolidSphereInstance(args, batch, glm::vec4(color, 1.0f), pipeline);
195 void renderWireCubeInstance(RenderArgs* args, gpu::Batch& batch,
const glm::vec4& color,
196 const render::ShapePipelinePointer& pipeline);
197 void renderWireCubeInstance(RenderArgs* args, gpu::Batch& batch,
const glm::vec3& color,
198 const render::ShapePipelinePointer& pipeline) {
199 renderWireCubeInstance(args, batch, glm::vec4(color, 1.0f), pipeline);
203 void renderShape(gpu::Batch& batch, Shape shape, gpu::BufferPointer& colorBuffer);
204 void renderWireShape(gpu::Batch& batch, Shape shape, gpu::BufferPointer& colorBuffer);
205 size_t getShapeTriangleCount(Shape shape);
207 void renderGrid(gpu::Batch& batch,
const glm::vec2& minCorner,
const glm::vec2& maxCorner,
208 int majorRows,
int majorCols,
float majorEdge,
209 int minorRows,
int minorCols,
float minorEdge,
210 const glm::vec4& color,
bool forward,
int id);
212 void renderBevelCornersRect(gpu::Batch& batch,
int x,
int y,
int width,
int height,
int bevelDistance,
const glm::vec4& color,
int id);
214 void renderUnitQuad(gpu::Batch& batch,
const glm::vec4& color,
int id);
216 void renderQuad(gpu::Batch& batch,
int x,
int y,
int width,
int height,
const glm::vec4& color,
int id)
217 { renderQuad(batch, glm::vec2(x,y), glm::vec2(x + width, y + height), color,
id); }
222 void renderQuad(gpu::Batch& batch,
const glm::vec2& minCorner,
const glm::vec2& maxCorner,
const glm::vec4& color,
int id);
224 void renderQuad(gpu::Batch& batch,
const glm::vec2& minCorner,
const glm::vec2& maxCorner,
225 const glm::vec2& texCoordMinCorner,
const glm::vec2& texCoordMaxCorner,
226 const glm::vec4& color,
int id);
228 void renderQuad(gpu::Batch& batch,
const glm::vec3& minCorner,
const glm::vec3& maxCorner,
const glm::vec4& color,
int id);
230 void renderQuad(gpu::Batch& batch,
const glm::vec3& topLeft,
const glm::vec3& bottomLeft,
231 const glm::vec3& bottomRight,
const glm::vec3& topRight,
232 const glm::vec2& texCoordTopLeft,
const glm::vec2& texCoordBottomLeft,
233 const glm::vec2& texCoordBottomRight,
const glm::vec2& texCoordTopRight,
234 const glm::vec4& color,
int id);
237 void renderLine(gpu::Batch& batch,
const glm::vec3& p1,
const glm::vec3& p2,
const glm::vec3& color,
int id)
238 { renderLine(batch, p1, p2, color, color,
id); }
240 void renderLine(gpu::Batch& batch,
const glm::vec3& p1,
const glm::vec3& p2,
241 const glm::vec3& color1,
const glm::vec3& color2,
int id)
242 { renderLine(batch, p1, p2, glm::vec4(color1, 1.0f), glm::vec4(color2, 1.0f),
id); }
244 void renderLine(gpu::Batch& batch,
const glm::vec3& p1,
const glm::vec3& p2,
245 const glm::vec4& color,
int id)
246 { renderLine(batch, p1, p2, color, color,
id); }
248 void renderLine(gpu::Batch& batch,
const glm::vec3& p1,
const glm::vec3& p2,
249 const glm::vec4& color1,
const glm::vec4& color2,
int id);
251 void renderDashedLine(gpu::Batch& batch,
const glm::vec3& start,
const glm::vec3& end,
const glm::vec4& color,
int id)
252 { renderDashedLine(batch, start, end, color, 0.05f, 0.025f,
id); }
254 void renderDashedLine(gpu::Batch& batch,
const glm::vec3& start,
const glm::vec3& end,
const glm::vec4& color,
255 const float dash_length,
const float gap_length,
int id);
257 void updateVertices(
int id,
const QVector<glm::vec2>& points,
const glm::vec4& color);
258 void updateVertices(
int id,
const QVector<glm::vec2>& points,
const QVector<glm::vec4>& colors);
259 void updateVertices(
int id,
const QVector<glm::vec3>& points,
const glm::vec4& color);
260 void updateVertices(
int id,
const QVector<glm::vec3>& points,
const QVector<glm::vec4>& colors);
261 void updateVertices(
int id,
const QVector<glm::vec3>& points,
const QVector<glm::vec2>& texCoords,
const glm::vec4& color);
262 void renderVertices(gpu::Batch& batch, gpu::Primitive primitiveType,
int id);
268 ShapeVertex(
const vec3& pos,
const vec3& normal,
const vec2& uv,
const vec3& tangent) : pos(pos), normal(normal), uv(uv), tangent(tangent) {}
277 gpu::BufferView _positionView;
278 gpu::BufferView _normalView;
279 gpu::BufferView _texCoordView;
280 gpu::BufferView _tangentView;
281 gpu::BufferView _indicesView;
282 gpu::BufferView _wireIndicesView;
284 void setupVertices(gpu::BufferPointer& vertexBuffer,
const std::vector<ShapeVertex>& vertices);
285 void setupIndices(gpu::BufferPointer& indexBuffer,
const geometry::IndexVector& indices,
const geometry::IndexVector& wireIndices);
286 void setupBatch(gpu::Batch& batch)
const;
287 void draw(gpu::Batch& batch)
const;
288 void drawWire(gpu::Batch& batch)
const;
289 void drawInstances(gpu::Batch& batch,
size_t count)
const;
290 void drawWireInstances(gpu::Batch& batch,
size_t count)
const;
293 using VShape = std::array<ShapeData, NUM_SHAPES>;
299 graphics::MeshPointer meshFromShape(Shape geometryShape, glm::vec3 color);
301 static uint32_t toCompactColor(
const glm::vec4& color);
309 typedef QPair<int, int> IntPair;
310 typedef QPair<unsigned int, unsigned int> VerticesIndices;
315 gpu::PipelinePointer _standardDrawPipeline;
316 gpu::PipelinePointer _standardDrawPipelineNoBlend;
318 gpu::BufferPointer _shapeVertices{ std::make_shared<gpu::Buffer>() };
319 gpu::BufferPointer _shapeIndices{ std::make_shared<gpu::Buffer>() };
328 using GridBuffer = gpu::BufferView;
329 void useGridPipeline(gpu::Batch& batch, GridBuffer gridBuffer,
bool transparent,
bool forward);
330 static std::map<std::pair<bool, bool>, gpu::PipelinePointer> _gridPipelines;
332 class BatchItemDetails {
334 static int population;
335 gpu::BufferPointer verticesBuffer;
336 gpu::BufferPointer normalBuffer;
337 gpu::BufferPointer colorBuffer;
338 gpu::BufferPointer uniformBuffer;
339 gpu::Stream::FormatPointer streamFormat;
340 gpu::BufferStreamPointer stream;
347 BatchItemDetails(
const GeometryCache::BatchItemDetails& other);
352 QHash<IntPair, VerticesIndices> _coneVBOs;
356 QHash<int, Vec3PairVec4Pair> _lastRegisteredQuad3DTexture;
357 QHash<int, BatchItemDetails> _registeredQuad3DTextures;
359 QHash<int, Vec4PairVec4> _lastRegisteredQuad2DTexture;
360 QHash<int, BatchItemDetails> _registeredQuad2DTextures;
362 QHash<int, Vec3PairVec4> _lastRegisteredQuad3D;
363 QHash<int, BatchItemDetails> _registeredQuad3D;
365 QHash<int, Vec4Pair> _lastRegisteredQuad2D;
366 QHash<int, BatchItemDetails> _registeredQuad2D;
368 QHash<int, Vec3Pair> _lastRegisteredBevelRects;
369 QHash<int, BatchItemDetails> _registeredBevelRects;
371 QHash<int, Vec3Pair> _lastRegisteredLine3D;
372 QHash<int, BatchItemDetails> _registeredLine3DVBOs;
374 QHash<int, Vec2Pair> _lastRegisteredLine2D;
375 QHash<int, BatchItemDetails> _registeredLine2DVBOs;
377 QHash<int, BatchItemDetails> _registeredVertices;
379 QHash<int, Vec3PairVec2Pair> _lastRegisteredDashedLines;
380 QHash<int, BatchItemDetails> _registeredDashedLines;
382 QHash<int, Vec2FloatPairPair> _lastRegisteredGridBuffer;
383 QHash<int, GridBuffer> _registeredGridBuffers;
386 static std::map<std::tuple<bool, bool, bool, bool>, gpu::ShaderPointer> _shapeShaders;
388 static std::map<std::tuple<bool, bool, bool, graphics::MaterialKey::CullFaceMode>, render::ShapePipelinePointer> _shapePipelines;
389 static QHash<SimpleProgramKey, gpu::PipelinePointer> _simplePrograms;
391 static render::ShapePipelinePointer getShapePipeline(
bool textured =
false,
bool transparent =
false,
bool unlit =
false,
392 bool depthBias =
false,
bool forward =
false, graphics::MaterialKey::CullFaceMode cullFaceMode = graphics::MaterialKey::CullFaceMode::CULL_BACK);
393 static render::ShapePipelinePointer getFadingShapePipeline(
bool textured =
false,
bool transparent =
false,
bool unlit =
false,
394 bool depthBias =
false,
bool forward =
false, graphics::MaterialKey::CullFaceMode cullFaceMode = graphics::MaterialKey::CullFaceMode::CULL_BACK);
Stores cached geometry.
Definition: GeometryCache.h:127
void useSimpleDrawPipeline(gpu::Batch &batch, bool noBlend=false)
Set a batch to the simple pipeline, returning the previous pipeline.
Definition: GeometryCache.cpp:1672
static GeometryCache::Shape getShapeForEntityShape(int entityShapeEnum)
Definition: GeometryCache.cpp:607
const ShapeData * getShapeData(Shape shape) const
Definition: GeometryCache.cpp:596