Make `get_vertex_attr()` fallible

This commit is contained in:
Patrick Walton 2019-06-12 10:46:00 -07:00
parent 636ff6dbf8
commit dbf02fb742
4 changed files with 57 additions and 40 deletions

View File

@ -304,12 +304,16 @@ impl Device for GLDevice {
}
}
fn get_vertex_attr(&self, program: &Self::Program, name: &str) -> GLVertexAttr {
fn get_vertex_attr(&self, program: &Self::Program, name: &str) -> Option<GLVertexAttr> {
let name = CString::new(format!("a{}", name)).unwrap();
let attr = unsafe {
gl::GetAttribLocation(program.gl_program, name.as_ptr() as *const GLchar) as GLuint
gl::GetAttribLocation(program.gl_program, name.as_ptr() as *const GLchar)
}; ck();
GLVertexAttr { attr }
if attr < 0 {
None
} else {
Some(GLVertexAttr { attr: attr as GLuint })
}
}
fn get_uniform(&self, program: &GLProgram, name: &str) -> GLUniform {

View File

@ -44,7 +44,7 @@ pub trait Device {
vertex_shader: Self::Shader,
fragment_shader: Self::Shader,
) -> Self::Program;
fn get_vertex_attr(&self, program: &Self::Program, name: &str) -> Self::VertexAttr;
fn get_vertex_attr(&self, program: &Self::Program, name: &str) -> Option<Self::VertexAttr>;
fn get_uniform(&self, program: &Self::Program, name: &str) -> Self::Uniform;
fn use_program(&self, program: &Self::Program);
fn configure_vertex_attr(&self, attr: &Self::VertexAttr, descriptor: &VertexAttrDescriptor);

View File

@ -944,12 +944,12 @@ where
BufferUploadMode::Dynamic,
);
let tess_coord_attr = device.get_vertex_attr(&fill_program.program, "TessCoord");
let from_px_attr = device.get_vertex_attr(&fill_program.program, "FromPx");
let to_px_attr = device.get_vertex_attr(&fill_program.program, "ToPx");
let from_subpx_attr = device.get_vertex_attr(&fill_program.program, "FromSubpx");
let to_subpx_attr = device.get_vertex_attr(&fill_program.program, "ToSubpx");
let tile_index_attr = device.get_vertex_attr(&fill_program.program, "TileIndex");
let tess_coord_attr = device.get_vertex_attr(&fill_program.program, "TessCoord").unwrap();
let from_px_attr = device.get_vertex_attr(&fill_program.program, "FromPx").unwrap();
let to_px_attr = device.get_vertex_attr(&fill_program.program, "ToPx").unwrap();
let from_subpx_attr = device.get_vertex_attr(&fill_program.program, "FromSubpx").unwrap();
let to_subpx_attr = device.get_vertex_attr(&fill_program.program, "ToSubpx").unwrap();
let tile_index_attr = device.get_vertex_attr(&fill_program.program, "TileIndex").unwrap();
device.bind_vertex_array(&vertex_array);
device.use_program(&fill_program.program);
@ -1029,10 +1029,14 @@ where
) -> AlphaTileVertexArray<D> {
let (vertex_array, vertex_buffer) = (device.create_vertex_array(), device.create_buffer());
let tess_coord_attr = device.get_vertex_attr(&alpha_tile_program.program, "TessCoord");
let tile_origin_attr = device.get_vertex_attr(&alpha_tile_program.program, "TileOrigin");
let backdrop_attr = device.get_vertex_attr(&alpha_tile_program.program, "Backdrop");
let tile_index_attr = device.get_vertex_attr(&alpha_tile_program.program, "TileIndex");
let tess_coord_attr = device.get_vertex_attr(&alpha_tile_program.program, "TessCoord")
.unwrap();
let tile_origin_attr = device.get_vertex_attr(&alpha_tile_program.program, "TileOrigin")
.unwrap();
let backdrop_attr = device.get_vertex_attr(&alpha_tile_program.program, "Backdrop")
.unwrap();
let tile_index_attr = device.get_vertex_attr(&alpha_tile_program.program, "TileIndex")
.unwrap();
let color_tex_coord_attr = device.get_vertex_attr(&alpha_tile_program.program,
"ColorTexCoord");
@ -1074,14 +1078,16 @@ where
offset: 6,
divisor: 1,
});
device.configure_vertex_attr(&color_tex_coord_attr, &VertexAttrDescriptor {
size: 2,
class: VertexAttrClass::FloatNorm,
attr_type: VertexAttrType::U16,
stride: MASK_TILE_INSTANCE_SIZE,
offset: 8,
divisor: 1,
});
if let Some(color_tex_coord_attr) = color_tex_coord_attr {
device.configure_vertex_attr(&color_tex_coord_attr, &VertexAttrDescriptor {
size: 2,
class: VertexAttrClass::FloatNorm,
attr_type: VertexAttrType::U16,
stride: MASK_TILE_INSTANCE_SIZE,
offset: 8,
divisor: 1,
});
}
device.bind_buffer(quad_vertex_indices_buffer, BufferTarget::Index);
AlphaTileVertexArray { vertex_array, vertex_buffer }
@ -1108,8 +1114,10 @@ where
) -> SolidTileVertexArray<D> {
let (vertex_array, vertex_buffer) = (device.create_vertex_array(), device.create_buffer());
let tess_coord_attr = device.get_vertex_attr(&solid_tile_program.program, "TessCoord");
let tile_origin_attr = device.get_vertex_attr(&solid_tile_program.program, "TileOrigin");
let tess_coord_attr = device.get_vertex_attr(&solid_tile_program.program, "TessCoord")
.unwrap();
let tile_origin_attr = device.get_vertex_attr(&solid_tile_program.program, "TileOrigin")
.unwrap();
let color_tex_coord_attr = device.get_vertex_attr(&solid_tile_program.program,
"ColorTexCoord");
@ -1135,14 +1143,16 @@ where
offset: 0,
divisor: 1,
});
device.configure_vertex_attr(&color_tex_coord_attr, &VertexAttrDescriptor {
size: 2,
class: VertexAttrClass::FloatNorm,
attr_type: VertexAttrType::U16,
stride: SOLID_TILE_INSTANCE_SIZE,
offset: 4,
divisor: 1,
});
if let Some(color_tex_coord_attr) = color_tex_coord_attr {
device.configure_vertex_attr(&color_tex_coord_attr, &VertexAttrDescriptor {
size: 2,
class: VertexAttrClass::FloatNorm,
attr_type: VertexAttrType::U16,
stride: SOLID_TILE_INSTANCE_SIZE,
offset: 4,
divisor: 1,
});
}
device.bind_buffer(quad_vertex_indices_buffer, BufferTarget::Index);
SolidTileVertexArray { vertex_array, vertex_buffer }
@ -1409,7 +1419,8 @@ where
quad_vertex_indices_buffer: &D::Buffer,
) -> PostprocessVertexArray<D> {
let vertex_array = device.create_vertex_array();
let position_attr = device.get_vertex_attr(&postprocess_program.program, "Position");
let position_attr = device.get_vertex_attr(&postprocess_program.program, "Position")
.unwrap();
device.bind_vertex_array(&vertex_array);
device.use_program(&postprocess_program.program);
@ -1462,8 +1473,7 @@ where
let vertex_array = device.create_vertex_array();
let (vertex_buffer, index_buffer) = (device.create_buffer(), device.create_buffer());
let position_attr = device.get_vertex_attr(&stencil_program.program, "Position");
let position_attr = device.get_vertex_attr(&stencil_program.program, "Position").unwrap();
device.bind_vertex_array(&vertex_array);
device.use_program(&stencil_program.program);
device.bind_buffer(&vertex_buffer, BufferTarget::Vertex);
@ -1529,8 +1539,8 @@ where
) -> ReprojectionVertexArray<D> {
let vertex_array = device.create_vertex_array();
let position_attr = device.get_vertex_attr(&reprojection_program.program, "Position");
let position_attr = device.get_vertex_attr(&reprojection_program.program, "Position")
.unwrap();
device.bind_vertex_array(&vertex_array);
device.use_program(&reprojection_program.program);
device.bind_buffer(quad_vertex_positions_buffer, BufferTarget::Vertex);

View File

@ -585,8 +585,10 @@ impl<D> DebugTextureVertexArray<D> where D: Device {
let (vertex_buffer, index_buffer) = (device.create_buffer(), device.create_buffer());
let vertex_array = device.create_vertex_array();
let position_attr = device.get_vertex_attr(&debug_texture_program.program, "Position");
let tex_coord_attr = device.get_vertex_attr(&debug_texture_program.program, "TexCoord");
let position_attr = device.get_vertex_attr(&debug_texture_program.program, "Position")
.unwrap();
let tex_coord_attr = device.get_vertex_attr(&debug_texture_program.program, "TexCoord")
.unwrap();
device.bind_vertex_array(&vertex_array);
device.use_program(&debug_texture_program.program);
@ -624,7 +626,8 @@ impl<D> DebugSolidVertexArray<D> where D: Device {
let (vertex_buffer, index_buffer) = (device.create_buffer(), device.create_buffer());
let vertex_array = device.create_vertex_array();
let position_attr = device.get_vertex_attr(&debug_solid_program.program, "Position");
let position_attr = device.get_vertex_attr(&debug_solid_program.program, "Position")
.unwrap();
device.bind_vertex_array(&vertex_array);
device.use_program(&debug_solid_program.program);
device.bind_buffer(&vertex_buffer, BufferTarget::Vertex);