Skip to content

Commit 84b7dcc

Browse files
committed
implement texture
1 parent 4484cf4 commit 84b7dcc

File tree

7 files changed

+409
-273
lines changed

7 files changed

+409
-273
lines changed

src/common/command_buffers/gpu/gpu_texture.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ namespace commandbuffers
88

99
struct GPUTextureDescriptor
1010
{
11+
std::string_view label;
12+
GPUTextureUsage usage = GPUTextureUsage::kNone;
13+
GPUTextureDimension dimension = GPUTextureDimension::k2D;
14+
GPUExtent3D size;
15+
GPUTextureFormat format = GPUTextureFormat::kUndefined;
16+
uint32_t mipLevelCount = 1;
17+
uint32_t sampleCount = 1;
18+
size_t viewFormatCount = 0;
19+
GPUTextureFormat const *viewFormats = nullptr;
1120
};
1221

1322
class GPUTextureBase : public GPUHandle

src/renderer/context_webgl.cpp

Lines changed: 98 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <optional>
21
#include <vector>
32

43
#include <renderer/context_webgl.hpp>
@@ -12,14 +11,94 @@ namespace renderer
1211
using namespace std;
1312
using namespace commandbuffers;
1413

15-
string TrContextWebGL::ShaderModule::toString() const
14+
namespace details
1615
{
17-
string type_str = "Unknown";
18-
if (type == WEBGL_VERTEX_SHADER)
19-
type_str = "Vertex";
20-
else if (type == WEBGL_FRAGMENT_SHADER)
21-
type_str = "Fragment";
22-
return "ShaderModule(" + type_str + " id=" + to_string(id) + ")";
16+
ObjectBase::ObjectBase(WebGLuint id)
17+
: id(id)
18+
{
19+
}
20+
21+
bool ObjectBase::isTexture() const
22+
{
23+
return false;
24+
}
25+
26+
bool ObjectBase::isBuffer() const
27+
{
28+
return false;
29+
}
30+
31+
bool ObjectBase::isFramebuffer() const
32+
{
33+
return false;
34+
}
35+
36+
bool ObjectBase::isRenderbuffer() const
37+
{
38+
return false;
39+
}
40+
41+
string ObjectBase::toString() const
42+
{
43+
return to_string(id);
44+
}
45+
46+
void ObjectBase::set(WebGLuint id)
47+
{
48+
this->id = id;
49+
}
50+
51+
void BindableObject::setTarget(const ObjectTargetBase &target)
52+
{
53+
this->target = target.value();
54+
}
55+
56+
ShaderModule::ShaderModule(WebGLuint id, WebGLenum type)
57+
: ObjectBase(id)
58+
, type(type)
59+
{
60+
assert(type == WEBGL_VERTEX_SHADER ||
61+
type == WEBGL_FRAGMENT_SHADER);
62+
}
63+
64+
string ShaderModule::toString() const
65+
{
66+
string type_str = "Unknown";
67+
if (type == WEBGL_VERTEX_SHADER)
68+
type_str = "Vertex";
69+
else if (type == WEBGL_FRAGMENT_SHADER)
70+
type_str = "Fragment";
71+
return "ShaderModule(" + type_str + " id=" + to_string(id) + ")";
72+
}
73+
74+
Program::Program(WebGLuint id)
75+
: ObjectBase(id)
76+
, vertexShader(nullptr)
77+
, fragmentShader(nullptr)
78+
{
79+
}
80+
81+
string Texture::toString() const
82+
{
83+
string target_str = "Unknown";
84+
if (target == WEBGL_TEXTURE_2D)
85+
target_str = "2D";
86+
else if (target == WEBGL2_TEXTURE_2D_ARRAY)
87+
target_str = "2D[]";
88+
else if (target == WEBGL2_TEXTURE_3D)
89+
target_str = "3D";
90+
else if (target == WEBGL_TEXTURE_CUBE_MAP)
91+
target_str = "CubeMap";
92+
93+
return "Texture(" + target_str + " id=" + to_string(id) + ")";
94+
}
95+
96+
void Texture::setSize(WebGLsizei width, WebGLsizei height, WebGLsizei depth)
97+
{
98+
size[0] = width;
99+
size[1] = height;
100+
size[2] = depth;
101+
}
23102
}
24103

