History

Reimplement renderGrid to use frag shader

Zach Pomerantz committed 10 years ago

Commit 2bfbf1b1 parent e3ae2ba3 files · +123 −179Browse files

Showing the changes to libraries/render-utils/src/GeometryCache.cpp (1 of 3 files). Show all

Unified · Split

libraries/render-utils/src/GeometryCache.cpp+45−171
@@ -22,17 +22,19 @@ · 21 unmodified lines
2222 #include "TextureCache.h"
2323 #include "RenderUtilsLogging.h"
2424
25−#include "standardTransformPNTC_vert.h"
26−#include "standardDrawTexture_frag.h"
27−
2825 #include "gpu/StandardShaderLib.h"
2926
3027 #include "model/TextureMap.h"
3128
29+#include "standardTransformPNTC_vert.h"
30+#include "standardDrawTexture_frag.h"
31+
3232 #include "simple_vert.h"
3333 #include "simple_textured_frag.h"
3434 #include "simple_textured_emisive_frag.h"
3535
36+#include "grid_frag.h"
37+
3638 //#define WANT_DEBUG
3739
3840 const int GeometryCache::UNKNOWN_ID = -1;
@@ -564,186 +566,42 @@ void GeometryCache::renderWireSphere(gpu::Batch& batch) { · 525 unmodified lines
564566 renderWireShape(batch, Sphere);
565567 }
566568
569+void GeometryCache::renderGrid(gpu::Batch& batch,
570+ const glm::vec2& minCorner, const glm::vec2& maxCorner,
571+ int rows, int cols, const glm::vec4& color, float edge, int id) {
572+ static const glm::vec2 minTexCoord(0.0f, 1.0f);
573+ static const glm::vec2 maxTexCoord(1.0f, 0.0f);
567574
568−void GeometryCache::renderGrid(gpu::Batch& batch, int xDivisions, int yDivisions, const glm::vec4& color) {
569− IntPair key(xDivisions, yDivisions);
570− Vec3Pair colorKey(glm::vec3(color.x, color.y, yDivisions), glm::vec3(color.z, color.y, xDivisions));
571−
572− int vertices = (xDivisions + 1 + yDivisions + 1) * 2;
573− if (!_gridBuffers.contains(key)) {
574− auto verticesBuffer = std::make_shared<gpu::Buffer>();
575−
576− float* vertexData = new float[vertices * 2];
577− float* vertex = vertexData;
578−
579− for (int i = 0; i <= xDivisions; i++) {
580− float x = (float)i / xDivisions;
581−
582− *(vertex++) = x;
583− *(vertex++) = 0.0f;
584−
585− *(vertex++) = x;
586− *(vertex++) = 1.0f;
587− }
588− for (int i = 0; i <= yDivisions; i++) {
589− float y = (float)i / yDivisions;
590−
591− *(vertex++) = 0.0f;
592− *(vertex++) = y;
593−
594− *(vertex++) = 1.0f;
595− *(vertex++) = y;
596− }
597−
598− verticesBuffer->append(sizeof(float) * vertices * 2, (gpu::Byte*) vertexData);
599− delete[] vertexData;
600−
601− _gridBuffers[key] = verticesBuffer;
602− }
603−
604− if (!_gridColors.contains(colorKey)) {
605− auto colorBuffer = std::make_shared<gpu::Buffer>();
606− _gridColors[colorKey] = colorBuffer;
607−
608− int compactColor = ((int(color.x * 255.0f) & 0xFF)) |
609− ((int(color.y * 255.0f) & 0xFF) << 8) |
610− ((int(color.z * 255.0f) & 0xFF) << 16) |
611− ((int(color.w * 255.0f) & 0xFF) << 24);
612−
613− int* colorData = new int[vertices];
614− int* colorDataAt = colorData;
615−
616− for(int v = 0; v < vertices; v++) {
617− *(colorDataAt++) = compactColor;
618− }
619−
620− colorBuffer->append(sizeof(int) * vertices, (gpu::Byte*) colorData);
621− delete[] colorData;
622− }
623− gpu::BufferPointer verticesBuffer = _gridBuffers[key];
624− gpu::BufferPointer colorBuffer = _gridColors[colorKey];
625−
626− const int VERTICES_SLOT = 0;
627− const int COLOR_SLOT = 1;
628− auto streamFormat = std::make_shared<gpu::Stream::Format>(); // 1 for everyone
629−
630− streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::XYZ), 0);
631− streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA));
632−
633− gpu::BufferView verticesView(verticesBuffer, 0, verticesBuffer->getSize(), streamFormat->getAttributes().at(gpu::Stream::POSITION)._element);
634− gpu::BufferView colorView(colorBuffer, streamFormat->getAttributes().at(gpu::Stream::COLOR)._element);
635−
636− batch.setInputFormat(streamFormat);
637− batch.setInputBuffer(VERTICES_SLOT, verticesView);
638− batch.setInputBuffer(COLOR_SLOT, colorView);
639− batch.draw(gpu::LINES, vertices, 0);
640−}
641−
642−// TODO: why do we seem to create extra BatchItemDetails when we resize the window?? what's that??
643−void GeometryCache::renderGrid(gpu::Batch& batch, int x, int y, int width, int height, int rows, int cols, const glm::vec4& color, int id) {
644− #ifdef WANT_DEBUG
645− qCDebug(renderutils) << "GeometryCache::renderGrid(x["<<x<<"], "
646− "y["<<y<<"],"
647− "w["<<width<<"],"
648− "h["<<height<<"],"
649− "rows["<<rows<<"],"
650− "cols["<<cols<<"],"
651− " id:"<<id<<")...";
652− #endif
653−
654575 bool registered = (id != UNKNOWN_ID);
655− Vec3Pair key(glm::vec3(x, y, width), glm::vec3(height, rows, cols));
656− Vec3Pair colorKey(glm::vec3(color.x, color.y, rows), glm::vec3(color.z, color.y, cols));
576+ Vec2FloatPair key(glm::vec2(rows, cols), edge);
657577
658− int vertices = (cols + 1 + rows + 1) * 2;
659− if ((registered && (!_registeredAlternateGridBuffers.contains(id) || _lastRegisteredAlternateGridBuffers[id] != key))
660− || (!registered && !_alternateGridBuffers.contains(key))) {
578+ // Make the gridbuffer
579+ if ((registered && (!_registeredGridBuffers.contains(id) || _lastRegisteredGridBuffer[id] != key)) ||
580+ (!registered && !_gridBuffers.contains(key))) {
581+ GridSchema gridSchema;
582+ GridBuffer gridBuffer = std::make_shared<gpu::Buffer>(sizeof(GridSchema), (const gpu::Byte*) &gridSchema);
661583
662− if (registered && _registeredAlternateGridBuffers.contains(id)) {
663− _registeredAlternateGridBuffers[id].reset();
664− #ifdef WANT_DEBUG
665− qCDebug(renderutils) << "renderGrid()... RELEASING REGISTERED VERTICES BUFFER";
666− #endif
584+ if (registered && _registeredGridBuffers.contains(id)) {
585+ gridBuffer = _registeredGridBuffers[id];
667586 }
668587
669− auto verticesBuffer = std::make_shared<gpu::Buffer>();
670588 if (registered) {
671− _registeredAlternateGridBuffers[id] = verticesBuffer;
672− _lastRegisteredAlternateGridBuffers[id] = key;
589+ _registeredGridBuffers[id] = gridBuffer;
590+ _lastRegisteredGridBuffer[id] = key;
673591 } else {
674− _alternateGridBuffers[key] = verticesBuffer;
675− }
676−
677− float* vertexData = new float[vertices * 2];
678− float* vertex = vertexData;
679−
680− int dx = width / cols;
681− int dy = height / rows;
682− int tx = x;
683− int ty = y;
684−
685− // Draw horizontal grid lines
686− for (int i = rows + 1; --i >= 0; ) {
687− *(vertex++) = x;
688− *(vertex++) = ty;
689−
690− *(vertex++) = x + width;
691− *(vertex++) = ty;
692−
693− ty += dy;
694− }
695− // Draw vertical grid lines
696− for (int i = cols + 1; --i >= 0; ) {
697− *(vertex++) = tx;
698− *(vertex++) = y;
699−
700− *(vertex++) = tx;
701− *(vertex++) = y + height;
702− tx += dx;
703− }
704−
705− verticesBuffer->append(sizeof(float) * vertices * 2, (gpu::Byte*) vertexData);
706− delete[] vertexData;
707− }
708−
709− if (!_gridColors.contains(colorKey)) {
710− auto colorBuffer = std::make_shared<gpu::Buffer>();
711− _gridColors[colorKey] = colorBuffer;
712−
713− int compactColor = ((int(color.x * 255.0f) & 0xFF)) |
714− ((int(color.y * 255.0f) & 0xFF) << 8) |
715− ((int(color.z * 255.0f) & 0xFF) << 16) |
716− ((int(color.w * 255.0f) & 0xFF) << 24);
717−
718− int* colorData = new int[vertices];
719− int* colorDataAt = colorData;
720−
721−
722− for(int v = 0; v < vertices; v++) {
723− *(colorDataAt++) = compactColor;
592+ _gridBuffers[key] = gridBuffer;
724593 }
725594
726− colorBuffer->append(sizeof(int) * vertices, (gpu::Byte*) colorData);
727− delete[] colorData;
595+ gridBuffer.edit<GridSchema>().period = glm::vec2(cols, rows);
596+ gridBuffer.edit<GridSchema>().offset.x = -(edge / cols) / 2;
597+ gridBuffer.edit<GridSchema>().offset.y = -(edge / rows) / 2;
598+ gridBuffer.edit<GridSchema>().balance = glm::vec2(1 - edge);
728599 }
729− gpu::BufferPointer verticesBuffer = registered ? _registeredAlternateGridBuffers[id] : _alternateGridBuffers[key];
730−
731− gpu::BufferPointer colorBuffer = _gridColors[colorKey];
732600
733− const int VERTICES_SLOT = 0;
734− const int COLOR_SLOT = 1;
735− auto streamFormat = std::make_shared<gpu::Stream::Format>(); // 1 for everyone
736−
737− streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::XYZ), 0);
738− streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA));
601+ // Set the grid pipeline
602+ useGridPipeline(batch, registered ? _registeredGridBuffers[id] : _gridBuffers[key]);
739603
740− gpu::BufferView verticesView(verticesBuffer, 0, verticesBuffer->getSize(), streamFormat->getAttributes().at(gpu::Stream::POSITION)._element);
741− gpu::BufferView colorView(colorBuffer, streamFormat->getAttributes().at(gpu::Stream::COLOR)._element);
742−
743− batch.setInputFormat(streamFormat);
744− batch.setInputBuffer(VERTICES_SLOT, verticesView);
745− batch.setInputBuffer(COLOR_SLOT, colorView);
746− batch.draw(gpu::LINES, vertices, 0);
604+ renderQuad(batch, minCorner, maxCorner, minTexCoord, maxTexCoord, color, id);
747605 }
748606
749607 void GeometryCache::updateVertices(int id, const QVector<glm::vec2>& points, const glm::vec4& color) {
@@ -1772,7 +1630,6 @@ void GeometryCache::useSimpleDrawPipeline(gpu::Batch& batch, bool noBlend) { · 1022 unmodified lines
17721630
17731631 auto state = std::make_shared<gpu::State>();
17741632
1775−
17761633 // enable decal blend
17771634 state->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA);
17781635
@@ -1792,6 +1649,23 @@ void GeometryCache::useSimpleDrawPipeline(gpu::Batch& batch, bool noBlend) { · 13 unmodified lines
17921649 }
17931650 }
17941651
1652+void GeometryCache::useGridPipeline(gpu::Batch& batch, GridBuffer gridBuffer) {
1653+ if (!_gridPipeline) {
1654+ auto vs = gpu::Shader::createVertex(std::string(standardTransformPNTC_vert));
1655+ auto ps = gpu::Shader::createPixel(std::string(grid_frag));
1656+ auto program = gpu::Shader::createProgram(vs, ps);
1657+ gpu::Shader::makeProgram((*program));
1658+
1659+ auto state = std::make_shared<gpu::State>();
1660+ state->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA);
1661+
1662+ _gridPipeline = gpu::Pipeline::create(program, state);
1663+ _gridSlot = program->getBuffers().findLocation("gridBuffer");
1664+ }
1665+ batch.setPipeline(_gridPipeline);
1666+ batch.setUniformBuffer(_gridSlot, gridBuffer);
1667+}
1668+
17951669
17961670
17971671 class SimpleProgramKey {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t