25104
TrContextWebGL::TrContextWebGL(Ref<TrContentRenderer> content_renderer)
@@ -84,7 +163,7 @@ namespace renderer
84163
}
85164
case COMMAND_BUFFER_CREATE_TEXTURE_REQ:
86165
{
87-
glCreateTypedObject<CreateTextureCommandBufferRequest>(textures_, req);
166+
glCreateTypedObject<details::Texture, CreateTextureCommandBufferRequest>(textures_, req);
88167
break;
89168
}
90169
case COMMAND_BUFFER_TEXTURE_IMAGE_2D_REQ:
@@ -329,12 +408,14 @@ namespace renderer
329408
}
330409
case COMMAND_BUFFER_CREATE_FRAMEBUFFER_REQ:
331410
{
332-
glCreateTypedObject<CreateFramebufferCommandBufferRequest>(framebuffers_, req);
411+
glCreateTypedObject<details::Framebuffer,
412+
CreateFramebufferCommandBufferRequest>(framebuffers_, req);
333413
break;
334414
}
335415
case COMMAND_BUFFER_CREATE_RENDERBUFFER_REQ:
336416
{
337-
glCreateTypedObject<CreateRenderbufferCommandBufferRequest>(renderbuffers_, req);
417+
glCreateTypedObject<details::Renderbuffer,
418+
CreateRenderbufferCommandBufferRequest>(renderbuffers_, req);
338419
break;
339420
}
340421
case COMMAND_BUFFER_GENERATE_MIPMAP_REQ:
@@ -382,14 +463,14 @@ namespace renderer
382463
{
383464
const auto &typed_req = To<CreateProgramCommandBufferRequest>(req);
384465
auto index = glCreateProgram();
385-
programs_[index].id = req.id; // Modify the id to the request id.
466+
programs_[index]->set(req.id);
386467
break;
387468
}
388469
case COMMAND_BUFFER_CREATE_SHADER_REQ:
389470
{
390471
const auto &typed_req = To<CreateShaderCommandBufferRequest>(req);
391472
auto index = glCreateShader(typed_req.shaderType);
392-
shader_modules_[index].id = req.id; // Modify the id to the request id.
473+
shader_modules_[index]->set(req.id);
393474
break;
394475
}
395476
case COMMAND_BUFFER_DELETE_PROGRAM_REQ:
@@ -701,7 +782,8 @@ namespace renderer
701782
}
702783
case COMMAND_BUFFER_CREATE_BUFFER_REQ:
703784
{
704-
glCreateTypedObject<CreateBufferCommandBufferRequest>(buffers_, req);
785+
glCreateTypedObject<details::Buffer,
786+
CreateBufferCommandBufferRequest>(buffers_, req);
705787
break;
706788
}
707789
case COMMAND_BUFFER_VERTEX_ATTRIB_1F_REQ:
@@ -962,42 +1044,14 @@ namespace renderer
9621044
}
9631045
}
9641046

965-
void TrContextWebGL::glGenObjects(std::vector<WebGLuint> &source_list,
966-
WebGLsizei n,
967-
WebGLuint *generated_list)
968-
{
969-
if (n <= 0 || generated_list == nullptr)
970-
return;
971-
972-
size_t old_size = source_list.size();
973-
WebGLuint initial_value = 0x0;
974-
source_list.resize(old_size + static_cast<size_t>(n));
975-
std::fill(source_list.begin() + old_size,
976-
source_list.end(),
977-
initial_value);
978-
979-
for (size_t i = 0; i < n; i++)
980-
{
981-
generated_list[i] = source_list[old_size + i];
982-
}
983-
}
984-
9851047
void TrContextWebGL::debugPrintPrograms(int depth)
9861048
{
987-
debugPrintObjects<Program>("Programs",
988-
programs_,
989-
depth,
990-
[](const Program &program)
991-
{ return to_string(program.id); });
1049+
debugPrintObjects("Programs", programs_, depth);
9921050
}
9931051

9941052
void TrContextWebGL::debugPrintShaderModules(int depth)
9951053
{
996-
auto print_shader = [](const ShaderModule &module)
997-
{
998-
return module.toString();
999-
};
1000-
debugPrintObjects<ShaderModule>("Shader Modules", shader_modules_, depth, print_shader);
1054+
debugPrintObjects("Shader Modules", shader_modules_, depth);
10011055
}
10021056

10031057
void TrContextWebGL::debugPrintBuffers(int depth)

0 commit comments

Comments
 (0)